A simple "bring your own queries and types" GraphQL client.
The github 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:
rust
let mut graphql_client = GraphqlClient::new("https://api.github.com/graphql")?;
graphql_client.set_bearer_auth("your-github-api-token");
You need the structs into which to deserialize the server's answer:
```rust
pub struct Repository { stargazers: RepoStargazers, }
pub struct RepoStargazers { totalCount: usize, } ```
And you need a query:
rust
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.
You now can fetch and display the data:
rust
let repo: Repository = graphql_client.get_first_item(query)?;
println!("stars: {}", repo.stargazers.totalCount);