|
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 | +[](https://travis-ci.org/tomhoule/graphql-client) |
| 4 | +[](https://docs.rs/graphql_client/0.0.1/graphql_client/) |
| 5 | +[](https://crates.io/crates/graphql_client) |
4 | 6 |
|
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: |
| 7 | +Derive Rust code to safely and ergonomically manipulate GraphQL queries. |
| 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: |
6 | 10 |
|
7 | 11 | ```rust
|
8 | 12 | #[derive(GraphQLQuery)]
|
9 |
| -#[gql( |
10 |
| - query = "/graphql/queries/my_query.graphql", |
11 |
| - schema = "/graphql/schema.graphql" |
| 13 | +#[graphql( |
| 14 | + // The paths are relative to the directory where your `Cargo.toml` is located. |
| 15 | + query_path = "src/graphql/queries/my_query.graphql", |
| 16 | + schema_path = "src/graphql/schema.json" |
12 | 17 | )]
|
13 | 18 | struct MyQuery;
|
| 19 | +``` |
14 | 20 |
|
| 21 | +```rust |
15 | 22 | fn perform_my_query(variables: &my_query::Variables) -> Result<(), failure::Error> {
|
16 | 23 | let body = MyQuery::expand(variables);
|
17 | 24 | let client = reqwest::Client::new();
|
18 |
| - let res: HttpResponse<graphql_client::Response<my_query::ResponseData>> = client.post("/graphql", body)?; |
19 |
| - println!("{:#?}", res.body); |
| 25 | + let mut res: HttpResponse<graphql_client::Response<my_query::ResponseData>> = client.post("/graphql").json(&body).send()?; |
| 26 | + println!("{:#?}", res.text()); |
| 27 | + Ok(()) |
20 | 28 | }
|
21 | 29 | ```
|
22 | 30 |
|
| 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 | + |
23 | 35 | ## Features
|
24 | 36 |
|
25 |
| -* Strongly typed query variables |
26 |
| -* Strongly typed response |
| 37 | +- Strongly typed query variables |
| 38 | +- Strongly typed responses |
| 39 | +- Works in the browser (WebAssembly) |
| 40 | + |
| 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). |
27 | 42 |
|
28 |
| -### Planned features |
| 43 | +There is an embryonic CLI for downloading schemas - the plan is to make it something similar to `apollo-codegen`. |
29 | 44 |
|
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 | 45 |
|
34 |
| -## What is generated? |
| 46 | +## What is generated? |
35 | 47 |
|
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 |
| 48 | +- A module named after the struct under derive, which contains: |
| 49 | + - A `ResponseData` struct implementing `serde::Deserialize` |
| 50 | + - A `Variables` struct meant to contain the variables expected by the query |
| 51 | +- An impl for the `GraphQLQuery` trait for the struct under derive |
40 | 52 |
|
41 |
| -See the example generated module for a full example. |
| 53 | +See the [example generated module](https://www.tomhoule.com/docs/example_module/) for more details. |
42 | 54 |
|
43 | 55 | ## Examples
|
44 | 56 |
|
45 | 57 | See the examples directory in this project.
|
| 58 | + |
| 59 | +## Code of conduct |
| 60 | + |
| 61 | +Anyone who interacts with this project in any space, including but not limited to |
| 62 | +this GitHub repository, must follow our [code of conduct](https://github.com/tomhoule/graphql-client/blob/master/CODE_OF_CONDUCT.md). |
| 63 | + |
| 64 | +## License |
| 65 | + |
| 66 | +Licensed under either of these: |
| 67 | + |
| 68 | + * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or |
| 69 | + https://www.apache.org/licenses/LICENSE-2.0) |
| 70 | + * MIT license ([LICENSE-MIT](LICENSE-MIT) or |
| 71 | + https://opensource.org/licenses/MIT) |
| 72 | + |
| 73 | +### Contributing |
| 74 | + |
| 75 | +Unless you explicitly state otherwise, any contribution you intentionally submit |
| 76 | +for inclusion in the work, as defined in the Apache-2.0 license, shall be |
| 77 | +dual-licensed as above, without any additional terms or conditions. |
0 commit comments