|
4 | 4 | [](https://docs.rs/graphql_client/0.0.1/graphql_client/)
|
5 | 5 | [](https://crates.io/crates/graphql_client)
|
6 | 6 |
|
7 |
| -Derive Rust code to safely and ergonomically manipulate GraphQL queries. |
| 7 | +A typed, ergonomic GraphQL client library for Rust. |
8 | 8 |
|
9 |
| -This library does not provide any networking, caching or other client functionality yet, it is just meant to make it easy to interact with a GraphQL query and the corresponding response in a strongly typed way. Making a request can be as simple as this: |
| 9 | +## Getting started |
| 10 | + |
| 11 | +In order to provide precise types for a response, graphql_client needs to read the query and the schema at compile-time. |
| 12 | + |
| 13 | +This is achieved through a procedural macro, as in the following snippet: |
10 | 14 |
|
11 | 15 | ```rust
|
| 16 | +// The paths are relative to the directory where your `Cargo.toml` is located. |
| 17 | +// Both json and the GraphQL schema language are supported as sources for the schema |
12 | 18 | #[derive(GraphQLQuery)]
|
13 | 19 | #[graphql(
|
14 |
| - // The paths are relative to the directory where your `Cargo.toml` is located. |
| 20 | + schema_path = "src/graphql/schema.json", |
15 | 21 | query_path = "src/graphql/queries/my_query.graphql",
|
16 |
| - schema_path = "src/graphql/schema.json" |
17 | 22 | )]
|
18 |
| -struct MyQuery; |
| 23 | +pub struct MyQuery; |
19 | 24 | ```
|
20 | 25 |
|
| 26 | +The `derive` will generate a module named `my_query` in this example - the name is the struct's name, but in snake case. |
| 27 | + |
| 28 | +That module contains all the struct and enum definitions necessary to deserialize a response to that query. |
| 29 | + |
| 30 | +The root type for the response is named `ResponseData`. The GraphQL response will take the form of a `GraphQLResponse<ResponseData>` (the [GraphQLResponse](https://docs.rs/graphql_client/latest/graphql_client/struct.GraphQLResponse.html) type is always the same). |
| 31 | + |
| 32 | +The module also contains a struct called `Variables` representing the variables expected by the query. |
| 33 | + |
| 34 | +[A full example is available](https://github.com/tomhoule/graphql-client/tree/master/examples/example_module), including [rustdoc output](https://www.tomhoule.com/docs/example_module/). |
| 35 | + |
| 36 | +NOTE: `serde` and `serde_derive` need to be imported in the current crate with `extern crate`. |
| 37 | + |
| 38 | +For convenience, the [GraphQLQuery trait](https://docs.rs/graphql_client/latest/graphql_client/trait.GraphQLQuery.html), is implemented for the struct under derive, so it can be used this way: |
| 39 | + |
21 | 40 | ```rust
|
22 | 41 | fn perform_my_query(variables: &my_query::Variables) -> Result<(), failure::Error> {
|
23 |
| - let body = MyQuery::expand(variables); |
| 42 | + let request_body = MyQuery::expand(variables); |
24 | 43 | let client = reqwest::Client::new();
|
25 |
| - let mut res: HttpResponse<graphql_client::Response<my_query::ResponseData>> = client.post("/graphql").json(&body).send()?; |
26 |
| - println!("{:#?}", res.text()); |
| 44 | + let mut res = client.post("/graphql").json(&request_body).send()?; |
| 45 | + let response_body: GraphQLResponse<my_query::ResponseData> = res.json()?; |
| 46 | + println!("{:#?}", response_body); |
27 | 47 | Ok(())
|
28 | 48 | }
|
29 | 49 | ```
|
30 | 50 |
|
31 |
| -The GraphQL schema language (`.graphql`) and `schema.json` are both supported as sources for the schema. |
32 |
| - |
33 |
| -`serde_derive` needs to be visible in the context of the `GraphQLQuery` derive (add it as an `extern crate`). |
34 |
| - |
35 | 51 | ## Features
|
36 | 52 |
|
37 | 53 | - Strongly typed query variables
|
38 | 54 | - Strongly typed responses
|
39 | 55 | - Works in the browser (WebAssembly)
|
| 56 | +- Supports GraphQL fragments, objects, unions, inputs, enums, custom scalars and input objects |
40 | 57 |
|
41 |
| -Integration with different HTTP libraries is planned, although building one yourself is trivial (just send the constructed request payload as JSON with a POST request to a GraphQL endpoint, modulo authentication). |
| 58 | +### Roadmap |
42 | 59 |
|
43 |
| -There is an embryonic CLI for downloading schemas - the plan is to make it something similar to `apollo-codegen`. |
| 60 | +A lot of desired features have been defined in issues. |
44 | 61 |
|
45 |
| -## What is generated? |
| 62 | +graphql_client does not provide any networking, caching or other client functionality yet. Integration with different HTTP libraries is planned, although building one yourself is trivial (just send the constructed request payload as JSON with a POST request to a GraphQL endpoint, modulo authentication). |
46 | 63 |
|
47 |
| -- A module named after the struct under derive, which contains: |
48 |
| - - A `ResponseData` struct implementing `serde::Deserialize` |
49 |
| - - A `Variables` struct meant to contain the variables expected by the query |
50 |
| -- An impl for the `GraphQLQuery` trait for the struct under derive |
| 64 | +There is an embryonic CLI for downloading schemas - the plan is to make it something similar to `apollo-codegen`. |
51 | 65 |
|
52 |
| -See the [example generated module](https://www.tomhoule.com/docs/example_module/) for more details. |
53 | 66 |
|
54 | 67 | ## Examples
|
55 | 68 |
|
56 |
| -See the examples directory in this project. |
| 69 | +See the examples directory in this repository. |
57 | 70 |
|
58 | 71 | ## Code of conduct
|
59 | 72 |
|
|
0 commit comments