Skip to content

Commit 64fc31b

Browse files
committed
Update the main README regarding the struct naming change
TODO: - update the README for graphql-cli - update the changelog
1 parent cae30db commit 64fc31b

File tree

4 files changed

+19
-12
lines changed

4 files changed

+19
-12
lines changed

README.md

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

3030
- In order to provide precise types for a response, graphql_client needs to read the query and the schema at compile-time.
3131

32-
To download the schema, you have multiple options. This projects provides a [CLI](https://github.com/graphql-rust/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.
32+
To download the schema, you have multiple options. This projects provides a [CLI](https://github.com/graphql-rust/graphql-client/tree/master/graphql_client_cli), however it does not matter what tool you use, the resulting `schema.json` is the same.
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

@@ -47,10 +47,10 @@ A typed GraphQL client library for Rust.
4747
schema_path = "tests/unions/union_schema.graphql",
4848
query_path = "tests/unions/union_query.graphql",
4949
)]
50-
pub struct MyQuery;
50+
pub struct UnionQuery;
5151
```
5252

53-
The `derive` will generate a module named `my_query` in this example - the name is the struct's name, but in snake case.
53+
The `derive` will generate a module named `union_query` in this example - the name is the struct's name, but in snake case.
5454

5555
That module contains all the struct and enum definitions necessary to deserialize a response to that query.
5656

@@ -74,16 +74,16 @@ A typed GraphQL client library for Rust.
7474
query_path = "tests/unions/union_query.graphql",
7575
response_derives = "Debug",
7676
)]
77-
pub struct MyQuery;
77+
pub struct UnionQuery;
7878
79-
fn perform_my_query(variables: my_query::Variables) -> Result<(), failure::Error> {
79+
fn perform_my_query(variables: union_query::Variables) -> Result<(), failure::Error> {
8080
8181
// this is the important line
82-
let request_body = MyQuery::build_query(variables);
82+
let request_body = UnionQuery::build_query(variables);
8383
8484
let client = reqwest::Client::new();
8585
let mut res = client.post("/graphql").json(&request_body).send()?;
86-
let response_body: Response<my_query::ResponseData> = res.json()?;
86+
let response_body: Response<union_query::ResponseData> = res.json()?;
8787
println!("{:#?}", response_body);
8888
Ok(())
8989
}
@@ -105,7 +105,7 @@ extern crate graphql_client;
105105
query_path = "tests/unions/union_query.graphql",
106106
response_derives = "Serialize,PartialEq",
107107
)]
108-
struct SearchQuery;
108+
struct UnionQuery;
109109
```
110110

111111
## Custom scalars
@@ -127,7 +127,7 @@ extern crate graphql_client;
127127
query_path = "tests/unions/union_query.graphql",
128128
deprecated = "warn"
129129
)]
130-
pub struct MyQuery;
130+
pub struct UnionQuery;
131131
```
132132

133133
Valid values are:
@@ -143,15 +143,20 @@ The default is `warn`.
143143

144144
You can write multiple operations in one query document (one `.graphql` file). You can then select one by naming the struct you `#[derive(GraphQLQuery)]` on with the same name as one of the operations. This is neat, as it allows sharing fragments between operations.
145145

146-
If you want to name the struct different from query name, you can use ``selected_operation`` argument like this.
146+
Note that the struct and the operation in the GraphQL file *must* have the same name. We enforce this to make the generated code more predictable.
147+
148+
```rust,skt-empty-main
149+
#[macro_use]
150+
extern crate graphql_client;
147151
148152
#[derive(GraphQLQuery)]
149153
#[graphql(
150154
schema_path = "tests/unions/union_schema.graphql",
151155
query_path = "tests/unions/union_query.graphql",
152156
selected_operation = "SearchQuery"
153157
)]
154-
pub struct MyQuery;
158+
pub struct UnionQuery;
159+
```
155160

