Skip to content

Commit acde85a

Browse files
tyranronilslv
andauthored
Implement graphql_input_value! and graphql_vars! macros (#996, #503)
- add `From` impls to `InputValue` mirroring `Value` impls to provide better support for `Option` handling - support expressions in `graphql_value!` macro - use `null` in addition to `None` to create `Value::Null` in `graphql_value!` macro to mirror `serde_json::json!` - use macros for `InputValue` and `Variables` construction in tests Co-authored-by: Ilya Solovyiov <[email protected]>
1 parent bc66a2d commit acde85a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+3336
-1998
lines changed

integration_tests/juniper_tests/src/array.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use juniper::{
2-
graphql_object, graphql_value, EmptyMutation, EmptySubscription, GraphQLInputObject, RootNode,
3-
Variables,
2+
graphql_object, graphql_value, graphql_vars, EmptyMutation, EmptySubscription,
3+
GraphQLInputObject, RootNode,
44
};
55

66
mod as_output_field {
@@ -24,7 +24,7 @@ mod as_output_field {
2424
"#;
2525

2626
let schema = RootNode::new(Query, EmptyMutation::new(), EmptySubscription::new());
27-
let (res, errors) = juniper::execute(query, None, &schema, &Variables::new(), &())
27+
let (res, errors) = juniper::execute(query, None, &schema, &graphql_vars! {}, &())
2828
.await
2929
.unwrap();
3030

@@ -68,7 +68,7 @@ mod as_input_field {
6868
"#;
6969

7070
let schema = RootNode::new(Query, EmptyMutation::new(), EmptySubscription::new());
71-
let (res, errors) = juniper::execute(query, None, &schema, &Variables::new(), &())
71+
let (res, errors) = juniper::execute(query, None, &schema, &graphql_vars! {}, &())
7272
.await
7373
.unwrap();
7474

@@ -85,7 +85,7 @@ mod as_input_field {
8585
"#;
8686

8787
let schema = RootNode::new(Query, EmptyMutation::new(), EmptySubscription::new());
88-
let res = juniper::execute(query, None, &schema, &Variables::new(), &()).await;
88+
let res = juniper::execute(query, None, &schema, &graphql_vars! {}, &()).await;
8989

9090
assert!(res.is_err());
9191
assert!(res
@@ -103,7 +103,7 @@ mod as_input_field {
103103
"#;
104104

105105
let schema = RootNode::new(Query, EmptyMutation::new(), EmptySubscription::new());
106-
let res = juniper::execute(query, None, &schema, &Variables::new(), &()).await;
106+
let res = juniper::execute(query, None, &schema, &graphql_vars! {}, &()).await;
107107

108108
assert!(res.is_err());
109109
assert!(res
@@ -121,7 +121,7 @@ mod as_input_field {
121121
"#;
122122

123123
let schema = RootNode::new(Query, EmptyMutation::new(), EmptySubscription::new());
124-
let (res, errors) = juniper::execute(query, None, &schema, &Variables::new(), &())
124+
let (res, errors) = juniper::execute(query, None, &schema, &graphql_vars! {}, &())
125125
.await
126126
.unwrap();
127127

@@ -159,7 +159,7 @@ mod as_input_argument {
159159
"#;
160160

161161
let schema = RootNode::new(Query, EmptyMutation::new(), EmptySubscription::new());
162-
let (res, errors) = juniper::execute(query, None, &schema, &Variables::new(), &())
162+
let (res, errors) = juniper::execute(query, None, &schema, &graphql_vars! {}, &())
163163
.await
164164
.unwrap();
165165

@@ -176,7 +176,7 @@ mod as_input_argument {
176176
"#;
177177

178178
let schema = RootNode::new(Query, EmptyMutation::new(), EmptySubscription::new());
179-
let res = juniper::execute(query, None, &schema, &Variables::new(), &()).await;
179+
let res = juniper::execute(query, None, &schema, &graphql_vars! {}, &()).await;
180180

181181
assert!(res.is_err());
182182
assert!(res
@@ -194,7 +194,7 @@ mod as_input_argument {
194194
"#;
195195

196196
let schema = RootNode::new(Query, EmptyMutation::new(), EmptySubscription::new());
197-
let res = juniper::execute(query, None, &schema, &Variables::new(), &()).await;
197+
let res = juniper::execute(query, None, &schema, &graphql_vars! {}, &()).await;
198198

199199
assert!(res.is_err());
200200
assert!(res
@@ -212,7 +212,7 @@ mod as_input_argument {
212212
"#;
213213

214214
let schema = RootNode::new(Query, EmptyMutation::new(), EmptySubscription::new());
215-
let (res, errors) = juniper::execute(query, None, &schema, &Variables::new(), &())
215+
let (res, errors) = juniper::execute(query, None, &schema, &graphql_vars! {}, &())
216216
.await
217217
.unwrap();
218218

@@ -229,7 +229,7 @@ mod as_input_argument {
229229
"#;
230230

231231
let schema = RootNode::new(Query, EmptyMutation::new(), EmptySubscription::new());
232-
let (res, errors) = juniper::execute(query, None, &schema, &Variables::new(), &())
232+
let (res, errors) = juniper::execute(query, None, &schema, &graphql_vars! {}, &())
233233
.await
234234
.unwrap();
235235

@@ -246,7 +246,7 @@ mod as_input_argument {
246246
"#;
247247

248248
let schema = RootNode::new(Query, EmptyMutation::new(), EmptySubscription::new());
249-
let (res, errors) = juniper::execute(query, None, &schema, &Variables::new(), &())
249+
let (res, errors) = juniper::execute(query, None, &schema, &graphql_vars! {}, &())
250250
.await
251251
.unwrap();
252252

integration_tests/juniper_tests/src/codegen/derive_enum.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use fnv::FnvHashMap;
22
use juniper::{
3-
DefaultScalarValue, FromInputValue, GraphQLEnum, GraphQLType, InputValue, Registry,
3+
graphql_input_value, DefaultScalarValue, FromInputValue, GraphQLEnum, GraphQLType, Registry,
44
ToInputValue,
55
};
66

@@ -74,26 +74,26 @@ fn test_derived_enum() {
7474
// Test no rename variant.
7575
assert_eq!(
7676
<_ as ToInputValue>::to_input_value(&NoRenameEnum::AnotherVariant),
77-
InputValue::scalar("AnotherVariant")
77+
graphql_input_value!("AnotherVariant"),
7878
);
7979

8080
// Test Regular variant.
8181
assert_eq!(
8282
<_ as ToInputValue>::to_input_value(&SomeEnum::Regular),
83-
InputValue::scalar("REGULAR")
83+
graphql_input_value!("REGULAR"),
8484
);
8585
assert_eq!(
86-
FromInputValue::<DefaultScalarValue>::from_input_value(&InputValue::scalar("REGULAR")),
87-
Some(SomeEnum::Regular)
86+
FromInputValue::<DefaultScalarValue>::from_input_value(&graphql_input_value!(REGULAR)),
87+
Some(SomeEnum::Regular),
8888
);
8989

9090
// Test FULL variant.
9191
assert_eq!(
9292
<_ as ToInputValue>::to_input_value(&SomeEnum::Full),
93-
InputValue::scalar("FULL")
93+
graphql_input_value!("FULL"),
9494
);
9595
assert_eq!(
96-
FromInputValue::<DefaultScalarValue>::from_input_value(&InputValue::scalar("FULL")),
96+
FromInputValue::<DefaultScalarValue>::from_input_value(&graphql_input_value!(FULL)),
9797
Some(SomeEnum::Full)
9898
);
9999
}

integration_tests/juniper_tests/src/codegen/derive_input_object.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use fnv::FnvHashMap;
22
use juniper::{
3-
marker, DefaultScalarValue, FromInputValue, GraphQLInputObject, GraphQLType, GraphQLValue,
4-
InputValue, Registry, ToInputValue,
3+
graphql_input_value, marker, DefaultScalarValue, FromInputValue, GraphQLInputObject,
4+
GraphQLType, GraphQLValue, InputValue, Registry, ToInputValue,
55
};
66

77
#[derive(GraphQLInputObject, Debug, PartialEq)]
@@ -65,7 +65,7 @@ impl<'a> FromInputValue for &'a Fake {
6565

6666
impl<'a> ToInputValue for &'a Fake {
6767
fn to_input_value(&self) -> InputValue {
68-
InputValue::scalar("this is fake")
68+
graphql_input_value!("this is fake")
6969
}
7070
}
7171

@@ -119,19 +119,17 @@ fn test_derived_input_object() {
119119

120120
// Test default value injection.
121121

122-
let input_no_defaults: InputValue = ::serde_json::from_value(serde_json::json!({
122+
let input_no_defaults = graphql_input_value!({
123123
"regularField": "a",
124-
}))
125-
.unwrap();
126-
127-
let output_no_defaults: Input = FromInputValue::from_input_value(&input_no_defaults).unwrap();
124+
});
125+
let output_no_defaults = Input::from_input_value(&input_no_defaults).unwrap();
128126
assert_eq!(
129127
output_no_defaults,
130128
Input {
131129
regular_field: "a".into(),
132130
c: 33,
133131
other: None,
134-
}
132+
},
135133
);
136134

137135
// Test with all values supplied.
@@ -150,7 +148,7 @@ fn test_derived_input_object() {
150148
regular_field: "a".into(),
151149
c: 55,
152150
other: Some(true),
153-
}
151+
},
154152
);
155153

156154
// Test disable renaming
@@ -165,7 +163,7 @@ fn test_derived_input_object() {
165163
output,
166164
NoRenameInput {
167165
regular_field: "hello".into(),
168-
}
166+
},
169167
);
170168
}
171169

integration_tests/juniper_tests/src/codegen/derive_object_with_raw_idents.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use juniper::{
2-
execute, graphql_object, graphql_value, EmptyMutation, EmptySubscription, GraphQLInputObject,
3-
RootNode, Value, Variables,
2+
execute, graphql_object, graphql_value, graphql_vars, EmptyMutation, EmptySubscription,
3+
GraphQLInputObject, RootNode, Value,
44
};
55

66
pub struct Query;
@@ -93,7 +93,7 @@ async fn run_type_info_query(doc: &str) -> Value {
9393
EmptySubscription::<()>::new(),
9494
);
9595

96-
let (result, errs) = execute(doc, None, &schema, &Variables::new(), &())
96+
let (result, errs) = execute(doc, None, &schema, &graphql_vars! {}, &())
9797
.await
9898
.expect("Execution failed");
9999

integration_tests/juniper_tests/src/codegen/impl_scalar.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use juniper::{
2-
execute, graphql_object, graphql_scalar, graphql_value, DefaultScalarValue, EmptyMutation,
3-
EmptySubscription, Object, ParseScalarResult, ParseScalarValue, RootNode, Value, Variables,
2+
execute, graphql_object, graphql_scalar, graphql_value, graphql_vars, DefaultScalarValue,
3+
EmptyMutation, EmptySubscription, Object, ParseScalarResult, ParseScalarValue, RootNode, Value,
44
};
55

66
use crate::custom_scalar::MyScalarValue;
@@ -169,7 +169,7 @@ where
169169
EmptySubscription::<()>::new(),
170170
);
171171

172-
let (result, errs) = execute(doc, None, &schema, &Variables::new(), &())
172+
let (result, errs) = execute(doc, None, &schema, &graphql_vars! {}, &())
173173
.await
174174
.expect("Execution failed");
175175

@@ -226,7 +226,7 @@ async fn default_name_introspection() {
226226
);
227227
assert_eq!(
228228
type_info.get_field_value("description"),
229-
Some(&graphql_value!(None)),
229+
Some(&graphql_value!(null)),
230230
);
231231
})
232232
.await;
@@ -250,7 +250,7 @@ async fn other_order_introspection() {
250250
);
251251
assert_eq!(
252252
type_info.get_field_value("description"),
253-
Some(&graphql_value!(None)),
253+
Some(&graphql_value!(null)),
254254
);
255255
})
256256
.await;
@@ -274,7 +274,7 @@ async fn named_introspection() {
274274
);
275275
assert_eq!(
276276
type_info.get_field_value("description"),
277-
Some(&graphql_value!(None)),
277+
Some(&graphql_value!(null)),
278278
);
279279
})
280280
.await;
@@ -299,7 +299,7 @@ async fn scalar_description_introspection() {
299299
assert_eq!(
300300
type_info.get_field_value("description"),
301301
Some(&graphql_value!(
302-
"A sample scalar, represented as an integer"
302+
"A sample scalar, represented as an integer",
303303
)),
304304
);
305305
})
@@ -324,7 +324,7 @@ async fn generated_scalar_introspection() {
324324
);
325325
assert_eq!(
326326
type_info.get_field_value("description"),
327-
Some(&graphql_value!(None)),
327+
Some(&graphql_value!(null)),
328328
);
329329
})
330330
.await;
@@ -341,7 +341,7 @@ async fn resolves_with_custom_scalar_value() {
341341
);
342342

343343
assert_eq!(
344-
execute(DOC, None, &schema, &Variables::new(), &()).await,
344+
execute(DOC, None, &schema, &graphql_vars! {}, &()).await,
345345
Ok((graphql_value!({"withCustomScalarValue": 0}), vec![])),
346346
);
347347
}

0 commit comments

Comments
 (0)