Skip to content

Commit 87cb608

Browse files
committed
WIP - input objects
1 parent 156dc93 commit 87cb608

File tree

4 files changed

+87
-3
lines changed

4 files changed

+87
-3
lines changed

graphql_query_derive/src/constants.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ pub const TYPENAME_FIELD: &'static str = "__typename";
55
pub fn string_type() -> Ident {
66
Ident::new("String", Span::call_site())
77
}
8+
9+
pub fn float_type() -> Ident {
10+
Ident::new("Float", Span::call_site())
11+
}

graphql_query_derive/src/inputs.rs

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,78 @@
1+
use query::QueryContext;
2+
use objects::GqlObjectField;
3+
use proc_macro2::{Ident, Span, TokenStream};
4+
use introspection_response;
5+
use graphql_parser;
6+
17
#[derive(Debug, PartialEq)]
2-
pub struct GqlInput;
8+
pub struct GqlInput {
9+
pub name: String,
10+
pub fields: Vec<GqlObjectField>,
11+
}
12+
13+
impl GqlInput {
14+
pub fn to_rust(&self, context: &QueryContext) -> TokenStream {
15+
let name = Ident::new(&self.name, Span::call_site());
16+
let fields = self.fields.iter().map(|field| {
17+
let ty = field.type_.to_rust(&context, "");
18+
let name = Ident::new(&field.name, Span::call_site());
19+
quote!(#ty: #name)
20+
});
21+
22+
quote!{
23+
#[derive(Debug, Serialize)]
24+
#[serde(rename_all = "camelCase")]
25+
pub struct #name {
26+
#(#fields,)*
27+
}
28+
}
29+
}
30+
}
31+
32+
impl ::std::convert::From<graphql_parser::schema::InputObjectType> for GqlInput {
33+
fn from(schema_input: graphql_parser::schema::InputObjectType) -> GqlInput {
34+
unimplemented!();
35+
}
36+
}
37+
38+
impl ::std::convert::From<introspection_response::FullType> for GqlInput {
39+
fn from(schema_input: introspection_response::FullType) -> GqlInput {
40+
unimplemented!();
41+
}
42+
}
43+
44+
#[cfg(test)]
45+
mod tests {
46+
use super::*;
47+
use constants::*;
48+
use field_type::FieldType;
49+
50+
#[test]
51+
fn gql_input_to_rust() {
52+
let input = GqlInput {
53+
name: "Cat".to_string(),
54+
fields: vec![
55+
GqlObjectField {
56+
name: "pawsCount".to_string(),
57+
type_: FieldType::Named(float_type())
58+
},
59+
GqlObjectField {
60+
name: "offsprings".to_string(),
61+
type_: FieldType::Vector(Box::new(FieldType::Named(Ident::new("Cat", Span::call_site())))),
62+
},
63+
GqlObjectField {
64+
name: "requirements".to_string(),
65+
type_: FieldType::Optional(Box::new(FieldType::Named(Ident::new("CatRequirements", Span::call_site())))),
66+
},
67+
],
68+
};
69+
70+
71+
let expected: String = vec![
72+
"",
73+
"",
74+
].into_iter().collect();
75+
76+
assert_eq!(format!("{:?}", input.to_rust()), expected);
77+
}
78+
}

graphql_query_derive/src/query.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use selection::Selection;
77
use std::collections::BTreeMap;
88
use variables::Variable;
99

10+
/// This holds all the information we need during the code generation phase.
1011
pub struct QueryContext {
1112
pub _subscription_root: Option<Vec<TokenStream>>,
1213
pub fragments: BTreeMap<String, GqlFragment>,
@@ -17,6 +18,7 @@ pub struct QueryContext {
1718
}
1819

1920
impl QueryContext {
21+
/// Create a QueryContext with the given Schema.
2022
pub fn new(schema: Schema) -> QueryContext {
2123
QueryContext {
2224
_subscription_root: None,
@@ -28,12 +30,14 @@ impl QueryContext {
2830
}
2931
}
3032

33+
/// Ingest the variable definitions for the query we are generating code for.
3134
pub fn register_variables(&mut self, variables: &[graphql_parser::query::VariableDefinition]) {
3235
variables.iter().for_each(|variable| {
3336
self.variables.push(variable.clone().into());
3437
});
3538
}
3639

40+
/// Generate the Variables struct and all the necessary supporting code.
3741
pub fn expand_variables(&self) -> TokenStream {
3842
if self.variables.is_empty() {
3943
return quote!(#[derive(Serialize)]

graphql_query_derive/src/schema.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ impl ::std::convert::From<graphql_parser::schema::Document> for Schema {
235235
);
236236
}
237237
schema::TypeDefinition::InputObject(input) => {
238-
schema.inputs.insert(input.name, GqlInput);
238+
schema.inputs.insert(input.name.clone(), GqlInput::from(input));
239239
}
240240
},
241241
schema::Definition::DirectiveDefinition(_) => (),
@@ -342,7 +342,7 @@ impl ::std::convert::From<::introspection_response::IntrospectionResponse> for S
342342
schema.interfaces.insert(name, iface);
343343
}
344344
Some(__TypeKind::INPUT_OBJECT) => {
345-
schema.inputs.insert(name, GqlInput);
345+
schema.inputs.insert(name, GqlInput::from(ty.clone()));
346346
}
347347
_ => unimplemented!("unimplemented definition"),
348348
}

0 commit comments

Comments
 (0)