A typed GraphQL client library for Rust.
- Precise types for query variables and responses.
- Supports GraphQL fragments, objects, unions, inputs, enums, custom scalars and input objects.
- Works in the browser (WebAssembly).
- Subscriptions support (serialization-deserialization only at the moment).
- Copies documentation from the GraphQL schema to the generated Rust code.
- Arbitrary derives on the generated responses.
- Arbitrary custom scalars.
- Supports multiple operations per query document.
- Supports setting GraphQL fields as deprecated and having the Rust compiler check their use.
- Optional reqwest-based client for boilerplate-free API calls from browsers.
- Implicit and explicit null support.
If you are not familiar with GraphQL, the official website provides a very good and comprehensive introduction.
Once you have written your query (most likely in something like graphiql), save it in a
.graphqlfile in your project.
In order to provide precise types for a response, graphql_client needs to read the query and the schema at compile-time.
To download the schema, you have multiple options. This projects provides a CLI, however it does not matter what tool you use, the resulting
schema.jsonis the same.
We now have everything we need to derive Rust types for our query. This is achieved through a procedural macro, as in the following snippet:
use GraphQLQuery; // The paths are relative to the directory where your `Cargo.toml` is located. // Both json and the GraphQL schema language are supported as sources for the schema ;
derivewill generate a module named
union_queryin this example - the name is the struct's name, but in snake case.
That module contains all the struct and enum definitions necessary to deserialize a response to that query.
The root type for the response is named
ResponseData. The GraphQL response will take the form of a
Response<ResponseData>(the Response type is always the same).
The module also contains a struct called
Variablesrepresenting the variables expected by the query.
We now need to create the complete payload that we are going to send to the server. For convenience, the GraphQLQuery trait, is implemented for the struct under derive, so a complete query body can be created this way:
use ; use Error; use reqwest; ; async
Alternative workflow using the CLI
You can introspect GraphQL APIs and generate module from a command line interface to the library:
Deriving specific traits on the response
The generated response types always derive
serde::Deserialize but you may want to print them (
Debug), compare them (
PartialEq) or derive any other trait on it. You can achieve this with the
response_derives option of the
graphql attribute. Example:
use GraphQLQuery; ;
The generated code will skip the serialization of
use GraphQLQuery; ;
In GraphQL, five scalar types,
ID, are available out of the box and are automatically mapped to equivalent types in Rust. However, in addition, custom scalar types can be defined by service providers by adding declarations like
scalar URI to the server schema.
If such custom scalar types are defined in the schema, depending on the content of the query, the generated code will also reference those scalar types. This means you have to provide matching Rust types in the scope of the struct under derive. It can be as simple as declarations like
type URI = String;. This gives you complete freedom on how to treat custom scalars, as long as they can be deserialized. If such declarations are not provided, you will get build errors like this:
error[E0412]: cannot find type `URI` in module `super` | | #[derive(GraphQLQuery)] | ^^^^^^^^^^^^ not found in `super` | = note: possible candidate is found in another module, you can import it into scope: crate::repo_view::URI
The generated code has support for
field annotations. You can configure how deprecations are handled via the
deprecated argument in the
use GraphQLQuery; ;
Valid values are:
allow: the response struct fields are not marked as deprecated.
warn: the response struct fields are marked as
deny: The struct fields are not included in the response struct and using them is a compile error.
The default is
Query documents with multiple operations
You can write multiple operations in one query document (one
.graphql file). You can then select one by naming the struct you
#[derive(GraphQLQuery)] on with the same name as one of the operations. This is neat, as it allows sharing fragments between operations.
Note that the struct and the operation in the GraphQL file must have the same name. We enforce this to make the generated code more predictable.
use GraphQLQuery; ;
There is an example in the tests.
Documentation for the generated modules
You can use
cargo doc --document-private-items to generate rustdoc documentation on the generated code.
Make cargo recompile when .graphql files have changed
See the examples directory in this repository.
Warmest thanks to all those who contributed in any way (not only code) to this project:
- Alex Vlasov (@indifferentalex)
- Ben Boeckel (@mathstuf)
- Chris Fung (@aergonaut)
- Christian Legnitto (@LegNeato)
- David Gräff (@davidgraeff)
- Dirkjan Ochtman (@djc)
- Fausto Nunez Alberro (@brainlessdeveloper)
- Hirokazu Hata (@h-michael)
- Peter Gundel (@peterfication)
- Sonny Scroggin (@scrogson)
- Sooraj Chandran (@SoorajChandran)
- Tom Houlé (@tomhoule)
Code of conduct
Anyone who interacts with this project in any space, including but not limited to this GitHub repository, must follow our code of conduct.
Licensed under either of these:
- Apache License, Version 2.0, (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)
Unless you explicitly state otherwise, any contribution you intentionally submit for inclusion in the work, as defined in the Apache-2.0 license, shall be dual-licensed as above, without any additional terms or conditions.