Skip to content

Commit 79d757c

Browse files
authored
Merge pull request #227 from graphql-rust/skeptic
Test the code in the README with skeptic
2 parents e9af9cd + d04898d commit 79d757c

File tree

5 files changed

+49
-15
lines changed

5 files changed

+49
-15
lines changed

README.md

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ A typed GraphQL client library for Rust.
3333

3434
- 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:
3535

36-
```rust
36+
```rust,skt-empty-main
3737
extern crate serde;
3838
#[macro_use]
3939
extern crate serde_derive;
@@ -44,8 +44,8 @@ A typed GraphQL client library for Rust.
4444
// Both json and the GraphQL schema language are supported as sources for the schema
4545
#[derive(GraphQLQuery)]
4646
#[graphql(
47-
schema_path = "src/graphql/schema.json",
48-
query_path = "src/graphql/queries/my_query.graphql",
47+
schema_path = "tests/unions/union_schema.graphql",
48+
query_path = "tests/unions/union_query.graphql",
4949
)]
5050
pub struct MyQuery;
5151
```
@@ -60,15 +60,23 @@ A typed GraphQL client library for Rust.
6060

6161
* 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:
6262

63-
```rust
63+
```rust,skt-empty-main
6464
extern crate failure;
6565
#[macro_use]
6666
extern crate graphql_client;
6767
extern crate reqwest;
6868
6969
use graphql_client::{GraphQLQuery, Response};
7070
71-
fn perform_my_query(variables: &my_query::Variables) -> Result<(), failure::Error> {
71+
#[derive(GraphQLQuery)]
72+
#[graphql(
73+
schema_path = "tests/unions/union_schema.graphql",
74+
query_path = "tests/unions/union_query.graphql",
75+
response_derives = "Debug",
76+
)]
77+
pub struct MyQuery;
78+
79+
fn perform_my_query(variables: my_query::Variables) -> Result<(), failure::Error> {
7280
7381
// this is the important line
7482
let request_body = MyQuery::build_query(variables);
@@ -87,12 +95,15 @@ A typed GraphQL client library for Rust.
8795

8896
The generated response types always derive `serde::Deserialize` but you may want to print them (`Debug`), compare them (`PartialEq`) or derive any other trait on it. You can achieve this with the `response_derives` option of the `graphql` attribute. Example:
8997

90-
```rust
98+
```rust,skt-empty-main
99+
#[macro_use]
100+
extern crate graphql_client;
101+
91102
#[derive(GraphQLQuery)]
92103
#[graphql(
93-
schema_path = "src/search_schema.graphql",
94-
query_path = "src/search_query.graphql"
95-
response_derives = "Serialize,PartialEq",
104+
schema_path = "tests/unions/union_schema.graphql",
105+
query_path = "tests/unions/union_query.graphql",
106+
response_derives = "Serialize,PartialEq",
96107
)]
97108
struct SearchQuery;
98109
```
@@ -106,12 +117,15 @@ The generated code will reference the scalar types as defined in the server sche
106117
The generated code has support for [`@deprecated`](http://facebook.github.io/graphql/June2018/#sec-Field-Deprecation)
107118
field annotations. You can configure how deprecations are handled via the `deprecated` argument in the `GraphQLQuery` derive:
108119

109-
```rust
120+
```rust,skt-empty-main
121+
#[macro_use]
122+
extern crate graphql_client;
123+
110124
#[derive(GraphQLQuery)]
111125
#[graphql(
112-
schema_path = "src/graphql/schema.json",
113-
query_path = "src/graphql/queries/my_query.graphql",
114-
deprecated = "warn"
126+
schema_path = "tests/unions/union_schema.graphql",
127+
query_path = "tests/unions/union_query.graphql",
128+
deprecated = "warn"
115129
)]
116130
pub struct MyQuery;
117131
```
@@ -133,8 +147,8 @@ If you want to name the struct different from query name, you can use ``selected
133147

134148
#[derive(GraphQLQuery)]
135149
#[graphql(
136-
schema_path = "src/graphql/schema.json",
137-
query_path = "src/graphql/queries/my_query.graphql",
150+
schema_path = "tests/unions/union_schema.graphql",
151+
query_path = "tests/unions/union_query.graphql",
138152
selected_operation = "SearchQuery"
139153
)]
140154
pub struct MyQuery;

README.md.skt.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```rust,skt-empty-main
2+
#![allow(dead_code)]
3+
4+
{}
5+
6+
fn main() {{ }}
7+
```

graphql_client/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,9 @@ itertools = "0.7"
1515
serde = "^1.0.78"
1616
serde_derive = "1.0"
1717
serde_json = "1.0"
18+
19+
[build-dependencies]
20+
skeptic = "0.13"
21+
22+
[dev-dependencies]
23+
skeptic = "0.13"

graphql_client/build.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
extern crate skeptic;
2+
3+
fn main() {
4+
// Generates doc tests for the readme.
5+
skeptic::generate_doc_tests(&["../README.md"]);
6+
}

graphql_client/tests/skeptic.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include!(concat!(env!("OUT_DIR"), "/skeptic-tests.rs"));

0 commit comments

Comments
 (0)