156161
There is an example [in the tests](./tests/operation_selection).
157162

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod interface_not_on_everything_query { # ! [ allow ( non_camel_case_types ) ] # ! [ allow ( non_snake_case ) ] # ! [ allow ( dead_code ) ] use serde ; pub const OPERATION_NAME : & 'static str = "InterfaceNotOnEverythingQuery" ; pub const QUERY : & 'static str = "query InterfaceNotOnEverythingQuery {\n everything {\n __typename\n name\n ... on Person {\n birthday\n }\n ... on Organization {\n industry\n }\n }\n}\n" ; use serde_derive :: * ; # [ allow ( dead_code ) ] type Boolean = bool ; # [ allow ( dead_code ) ] type Float = f64 ; # [ allow ( dead_code ) ] type Int = i64 ; # [ allow ( dead_code ) ] type ID = String ; # [ derive ( Eq , PartialEq ) ] pub enum Industry { CAT_FOOD , CHOCOLATE , OTHER , Other ( String ) , } impl :: serde :: Serialize for Industry { fn serialize < S : serde :: Serializer > ( & self , ser : S ) -> Result < S :: Ok , S :: Error > { ser . serialize_str ( match * self { Industry :: CAT_FOOD => "CAT_FOOD" , Industry :: CHOCOLATE => "CHOCOLATE" , Industry :: OTHER => "OTHER" , Industry :: Other ( ref s ) => & s , } ) } } impl < 'de > :: serde :: Deserialize < 'de > for Industry { fn deserialize < D : :: serde :: Deserializer < 'de >> ( deserializer : D ) -> Result < Self , D :: Error > { let s = < String > :: deserialize ( deserializer ) ? ; match s . as_str ( ) { "CAT_FOOD" => Ok ( Industry :: CAT_FOOD ) , "CHOCOLATE" => Ok ( Industry :: CHOCOLATE ) , "OTHER" => Ok ( Industry :: OTHER ) , _ => Ok ( Industry :: Other ( s ) ) , } } } # [ derive ( Deserialize ) ] pub struct InterfaceNotOnEverythingQueryEverythingOnOrganization { pub industry : Industry , } # [ derive ( Deserialize ) ] pub struct InterfaceNotOnEverythingQueryEverythingOnPerson { pub birthday : Option < String > , } # [ derive ( Deserialize ) ] # [ serde ( tag = "__typename" ) ] pub enum InterfaceNotOnEverythingQueryEverythingOn { Organization ( InterfaceNotOnEverythingQueryEverythingOnOrganization ) , Person ( InterfaceNotOnEverythingQueryEverythingOnPerson ) , Dog , } # [ derive ( Deserialize ) ] pub struct InterfaceNotOnEverythingQueryEverything { pub name : String , # [ serde ( flatten ) ] pub on : InterfaceNotOnEverythingQueryEverythingOn , } # [ derive ( Serialize ) ] pub struct Variables ; # [ derive ( Deserialize ) ] pub struct ResponseData { pub everything : Option < Vec < InterfaceNotOnEverythingQueryEverything > > , } impl :: graphql_client :: GraphQLQuery for super :: InterfaceNotOnEverythingQuery { type Variables = Variables ; type ResponseData = ResponseData ; fn build_query ( variables : Self :: Variables ) -> :: graphql_client :: QueryBody < Self :: Variables > { :: graphql_client :: QueryBody { variables , query : QUERY , operation_name : OPERATION_NAME , } } } }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod union_query { # ! [ allow ( non_camel_case_types ) ] # ! [ allow ( non_snake_case ) ] # ! [ allow ( dead_code ) ] use serde ; pub const OPERATION_NAME : & 'static str = "UnionQuery" ; pub const QUERY : & 'static str = "query UnionQuery {\n names {\n __typename\n ... on Dog {\n name\n }\n ... on Person {\n firstName\n lastName\n }\n ... on Organization {\n title\n }\n }\n}\n\nfragment NamesFragment on NamedThing {\n __typename\n ... on Dog {\n name\n }\n ... on Person {\n firstName\n }\n ... on Organization {\n title\n }\n}\n\nquery FragmentOnUnion {\n names {\n ...NamesFragment\n }\n}\n" ; use serde_derive :: * ; # [ allow ( dead_code ) ] type Boolean = bool ; # [ allow ( dead_code ) ] type Float = f64 ; # [ allow ( dead_code ) ] type Int = i64 ; # [ allow ( dead_code ) ] type ID = String ; # [ derive ( Deserialize ) ] pub struct UnionQueryNamesOnDog { pub name : String , } # [ derive ( Deserialize ) ] pub struct UnionQueryNamesOnOrganization { pub title : String , } # [ derive ( Deserialize ) ] pub struct UnionQueryNamesOnPerson { # [ serde ( rename = "firstName" ) ] pub first_name : String , # [ serde ( rename = "lastName" ) ] pub last_name : Option < String > , } # [ derive ( Deserialize ) ] # [ serde ( tag = "__typename" ) ] pub enum UnionQueryNames { Dog ( UnionQueryNamesOnDog ) , Organization ( UnionQueryNamesOnOrganization ) , Person ( UnionQueryNamesOnPerson ) } # [ derive ( Serialize ) ] pub struct Variables ; # [ derive ( Deserialize ) ] pub struct ResponseData { pub names : Option < Vec < UnionQueryNames > > , } impl :: graphql_client :: GraphQLQuery for super :: UnionQuery { type Variables = Variables ; type ResponseData = ResponseData ; fn build_query ( variables : Self :: Variables ) -> :: graphql_client :: QueryBody < Self :: Variables > { :: graphql_client :: QueryBody { variables , query : QUERY , operation_name : OPERATION_NAME , } } } } pub mod fragment_on_union { # ! [ allow ( non_camel_case_types ) ] # ! [ allow ( non_snake_case ) ] # ! [ allow ( dead_code ) ] use serde ; pub const OPERATION_NAME : & 'static str = "FragmentOnUnion" ; pub const QUERY : & 'static str = "query UnionQuery {\n names {\n __typename\n ... on Dog {\n name\n }\n ... on Person {\n firstName\n lastName\n }\n ... on Organization {\n title\n }\n }\n}\n\nfragment NamesFragment on NamedThing {\n __typename\n ... on Dog {\n name\n }\n ... on Person {\n firstName\n }\n ... on Organization {\n title\n }\n}\n\nquery FragmentOnUnion {\n names {\n ...NamesFragment\n }\n}\n" ; use serde_derive :: * ; # [ allow ( dead_code ) ] type Boolean = bool ; # [ allow ( dead_code ) ] type Float = f64 ; # [ allow ( dead_code ) ] type Int = i64 ; # [ allow ( dead_code ) ] type ID = String ; # [ derive ( Deserialize ) ] pub struct FragmentOnUnionNamesOnDog { pub name : String , } # [ derive ( Deserialize ) ] pub struct FragmentOnUnionNamesOnOrganization { pub title : String , } # [ derive ( Deserialize ) ] pub struct FragmentOnUnionNamesOnPerson { # [ serde ( rename = "firstName" ) ] pub first_name : String , } # [ derive ( Deserialize ) ] # [ serde ( tag = "__typename" ) ] pub enum FragmentOnUnionNames { Dog ( FragmentOnUnionNamesOnDog ) , Organization ( FragmentOnUnionNamesOnOrganization ) , Person ( FragmentOnUnionNamesOnPerson ) } # [ derive ( Serialize ) ] pub struct Variables ; # [ derive ( Deserialize ) ] pub struct ResponseData { pub names : Option < Vec < FragmentOnUnionNames > > , } impl :: graphql_client :: GraphQLQuery for super :: FragmentOnUnion { type Variables = Variables ; type ResponseData = ResponseData ; fn build_query ( variables : Self :: Variables ) -> :: graphql_client :: QueryBody < Self :: Variables > { :: graphql_client :: QueryBody { variables , query : QUERY , operation_name : OPERATION_NAME , } } } }

graphql_client_cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "graphql_client_cli"
3-
description = "The CLI for graphql-client (WIP)"
3+
description = "The CLI for graphql-client"
44
version = "0.6.1"
55
authors = ["Tom Houlé <[email protected]>"]
66
license = "Apache-2.0 OR MIT"

0 commit comments

Comments
 (0)