@@ -10,7 +10,7 @@ class Query < GraphQL::Schema::Object
10
10
argument :arg_with_block , String , required : false do
11
11
description "test"
12
12
end
13
-
13
+ argument :required_with_default_arg , Int , required : true , default_value : 1
14
14
argument :aliased_arg , String , required : false , as : :renamed
15
15
argument :prepared_arg , Int , required : false , prepare : :multiply
16
16
argument :prepared_by_proc_arg , Int , required : false , prepare : -> ( val , context ) { context [ :multiply_by ] * val }
@@ -30,7 +30,11 @@ def call(val, context)
30
30
end
31
31
32
32
def field ( **args )
33
- args . inspect
33
+ # sort the fields so that they match the output of the new interpreter
34
+ sorted_keys = args . keys . sort
35
+ sorted_args = { }
36
+ sorted_keys . each { |k | sorted_args [ k ] = args [ k ] }
37
+ sorted_args . inspect
34
38
end
35
39
36
40
def multiply ( val )
@@ -48,7 +52,7 @@ class Schema < GraphQL::Schema
48
52
49
53
describe "#keys" do
50
54
it "is not overwritten by the 'keys' argument" do
51
- expected_keys = [ "aliasedArg" , "arg" , "argWithBlock" , "explodingPreparedArg" , "keys" , "preparedArg" , "preparedByCallableArg" , "preparedByProcArg" ]
55
+ expected_keys = [ "aliasedArg" , "arg" , "argWithBlock" , "explodingPreparedArg" , "keys" , "preparedArg" , "preparedByCallableArg" , "preparedByProcArg" , "requiredWithDefaultArg" ]
52
56
assert_equal expected_keys , SchemaArgumentTest ::Query . fields [ "field" ] . arguments . keys . sort
53
57
end
54
58
end
@@ -103,7 +107,7 @@ class Schema < GraphQL::Schema
103
107
104
108
res = SchemaArgumentTest ::Schema . execute ( query_str )
105
109
# Make sure it's getting the renamed symbol:
106
- assert_equal '{:renamed=>"x"}' , res [ "data" ] [ "field" ]
110
+ assert_equal '{:renamed=>"x", :required_with_default_arg=>1 }' , res [ "data" ] [ "field" ]
107
111
end
108
112
end
109
113
@@ -115,7 +119,7 @@ class Schema < GraphQL::Schema
115
119
116
120
res = SchemaArgumentTest ::Schema . execute ( query_str , context : { multiply_by : 3 } )
117
121
# Make sure it's getting the renamed symbol:
118
- assert_equal '{:prepared_arg=>15}' , res [ "data" ] [ "field" ]
122
+ assert_equal '{:prepared_arg=>15, :required_with_default_arg=>1 }' , res [ "data" ] [ "field" ]
119
123
end
120
124
121
125
it "calls the method on the provided Proc" do
@@ -125,7 +129,7 @@ class Schema < GraphQL::Schema
125
129
126
130
res = SchemaArgumentTest ::Schema . execute ( query_str , context : { multiply_by : 3 } )
127
131
# Make sure it's getting the renamed symbol:
128
- assert_equal '{:prepared_by_proc_arg=>15}' , res [ "data" ] [ "field" ]
132
+ assert_equal '{:prepared_by_proc_arg=>15, :required_with_default_arg=>1 }' , res [ "data" ] [ "field" ]
129
133
end
130
134
131
135
it "calls the method on the provided callable object" do
@@ -135,7 +139,7 @@ class Schema < GraphQL::Schema
135
139
136
140
res = SchemaArgumentTest ::Schema . execute ( query_str , context : { multiply_by : 3 } )
137
141
# Make sure it's getting the renamed symbol:
138
- assert_equal '{:prepared_by_callable_arg=>15}' , res [ "data" ] [ "field" ]
142
+ assert_equal '{:prepared_by_callable_arg=>15, :required_with_default_arg=>1 }' , res [ "data" ] [ "field" ]
139
143
end
140
144
141
145
it "handles exceptions raised by prepare" do
@@ -144,9 +148,39 @@ class Schema < GraphQL::Schema
144
148
GRAPHQL
145
149
146
150
res = SchemaArgumentTest ::Schema . execute ( query_str , context : { multiply_by : 3 } )
147
- assert_equal ( { 'f1' => '{:arg=>"echo"}' , 'f2' => nil } , res [ 'data' ] )
151
+ assert_equal ( { 'f1' => '{:arg=>"echo", :required_with_default_arg=>1 }' , 'f2' => nil } , res [ 'data' ] )
148
152
assert_equal ( res [ 'errors' ] [ 0 ] [ 'message' ] , 'boom!' )
149
153
assert_equal ( res [ 'errors' ] [ 0 ] [ 'path' ] , [ 'f2' ] )
150
154
end
151
155
end
156
+
157
+ describe "default_value:" do
158
+ it 'uses default_value: with no input' do
159
+ query_str = <<-GRAPHQL
160
+ { field() }
161
+ GRAPHQL
162
+
163
+ res = SchemaArgumentTest ::Schema . execute ( query_str )
164
+ assert_equal '{:required_with_default_arg=>1}' , res [ "data" ] [ "field" ]
165
+ end
166
+
167
+ it 'uses provided input value' do
168
+ query_str = <<-GRAPHQL
169
+ { field(requiredWithDefaultArg: 2) }
170
+ GRAPHQL
171
+
172
+ res = SchemaArgumentTest ::Schema . execute ( query_str )
173
+ assert_equal '{:required_with_default_arg=>2}' , res [ "data" ] [ "field" ]
174
+ end
175
+
176
+ it 'respects non-null type' do
177
+ query_str = <<-GRAPHQL
178
+ { field(requiredWithDefaultArg: null) }
179
+ GRAPHQL
180
+
181
+ res = SchemaArgumentTest ::Schema . execute ( query_str )
182
+ assert_equal "Argument 'requiredWithDefaultArg' on Field 'field' has an invalid value. Expected type 'Int!'." , res [ 'errors' ] [ 0 ] [ 'message' ]
183
+ end
184
+
185
+ end
152
186
end
0 commit comments