Skip to content

Commit 2691596

Browse files
committed
Reorganize the README
1 parent 75b5831 commit 2691596

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
lines changed

README.md

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,69 @@
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-
Derive Rust code to safely and ergonomically manipulate GraphQL queries.
7+
A typed, ergonomic GraphQL client library for Rust.
88

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

1115
```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
1218
#[derive(GraphQLQuery)]
1319
#[graphql(
14-
// The paths are relative to the directory where your `Cargo.toml` is located.
20+
schema_path = "src/graphql/schema.json",
1521
query_path = "src/graphql/queries/my_query.graphql",
16-
schema_path = "src/graphql/schema.json"
1722
)]
18-
struct MyQuery;
23+
pub struct MyQuery;
1924
```
2025

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+
2140
```rust
2241
fn perform_my_query(variables: &my_query::Variables) -> Result<(), failure::Error> {
23-
let body = MyQuery::expand(variables);
42+
let request_body = MyQuery::expand(variables);
2443
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);
2747
Ok(())
2848
}
2949
```
3050

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-
3551
## Features
3652

3753
- Strongly typed query variables
3854
- Strongly typed responses
3955
- Works in the browser (WebAssembly)
56+
- Supports GraphQL fragments, objects, unions, inputs, enums, custom scalars and input objects
4057

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
4259

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

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

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

52-
See the [example generated module](https://www.tomhoule.com/docs/example_module/) for more details.
5366

5467
## Examples
5568

56-
See the examples directory in this project.
69+
See the examples directory in this repository.
5770

5871
## Code of conduct
5972

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
///! The top-level documentation resides on the [project README](https://github.com/tomhoule/graphql-client) at the moment.
12
extern crate serde;
23
#[macro_use]
34
extern crate serde_derive;
@@ -7,6 +8,7 @@ extern crate graphql_query_derive;
78
#[doc(hidden)]
89
pub use graphql_query_derive::*;
910

11+
/// A convenience trait that can be used to build a GraphQL request body.
1012
pub trait GraphQLQuery<'de> {
1113
type Variables: serde::Serialize;
1214
type ResponseData: serde::Deserialize<'de>;
@@ -29,6 +31,9 @@ pub struct GraphQLError {
2931
pub path: String,
3032
}
3133

34+
/// The generic shape taken by the responses of GraphQL APIs.
35+
///
36+
/// This will generally be used with the `ResponseData` struct from a derived module.
3237
#[derive(Debug, Serialize, Deserialize)]
3338
pub struct GraphQLResponse<Data> {
3439
pub data: Option<Data>,

0 commit comments

Comments
 (0)