|
1 | | -NOTE: This is a WIP - I plan to write proper documentation and make a formal release soon. |
| 1 | +# graphql_client |
2 | 2 |
|
3 | | -Derive Rust code to safely interact with queries written in the GraphQL query language. |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | + |
4 | 8 |
|
5 | | -This library does not provide any networking, caching or other client functionality, it is just meant to make it easy to interact with a GraphQL query and the corresponding response in a strongly typed way. Building a client can be as simple as this: |
| 9 | +Derive Rust code to safely and ergonomically manipulate GraphQL queries. |
| 10 | + |
| 11 | +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: |
6 | 12 |
|
7 | 13 | ```rust |
8 | 14 | #[derive(GraphQLQuery)] |
9 | 15 | #[gql( |
10 | | - query = "/graphql/queries/my_query.graphql", |
11 | | - schema = "/graphql/schema.graphql" |
| 16 | + query_path = "src/graphql/queries/my_query.graphql", |
| 17 | + schema_path = "src/graphql/schema.json" |
12 | 18 | )] |
13 | 19 | struct MyQuery; |
14 | 20 |
|
15 | 21 | fn perform_my_query(variables: &my_query::Variables) -> Result<(), failure::Error> { |
16 | 22 | let body = MyQuery::expand(variables); |
17 | 23 | let client = reqwest::Client::new(); |
18 | | - let res: HttpResponse<graphql_client::Response<my_query::ResponseData>> = client.post("/graphql", body)?; |
19 | | - println!("{:#?}", res.body); |
| 24 | + let mut res: HttpResponse<graphql_client::Response<my_query::ResponseData>> = client.post("/graphql").json(&body).send()?; |
| 25 | + println!("{:#?}", res.text()); |
| 26 | + Ok(()) |
20 | 27 | } |
21 | 28 | ``` |
22 | 29 |
|
| 30 | +The paths in the `gql` attribute are relative to the directory where your `Cargo.toml` is located. |
| 31 | + |
| 32 | +The GraphQL schema language and schema.json are both supported as sources for the schema. |
| 33 | + |
| 34 | +`serde_derive` needs to be visible in the context of the `GraphQLQuery` derive (add it as an `extern crate`). |
| 35 | + |
23 | 36 | ## Features |
24 | 37 |
|
25 | | -* Strongly typed query variables |
26 | | -* Strongly typed response |
| 38 | +- Strongly typed query variables |
| 39 | +- Strongly typed responses |
| 40 | +- Works in the browser (WebAssembly) |
| 41 | + |
| 42 | +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). |
27 | 43 |
|
28 | | -### Planned features |
| 44 | +There is an embryonic CLI for downloading schemas - the plan is to make it something similar to `apollo-codegen`. |
29 | 45 |
|
30 | | -* Strongly typed subscriptions |
31 | | -* Query string minification (e.g. for embedding in a browser wasm app, and for minimizing payload size) |
32 | | -* A command line interface in addition to the custom derive for generating code and downloading schemas |
33 | 46 |
|
34 | | -## What is generated? |
| 47 | +## What is generated? |
35 | 48 |
|
36 | | -* A module named after the struct under derive, which contains: |
37 | | - * A `ResponseData` struct implementing `serde::Deserialize` |
38 | | - * A `Variables` struct meant to contain the variables expected by the query |
39 | | -* An impl for the `GraphQLQuery` trait for the struct under derive |
| 49 | +- A module named after the struct under derive, which contains: |
| 50 | + - A `ResponseData` struct implementing `serde::Deserialize` |
| 51 | + - A `Variables` struct meant to contain the variables expected by the query |
| 52 | +- An impl for the `GraphQLQuery` trait for the struct under derive |
40 | 53 |
|
41 | | -See the example generated module for a full example. |
| 54 | +See the [example generated module](https://www.tomhoule.com/docs/example_module/) for more details. |
42 | 55 |
|
43 | 56 | ## Examples |
44 | 57 |
|
|
0 commit comments