Skip to content

Commit 9ea0396

Browse files
authored
Merge pull request #51 from tomhoule/improve-readme
Improve the "getting started" part of the README
2 parents ce3d251 + fe3b8ab commit 9ea0396

File tree

1 file changed

+59
-42
lines changed

1 file changed

+59
-42
lines changed

README.md

Lines changed: 59 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,74 +4,86 @@
44
[![docs](https://docs.rs/graphql_client/badge.svg)](https://docs.rs/graphql_client/0.0.1/graphql_client/)
55
[![crates.io](https://img.shields.io/crates/v/graphql_client.svg)](https://crates.io/crates/graphql_client)
66

7-
A typed, ergonomic GraphQL client library for Rust.
7+
A typed GraphQL client library for Rust.
8+
9+
## Features
10+
11+
- Precise types for query variables and responses
12+
- Supports GraphQL fragments, objects, unions, inputs, enums, custom scalars and input objects
13+
- Works in the browser (WebAssembly)
814

915
## Getting started
1016

11-
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.
1218

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.
1420

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
18-
#[derive(GraphQLQuery)]
19-
#[graphql(
20-
schema_path = "src/graphql/schema.json",
21-
query_path = "src/graphql/queries/my_query.graphql",
22-
)]
23-
pub struct MyQuery;
24-
```
21+
- In order to provide precise types for a response, graphql_client needs to read the query and the schema at compile-time.
2522

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.
2724

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:
2926

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+
extern crate serde;
29+
#[macro_use]
30+
extern crate serde_derive;
31+
#[macro_use]
32+
extern crate graphql_client;
3133

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
36+
#[derive(GraphQLQuery)]
37+
#[graphql(
38+
schema_path = "src/graphql/schema.json",
39+
query_path = "src/graphql/queries/my_query.graphql",
40+
)]
41+
pub struct MyQuery;
42+
```
3343

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/).
44+
The `derive` will generate a module named `my_query` in this example - the name is the struct's name, but in snake case.
3545

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.
3747

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).
3949

40-
```rust
41-
fn perform_my_query(variables: &my_query::Variables) -> Result<(), failure::Error> {
42-
let request_body = MyQuery::expand(variables);
43-
let client = reqwest::Client::new();
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);
47-
Ok(())
48-
}
49-
```
50+
The module also contains a struct called `Variables` representing the variables expected by the query.
5051

51-
## Features
5252

53-
- Strongly typed query variables
54-
- Strongly typed responses
55-
- Works in the browser (WebAssembly)
56-
- Supports GraphQL fragments, objects, unions, inputs, enums, custom scalars and input objects
53+
- 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:
5754

58-
### Roadmap
55+
```rust
56+
extern crate failure;
57+
#[macro_use]
58+
extern crate graphql_client;
59+
extern crate reqwest;
5960

60-
A lot of desired features have been defined in issues.
61+
fn perform_my_query(variables: &my_query::Variables) -> Result<(), failure::Error> {
6162

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).
63+
// this is the important line
64+
let request_body = MyQuery::expand(variables);
6365

64-
There is an embryonic CLI for downloading schemas - the plan is to make it something similar to `apollo-codegen`.
66+
let client = reqwest::Client::new();
67+
let mut res = client.post("/graphql").json(&request_body).send()?;
68+
let response_body: GraphQLResponse<my_query::ResponseData> = res.json()?;
69+
println!("{:#?}", response_body);
70+
Ok(())
71+
}
72+
```
6573

74+
[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/).
6675

6776
## Examples
6877

6978
See the examples directory in this repository.
7079

71-
## Code of conduct
80+
## Roadmap
7281

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`.
7587

7688
## Contributors
7789

@@ -87,6 +99,11 @@ Many thanks go to all our contributors:
8799
This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification.
88100
Contributions of any kind are welcome!
89101

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).
106+
90107
## License
91108

92109
Licensed under either of these:

0 commit comments

Comments
 (0)