Skip to content

Commit

Permalink
Implement graphql_input_value! and graphql_vars! macros (#996, #503)
Browse files Browse the repository at this point in the history
- 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]>
  • Loading branch information
tyranron and ilslv authored Nov 26, 2021
1 parent bc66a2d commit acde85a
Show file tree
Hide file tree
Showing 58 changed files with 3,336 additions and 1,998 deletions.
26 changes: 13 additions & 13 deletions integration_tests/juniper_tests/src/array.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use juniper::{
graphql_object, graphql_value, EmptyMutation, EmptySubscription, GraphQLInputObject, RootNode,
Variables,
graphql_object, graphql_value, graphql_vars, EmptyMutation, EmptySubscription,
GraphQLInputObject, RootNode,
};

mod as_output_field {
Expand All @@ -24,7 +24,7 @@ mod as_output_field {
"#;

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

Expand Down Expand Up @@ -68,7 +68,7 @@ mod as_input_field {
"#;

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

Expand All @@ -85,7 +85,7 @@ mod as_input_field {
"#;

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

assert!(res.is_err());
assert!(res
Expand All @@ -103,7 +103,7 @@ mod as_input_field {
"#;

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

assert!(res.is_err());
assert!(res
Expand All @@ -121,7 +121,7 @@ mod as_input_field {
"#;

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

Expand Down Expand Up @@ -159,7 +159,7 @@ mod as_input_argument {
"#;

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

Expand All @@ -176,7 +176,7 @@ mod as_input_argument {
"#;

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

assert!(res.is_err());
assert!(res
Expand All @@ -194,7 +194,7 @@ mod as_input_argument {
"#;

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

assert!(res.is_err());
assert!(res
Expand All @@ -212,7 +212,7 @@ mod as_input_argument {
"#;

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

Expand All @@ -229,7 +229,7 @@ mod as_input_argument {
"#;

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

Expand All @@ -246,7 +246,7 @@ mod as_input_argument {
"#;

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

Expand Down
14 changes: 7 additions & 7 deletions integration_tests/juniper_tests/src/codegen/derive_enum.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use fnv::FnvHashMap;
use juniper::{
DefaultScalarValue, FromInputValue, GraphQLEnum, GraphQLType, InputValue, Registry,
graphql_input_value, DefaultScalarValue, FromInputValue, GraphQLEnum, GraphQLType, Registry,
ToInputValue,
};

Expand Down Expand Up @@ -74,26 +74,26 @@ fn test_derived_enum() {
// Test no rename variant.
assert_eq!(
<_ as ToInputValue>::to_input_value(&NoRenameEnum::AnotherVariant),
InputValue::scalar("AnotherVariant")
graphql_input_value!("AnotherVariant"),
);

// Test Regular variant.
assert_eq!(
<_ as ToInputValue>::to_input_value(&SomeEnum::Regular),
InputValue::scalar("REGULAR")
graphql_input_value!("REGULAR"),
);
assert_eq!(
FromInputValue::<DefaultScalarValue>::from_input_value(&InputValue::scalar("REGULAR")),
Some(SomeEnum::Regular)
FromInputValue::<DefaultScalarValue>::from_input_value(&graphql_input_value!(REGULAR)),
Some(SomeEnum::Regular),
);

// Test FULL variant.
assert_eq!(
<_ as ToInputValue>::to_input_value(&SomeEnum::Full),
InputValue::scalar("FULL")
graphql_input_value!("FULL"),
);
assert_eq!(
FromInputValue::<DefaultScalarValue>::from_input_value(&InputValue::scalar("FULL")),
FromInputValue::<DefaultScalarValue>::from_input_value(&graphql_input_value!(FULL)),
Some(SomeEnum::Full)
);
}
Expand Down
20 changes: 9 additions & 11 deletions integration_tests/juniper_tests/src/codegen/derive_input_object.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use fnv::FnvHashMap;
use juniper::{
marker, DefaultScalarValue, FromInputValue, GraphQLInputObject, GraphQLType, GraphQLValue,
InputValue, Registry, ToInputValue,
graphql_input_value, marker, DefaultScalarValue, FromInputValue, GraphQLInputObject,
GraphQLType, GraphQLValue, InputValue, Registry, ToInputValue,
};

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

impl<'a> ToInputValue for &'a Fake {
fn to_input_value(&self) -> InputValue {
InputValue::scalar("this is fake")
graphql_input_value!("this is fake")
}
}

Expand Down Expand Up @@ -119,19 +119,17 @@ fn test_derived_input_object() {

// Test default value injection.

let input_no_defaults: InputValue = ::serde_json::from_value(serde_json::json!({
let input_no_defaults = graphql_input_value!({
"regularField": "a",
}))
.unwrap();

let output_no_defaults: Input = FromInputValue::from_input_value(&input_no_defaults).unwrap();
});
let output_no_defaults = Input::from_input_value(&input_no_defaults).unwrap();
assert_eq!(
output_no_defaults,
Input {
regular_field: "a".into(),
c: 33,
other: None,
}
},
);

// Test with all values supplied.
Expand All @@ -150,7 +148,7 @@ fn test_derived_input_object() {
regular_field: "a".into(),
c: 55,
other: Some(true),
}
},
);

// Test disable renaming
Expand All @@ -165,7 +163,7 @@ fn test_derived_input_object() {
output,
NoRenameInput {
regular_field: "hello".into(),
}
},
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use juniper::{
execute, graphql_object, graphql_value, EmptyMutation, EmptySubscription, GraphQLInputObject,
RootNode, Value, Variables,
execute, graphql_object, graphql_value, graphql_vars, EmptyMutation, EmptySubscription,
GraphQLInputObject, RootNode, Value,
};

pub struct Query;
Expand Down Expand Up @@ -93,7 +93,7 @@ async fn run_type_info_query(doc: &str) -> Value {
EmptySubscription::<()>::new(),
);

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

Expand Down
18 changes: 9 additions & 9 deletions integration_tests/juniper_tests/src/codegen/impl_scalar.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use juniper::{
execute, graphql_object, graphql_scalar, graphql_value, DefaultScalarValue, EmptyMutation,
EmptySubscription, Object, ParseScalarResult, ParseScalarValue, RootNode, Value, Variables,
execute, graphql_object, graphql_scalar, graphql_value, graphql_vars, DefaultScalarValue,
EmptyMutation, EmptySubscription, Object, ParseScalarResult, ParseScalarValue, RootNode, Value,
};

use crate::custom_scalar::MyScalarValue;
Expand Down Expand Up @@ -169,7 +169,7 @@ where
EmptySubscription::<()>::new(),
);

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

Expand Down Expand Up @@ -226,7 +226,7 @@ async fn default_name_introspection() {
);
assert_eq!(
type_info.get_field_value("description"),
Some(&graphql_value!(None)),
Some(&graphql_value!(null)),
);
})
.await;
Expand All @@ -250,7 +250,7 @@ async fn other_order_introspection() {
);
assert_eq!(
type_info.get_field_value("description"),
Some(&graphql_value!(None)),
Some(&graphql_value!(null)),
);
})
.await;
Expand All @@ -274,7 +274,7 @@ async fn named_introspection() {
);
assert_eq!(
type_info.get_field_value("description"),
Some(&graphql_value!(None)),
Some(&graphql_value!(null)),
);
})
.await;
Expand All @@ -299,7 +299,7 @@ async fn scalar_description_introspection() {
assert_eq!(
type_info.get_field_value("description"),
Some(&graphql_value!(
"A sample scalar, represented as an integer"
"A sample scalar, represented as an integer",
)),
);
})
Expand All @@ -324,7 +324,7 @@ async fn generated_scalar_introspection() {
);
assert_eq!(
type_info.get_field_value("description"),
Some(&graphql_value!(None)),
Some(&graphql_value!(null)),
);
})
.await;
Expand All @@ -341,7 +341,7 @@ async fn resolves_with_custom_scalar_value() {
);

assert_eq!(
execute(DOC, None, &schema, &Variables::new(), &()).await,
execute(DOC, None, &schema, &graphql_vars! {}, &()).await,
Ok((graphql_value!({"withCustomScalarValue": 0}), vec![])),
);
}
Loading

0 comments on commit acde85a

Please sign in to comment.