You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In order to provide precise types for a response, graphql_client needs to read the query and the schema at compile-time.
17
+
- If you are not familiar with GraphQL, the [official website](https://graphql.org/) provides a very good and comprehensive introduction.
12
18
13
-
This is achieved through a procedural macro, as in the following snippet:
19
+
- Once you have written your query (most likely in something like [graphiql](https://github.com/graphql/graphiql)), save it in a `.graphql` file in your project.
14
20
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
- In order to provide precise types for a response, graphql_client needs to read the query and the schema at compile-time.
25
22
26
-
The `derive` will generate a module named `my_query` in this example - the name is the struct's name, but in snake case.
23
+
To download the schema, you have multiple options. This projects provides a [CLI](https://github.com/tomhoule/graphql-client/tree/master/graphql_client_cli), but there are also more mature tools like [apollo-cli](https://github.com/apollographql/apollo-cli). It does not matter which one you use, the resulting `schema.json` is the same.
27
24
28
-
That module contains all the struct and enum definitions necessary to deserialize a response to that query.
25
+
- 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:
29
26
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).
27
+
```rust
28
+
externcrate serde;
29
+
#[macro_use]
30
+
externcrate serde_derive;
31
+
#[macro_use]
32
+
externcrate graphql_client;
31
33
32
-
The module also contains a struct called `Variables` representing the variables expected by the query.
34
+
// The paths are relative to the directory where your `Cargo.toml` is located.
35
+
// Both json and the GraphQL schema language are supported as sources for the schema
[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/).
44
+
The `derive` will generate a module named `my_query` in this example - the name is the struct's name, but in snake case.
35
45
36
-
NOTE: `serde` and `serde_derive` need to be imported in the current crate with `extern crate`.
46
+
That module contains all the struct and enum definitions necessary to deserialize a response to that query.
37
47
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:
48
+
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).
- We now need to create the complete payload that we are going to send to the server. For convenience, the [GraphQLQuery trait](https://docs.rs/graphql_client/latest/graphql_client/trait.GraphQLQuery.html), is implemented for the struct under derive, so a complete query body can be created this way:
57
54
58
-
### Roadmap
55
+
```rust
56
+
externcrate failure;
57
+
#[macro_use]
58
+
externcrate graphql_client;
59
+
externcrate reqwest;
59
60
60
-
A lot of desired features have been defined in issues.
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).
63
+
// this is the important line
64
+
letrequest_body=MyQuery::expand(variables);
63
65
64
-
There is an embryonic CLI for downloading schemas - the plan is to make it something similar to `apollo-codegen`.
[A complete example using the GitHub GraphQL API is available](https://github.com/tomhoule/graphql-client/tree/master/examples/github), as well as sample [rustdoc output](https://www.tomhoule.com/docs/example_module/).
66
75
67
76
## Examples
68
77
69
78
See the examples directory in this repository.
70
79
71
-
## Code of conduct
80
+
## Roadmap
72
81
73
-
Anyone who interacts with this project in any space, including but not limited to
74
-
this GitHub repository, must follow our [code of conduct](https://github.com/tomhoule/graphql-client/blob/master/CODE_OF_CONDUCT.md).
82
+
A lot of desired features have been defined in issues.
83
+
84
+
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).
85
+
86
+
There is an embryonic CLI for downloading schemas - the plan is to make it something similar to `apollo-codegen`.
75
87
76
88
## Contributors
77
89
@@ -87,6 +99,11 @@ Many thanks go to all our contributors:
87
99
This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification.
88
100
Contributions of any kind are welcome!
89
101
102
+
## Code of conduct
103
+
104
+
Anyone who interacts with this project in any space, including but not limited to
105
+
this GitHub repository, must follow our [code of conduct](https://github.com/tomhoule/graphql-client/blob/master/CODE_OF_CONDUCT.md).
0 commit comments