@@ -49,6 +49,14 @@ graphql_input_object!(
49
49
}
50
50
) ;
51
51
52
+ graphql_input_object ! (
53
+ #[ derive( Debug ) ]
54
+ struct ExampleInputObject {
55
+ a: Option <String >,
56
+ b: i64 ,
57
+ }
58
+ ) ;
59
+
52
60
graphql_object ! ( TestType : ( ) |& self | {
53
61
field field_with_object_input( input: Option <TestInputObject >) -> String {
54
62
format!( "{:?}" , input)
@@ -85,6 +93,10 @@ graphql_object!(TestType: () |&self| {
85
93
field nn_list_nn( input: Vec <String >) -> String {
86
94
format!( "{:?}" , input)
87
95
}
96
+
97
+ field example_input( arg: ExampleInputObject ) -> String {
98
+ format!( "a: {:?}, b: {:?}" , arg. a, arg. b)
99
+ }
88
100
} ) ;
89
101
90
102
fn run_variable_query < F > ( query : & str , vars : Variables , f : F )
@@ -748,3 +760,134 @@ fn default_argument_when_nullable_variable_set_to_null() {
748
760
Some ( & Value :: string( r#""Hello World""# ) ) ) ;
749
761
} ) ;
750
762
}
763
+
764
+ #[ test]
765
+ fn nullable_input_object_arguments_successful_without_variables ( ) {
766
+ run_query (
767
+ r#"{ exampleInput(arg: {a: "abc", b: 123}) }"# ,
768
+ |result| {
769
+ assert_eq ! (
770
+ result. get( "exampleInput" ) ,
771
+ Some ( & Value :: string( r#"a: Some("abc"), b: 123"# ) ) ) ;
772
+ } ) ;
773
+
774
+ run_query (
775
+ r#"{ exampleInput(arg: {a: null, b: 1}) }"# ,
776
+ |result| {
777
+ assert_eq ! (
778
+ result. get( "exampleInput" ) ,
779
+ Some ( & Value :: string( r#"a: None, b: 1"# ) ) ) ;
780
+ } ) ;
781
+ }
782
+
783
+ #[ test]
784
+ fn nullable_input_object_arguments_successful_with_variables ( ) {
785
+ run_variable_query (
786
+ r#"query q($var: Int!) { exampleInput(arg: {b: $var}) }"# ,
787
+ vec ! [
788
+ ( "var" . to_owned( ) , InputValue :: int( 123 ) ) ,
789
+ ] . into_iter ( ) . collect ( ) ,
790
+ |result| {
791
+ assert_eq ! (
792
+ result. get( "exampleInput" ) ,
793
+ Some ( & Value :: string( r#"a: None, b: 123"# ) ) ) ;
794
+ } ) ;
795
+
796
+ run_variable_query (
797
+ r#"query q($var: String) { exampleInput(arg: {a: $var, b: 1}) }"# ,
798
+ vec ! [
799
+ ( "var" . to_owned( ) , InputValue :: null( ) ) ,
800
+ ] . into_iter ( ) . collect ( ) ,
801
+ |result| {
802
+ assert_eq ! (
803
+ result. get( "exampleInput" ) ,
804
+ Some ( & Value :: string( r#"a: None, b: 1"# ) ) ) ;
805
+ } ) ;
806
+
807
+ run_variable_query (
808
+ r#"query q($var: String) { exampleInput(arg: {a: $var, b: 1}) }"# ,
809
+ vec ! [
810
+ ] . into_iter ( ) . collect ( ) ,
811
+ |result| {
812
+ assert_eq ! (
813
+ result. get( "exampleInput" ) ,
814
+ Some ( & Value :: string( r#"a: None, b: 1"# ) ) ) ;
815
+ } ) ;
816
+ }
817
+
818
+ #[ test]
819
+ fn does_not_allow_missing_required_field ( ) {
820
+ let schema = RootNode :: new ( TestType , EmptyMutation :: < ( ) > :: new ( ) ) ;
821
+
822
+ let query = r#"{ exampleInput(arg: {a: "abc"}) }"# ;
823
+ let vars = vec ! [
824
+ ] . into_iter ( ) . collect ( ) ;
825
+
826
+ let error = :: execute ( query, None , & schema, & vars, & ( ) )
827
+ . unwrap_err ( ) ;
828
+
829
+ assert_eq ! ( error, ValidationError ( vec![
830
+ RuleError :: new(
831
+ r#"Invalid value for argument "arg", expected type "ExampleInputObject!""# ,
832
+ & [ SourcePosition :: new( 20 , 0 , 20 ) ] ,
833
+ ) ,
834
+ ] ) ) ;
835
+ }
836
+
837
+ #[ test]
838
+ fn does_not_allow_null_in_required_field ( ) {
839
+ let schema = RootNode :: new ( TestType , EmptyMutation :: < ( ) > :: new ( ) ) ;
840
+
841
+ let query = r#"{ exampleInput(arg: {a: "abc", b: null}) }"# ;
842
+ let vars = vec ! [
843
+ ] . into_iter ( ) . collect ( ) ;
844
+
845
+ let error = :: execute ( query, None , & schema, & vars, & ( ) )
846
+ . unwrap_err ( ) ;
847
+
848
+ assert_eq ! ( error, ValidationError ( vec![
849
+ RuleError :: new(
850
+ r#"Invalid value for argument "arg", expected type "ExampleInputObject!""# ,
851
+ & [ SourcePosition :: new( 20 , 0 , 20 ) ] ,
852
+ ) ,
853
+ ] ) ) ;
854
+ }
855
+
856
+ #[ test]
857
+ fn does_not_allow_missing_variable_for_required_field ( ) {
858
+ let schema = RootNode :: new ( TestType , EmptyMutation :: < ( ) > :: new ( ) ) ;
859
+
860
+ let query = r#"query q($var: Int!) { exampleInput(arg: {b: $var}) }"# ;
861
+ let vars = vec ! [
862
+ ] . into_iter ( ) . collect ( ) ;
863
+
864
+ let error = :: execute ( query, None , & schema, & vars, & ( ) )
865
+ . unwrap_err ( ) ;
866
+
867
+ assert_eq ! ( error, ValidationError ( vec![
868
+ RuleError :: new(
869
+ r#"Variable "$var" of required type "Int!" was not provided."# ,
870
+ & [ SourcePosition :: new( 8 , 0 , 8 ) ] ,
871
+ ) ,
872
+ ] ) ) ;
873
+ }
874
+
875
+ #[ test]
876
+ fn does_not_allow_null_variable_for_required_field ( ) {
877
+ let schema = RootNode :: new ( TestType , EmptyMutation :: < ( ) > :: new ( ) ) ;
878
+
879
+ let query = r#"query q($var: Int!) { exampleInput(arg: {b: $var}) }"# ;
880
+ let vars = vec ! [
881
+ ( "var" . to_owned( ) , InputValue :: null( ) ) ,
882
+ ] . into_iter ( ) . collect ( ) ;
883
+
884
+ let error = :: execute ( query, None , & schema, & vars, & ( ) )
885
+ . unwrap_err ( ) ;
886
+
887
+ assert_eq ! ( error, ValidationError ( vec![
888
+ RuleError :: new(
889
+ r#"Variable "$var" of required type "Int!" was not provided."# ,
890
+ & [ SourcePosition :: new( 8 , 0 , 8 ) ] ,
891
+ ) ,
892
+ ] ) ) ;
893
+ }
0 commit comments