Skip to content

Commit 74a6583

Browse files
committed
Test input object codegen
Only defaults left.
1 parent e3a9226 commit 74a6583

File tree

7 files changed

+92
-54
lines changed

7 files changed

+92
-54
lines changed

graphql_query_derive/src/inputs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl GqlInput {
2020
let fields = fields.iter().map(|field| {
2121
let ty = field.type_.to_rust(&context, "");
2222
let name = Ident::new(&field.name, Span::call_site());
23-
quote!(#name: #ty)
23+
quote!(pub #name: #ty)
2424
});
2525

2626
Ok(quote! {

graphql_query_derive/src/schema.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,20 +146,31 @@ impl Schema {
146146
.or(context._subscription_root.as_ref())
147147
.expect("no selection defined");
148148

149-
use proc_macro2::{Ident, Span};
150-
149+
// TODO: do something smarter here
151150
let scalar_definitions = context.schema.scalars.iter().map(|scalar_name| {
151+
use proc_macro2::{Ident, Span};
152152
let ident = Ident::new(scalar_name, Span::call_site());
153153
quote!(type #ident = String;)
154154
});
155155

156+
let input_object_definitions: Result<Vec<TokenStream>, _> = context
157+
.schema
158+
.inputs
159+
.values()
160+
.map(|i| i.to_rust(&context))
161+
.collect();
162+
let input_object_definitions = input_object_definitions?;
163+
156164
Ok(quote! {
157165
type Boolean = bool;
158166
type Float = f64;
159167
type Int = i64;
168+
type ID = String;
160169

161170
#(#scalar_definitions)*
162171

172+
#(#input_object_definitions)*
173+
163174
#(#enum_definitions)*
164175

165176
#(#fragment_definitions)*

tests/input_object_variables.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#[macro_use]
2+
extern crate graphql_client;
3+
#[macro_use]
4+
extern crate serde_derive;
5+
extern crate serde;
6+
extern crate serde_json;
7+
8+
#[derive(GraphQLQuery)]
9+
#[GraphQLQuery(
10+
query_path = "tests/input_object_variables/input_object_variables_query.graphql",
11+
schema_path = "tests/input_object_variables/input_object_variables_schema.graphql"
12+
)]
13+
#[allow(dead_code)]
14+
struct InputObjectVariablesQuery;
15+
16+
#[test]
17+
fn input_object_variables_query_variables_struct() {
18+
input_object_variables_query::Variables {
19+
msg: Some(input_object_variables_query::Message {
20+
content: None,
21+
to: Some(input_object_variables_query::Recipient {
22+
email: "[email protected]".to_string(),
23+
category: None,
24+
name: Some("Sarah Connor".to_string()),
25+
}),
26+
}),
27+
};
28+
}
29+
30+
#[derive(GraphQLQuery)]
31+
#[GraphQLQuery(
32+
query_path = "tests/input_object_variables/input_object_variables_query_defaults.graphql",
33+
schema_path = "tests/input_object_variables/input_object_variables_schema.graphql"
34+
)]
35+
#[allow(dead_code)]
36+
struct DefaultInputObjectVariablesQuery;
37+
38+
#[test]
39+
fn input_object_variables_default() {
40+
let variables = default_input_object_variables_query::Variables {
41+
msg: default_input_object_variables_query::Variables::default_msg(),
42+
reps: default_input_object_variables_query::Variables::default_reps(),
43+
};
44+
45+
let out = serde_json::to_string(&variables).unwrap();
46+
47+
assert_eq!(out, r#"{"msg":"o, hai","reps":3}"#);
48+
}

tests/input_object_variables/input_object_variables.rs

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
query VariablesQuery($msg: String!, $reps: Int) {
2-
echo(message: $msg, repetitions: $reps) {
3-
result
4-
}
1+
query VariablesQuery($msg: Message) {
2+
echo(message: $msg) {
3+
result
4+
}
55
}
Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,33 @@
1-
21
schema {
3-
query: ScalarVariablesQuery
2+
query: InputObjectVariablesQuery
3+
}
4+
5+
scalar Email
6+
7+
enum Category {
8+
PROFESSIONAL
9+
PERSONAL
10+
}
11+
12+
input Recipient {
13+
email: Email!
14+
name: String
15+
category: Category
16+
}
17+
18+
input Message {
19+
to: Recipient
20+
content: String
21+
}
22+
23+
input Options {
24+
pgpSignature: Boolean
425
}
526

6-
type ScalarVariablesQuery {
7-
echo(message: String!, repetitions: Int): EchoResult
27+
type InputObjectVariablesQuery {
28+
echo(message: Message!, options: Options = { pgpSignature: true }): EchoResult
829
}
930

1031
type EchoResult {
11-
result: String!
32+
result: String!
1233
}

0 commit comments

Comments
 (0)