Crates.io | byo-graphql |
lib.rs | byo-graphql |
version | 0.2.0 |
source | src |
created_at | 2020-12-03 15:01:54.601251 |
updated_at | 2024-03-11 19:32:25.239945 |
description | simple bring-your-own graphql client |
homepage | |
repository | https://github.com/Canop/byo-graphql |
max_upload_size | |
id | 319293 |
size | 51,709 |
A simple "bring your own queries and types" GraphQL client.
The github stars example demonstrates querying GitHub's GraphQL API to get the number of stars of a repository.
First create a client, that you may keep and reuse:
let mut graphql_client = GraphqlClient::new("https://api.github.com/graphql")?;
graphql_client.set_bearer_auth("your-github-api-token");
You need the struct into which to deserialize the server's answer:
#[derive(Deserialize)]
pub struct Repository {
stargazers: Count,
}
(Count
is a utility struct provided by byo_graphql, it's just struct Count { totalCount: usize }
)
And you need a query:
let query = r#"{
repository(owner: "Canop", name: "bacon") {
stargazers {
totalCount
}
}
}"#;
note: in the example's complete code, the query is dynamically built with format!
, as you'll usually do.
Now you can fetch the data:
let repo: Repository = graphql_client.get_first_item(query).await?;
let stars: usize = repo.stargazers.into();
The github issues example demonstrates how to query a long list with a cursor based exchange.