@@ -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 }
@@ -48,7 +48,7 @@ class Schema < GraphQL::Schema
48
48
49
49
describe "#keys" do
50
50
it "is not overwritten by the 'keys' argument" do
51
- expected_keys = [ "aliasedArg" , "arg" , "argWithBlock" , "explodingPreparedArg" , "keys" , "preparedArg" , "preparedByCallableArg" , "preparedByProcArg" ]
51
+ expected_keys = [ "aliasedArg" , "arg" , "argWithBlock" , "explodingPreparedArg" , "keys" , "preparedArg" , "preparedByCallableArg" , "preparedByProcArg" , "requiredWithDefaultArg" ]
52
52
assert_equal expected_keys , SchemaArgumentTest ::Query . fields [ "field" ] . arguments . keys . sort
53
53
end
54
54
end
@@ -103,7 +103,7 @@ class Schema < GraphQL::Schema
103
103
104
104
res = SchemaArgumentTest ::Schema . execute ( query_str )
105
105
# Make sure it's getting the renamed symbol:
106
- assert_equal '{:renamed=>"x"}' , res [ "data" ] [ "field" ]
106
+ assert_equal '{:required_with_default_arg=>1, : renamed=>"x"}' , res [ "data" ] [ "field" ]
107
107
end
108
108
end
109
109
@@ -115,7 +115,7 @@ class Schema < GraphQL::Schema
115
115
116
116
res = SchemaArgumentTest ::Schema . execute ( query_str , context : { multiply_by : 3 } )
117
117
# Make sure it's getting the renamed symbol:
118
- assert_equal '{:prepared_arg=>15}' , res [ "data" ] [ "field" ]
118
+ assert_equal '{:required_with_default_arg=>1, : prepared_arg=>15}' , res [ "data" ] [ "field" ]
119
119
end
120
120
121
121
it "calls the method on the provided Proc" do
@@ -125,7 +125,7 @@ class Schema < GraphQL::Schema
125
125
126
126
res = SchemaArgumentTest ::Schema . execute ( query_str , context : { multiply_by : 3 } )
127
127
# Make sure it's getting the renamed symbol:
128
- assert_equal '{:prepared_by_proc_arg=>15}' , res [ "data" ] [ "field" ]
128
+ assert_equal '{:required_with_default_arg=>1, : prepared_by_proc_arg=>15}' , res [ "data" ] [ "field" ]
129
129
end
130
130
131
131
it "calls the method on the provided callable object" do
@@ -135,7 +135,7 @@ class Schema < GraphQL::Schema
135
135
136
136
res = SchemaArgumentTest ::Schema . execute ( query_str , context : { multiply_by : 3 } )
137
137
# Make sure it's getting the renamed symbol:
138
- assert_equal '{:prepared_by_callable_arg=>15}' , res [ "data" ] [ "field" ]
138
+ assert_equal '{:required_with_default_arg=>1, : prepared_by_callable_arg=>15}' , res [ "data" ] [ "field" ]
139
139
end
140
140
141
141
it "handles exceptions raised by prepare" do
@@ -144,9 +144,39 @@ class Schema < GraphQL::Schema
144
144
GRAPHQL
145
145
146
146
res = SchemaArgumentTest ::Schema . execute ( query_str , context : { multiply_by : 3 } )
147
- assert_equal ( { 'f1' => '{:arg=>"echo"}' , 'f2' => nil } , res [ 'data' ] )
147
+ assert_equal ( { 'f1' => '{:arg=>"echo", :required_with_default_arg=>1 }' , 'f2' => nil } , res [ 'data' ] )
148
148
assert_equal ( res [ 'errors' ] [ 0 ] [ 'message' ] , 'boom!' )
149
149
assert_equal ( res [ 'errors' ] [ 0 ] [ 'path' ] , [ 'f2' ] )
150
150
end
151
151
end
152
+
153
+ describe "default_value:" do
154
+ it 'uses default_value: with no input' do
155
+ query_str = <<-GRAPHQL
156
+ { field() }
157
+ GRAPHQL
158
+
159
+ res = SchemaArgumentTest ::Schema . execute ( query_str )
160
+ assert_equal '{:required_with_default_arg=>1}' , res [ "data" ] [ "field" ]
161
+ end
162
+
163
+ it 'uses provided input value' do
164
+ query_str = <<-GRAPHQL
165
+ { field(requiredWithDefaultArg: 2) }
166
+ GRAPHQL
167
+
168
+ res = SchemaArgumentTest ::Schema . execute ( query_str )
169
+ assert_equal '{:required_with_default_arg=>2}' , res [ "data" ] [ "field" ]
170
+ end
171
+
172
+ it 'respects non-null type' do
173
+ query_str = <<-GRAPHQL
174
+ { field(requiredWithDefaultArg: null) }
175
+ GRAPHQL
176
+
177
+ res = SchemaArgumentTest ::Schema . execute ( query_str )
178
+ assert_equal "Argument 'requiredWithDefaultArg' on Field 'field' has an invalid value. Expected type 'Int!'." , res [ 'errors' ] [ 0 ] [ 'message' ]
179
+ end
180
+
181
+ end
152
182
end
0 commit comments