Skip to content

Commit 1b0630e

Browse files
authored
Merge pull request #1 from tomhoule/derive-from-json
Implement json introspected schema support
2 parents bd72a04 + d92681d commit 1b0630e

20 files changed

+49733
-139
lines changed

.travis.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
language: rust
2+
rust:
3+
- stable
4+
- beta
5+
- nightly
6+
cache: cargo
7+
script:
8+
- cargo test --verbose
9+
- cd graphql_query_derive && cargo test --verbose
10+
- cd ../examples/github && cargo build

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
2-
name = "graphql_query"
3-
version = "0.1.0"
2+
name = "graphql_client"
3+
version = "0.0.1"
44
authors = ["Tom Houlé <[email protected]>"]
55

66
[dependencies]

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct MyQuery;
1313
fn perform_my_query(variables: &my_query::Variables) -> Result<(), failure::Error> {
1414
let body = MyQuery::expand(variables);
1515
let client = reqwest::Client::new();
16-
let res: HttpResponse<graphql_query::Response<my_query::ResponseData>> = client.post("/graphql", body)?;
16+
let res: HttpResponse<graphql_client::Response<my_query::ResponseData>> = client.post("/graphql", body)?;
1717
println!("{:#?}", res.body);
1818
}
1919
```

examples/github/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ authors = ["Tom Houlé <[email protected]>"]
55

66
[dependencies]
77
failure = "*"
8-
graphql_query = { path = "../.." }
8+
graphql_client = { path = "../.." }
99
serde = "1.0"
1010
serde_derive = "1.0"
1111
serde_json = "1.0"

examples/github/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
extern crate failure;
22
#[macro_use]
3-
extern crate graphql_query;
3+
extern crate graphql_client;
44
extern crate reqwest;
55
extern crate serde;
66
extern crate serde_json;
77
#[macro_use]
88
extern crate serde_derive;
99

10-
use graphql_query::*;
10+
use graphql_client::*;
1111

1212
#[derive(GraphQLQuery)]
1313
#[GraphQLQuery(schema_path = "src/schema.graphql", query_path = "src/query_1.graphql")]

graphql_query_derive/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ syn = "*"
1313
proc-macro2 = { version = "*", features = ["nightly"] }
1414
serde = "1.0"
1515
serde_derive = "1.0"
16+
serde_json = "1.0"
1617
heck = "*"
1718
graphql-parser = "*"

graphql_query_derive/src/field_type.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use enums::ENUMS_PREFIX;
22
use graphql_parser::schema;
3+
use introspection_response;
34
use proc_macro2::{Ident, Span, TokenStream};
45
use query::QueryContext;
56
use schema::DEFAULT_SCALARS;
@@ -87,3 +88,34 @@ fn from_schema_type_inner(inner: schema::Type, non_null: bool) -> FieldType {
8788
FieldType::Optional(Box::new(inner_field_type))
8889
}
8990
}
91+
92+
fn from_json_type_inner(inner: &introspection_response::TypeRef, non_null: bool) -> FieldType {
93+
use introspection_response::*;
94+
let inner = inner.clone();
95+
96+
let inner_field_type = match inner.kind {
97+
Some(__TypeKind::NON_NULL) => {
98+
from_json_type_inner(&inner.of_type.expect("inner type is missing"), true)
99+
}
100+
Some(__TypeKind::LIST) => {
101+
FieldType::Vector(Box::new(from_json_type_inner(&inner.of_type.expect("inner type is missing"), false)))
102+
}
103+
Some(_) => FieldType::Named(Ident::new(
104+
&inner.name.expect("type name"),
105+
Span::call_site(),
106+
)),
107+
None => unreachable!("non-convertible type"),
108+
};
109+
110+
if non_null {
111+
inner_field_type
112+
} else {
113+
FieldType::Optional(Box::new(inner_field_type))
114+
}
115+
}
116+
117+
impl ::std::convert::From<introspection_response::FullTypeFieldsType> for FieldType {
118+
fn from(schema_type: introspection_response::FullTypeFieldsType) -> FieldType {
119+
from_json_type_inner(&schema_type.type_ref, false)
120+
}
121+
}

graphql_query_derive/src/interfaces.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use objects::GqlObjectField;
33
use proc_macro2::{Ident, Span, TokenStream};
44
use query::QueryContext;
55

6-
#[derive(Debug)]
6+
#[derive(Debug, PartialEq)]
77
pub struct GqlInterface {
88
pub implemented_by: Vec<String>,
99
pub name: String,
@@ -14,7 +14,7 @@ impl GqlInterface {
1414
pub fn response_for_selection(
1515
&self,
1616
_query_context: &QueryContext,
17-
selection: &query::SelectionSet,
17+
_selection: &query::SelectionSet,
1818
prefix: &str,
1919
) -> TokenStream {
2020
let name = Ident::new(&prefix, Span::call_site());

0 commit comments

Comments
 (0)