File tree Expand file tree Collapse file tree 2 files changed +30
-9
lines changed Expand file tree Collapse file tree 2 files changed +30
-9
lines changed Original file line number Diff line number Diff line change 11# frozen_string_literal: true
22module GraphQL
3- class CoercionError < GraphQL ::Error
4- # @return [Hash] Optional custom data for error objects which will be added
5- # under the `extensions` key.
6- attr_accessor :extensions
7-
8- def initialize ( message , extensions : nil )
9- @extensions = extensions
10- super ( message )
11- end
3+ class CoercionError < GraphQL ::ExecutionError
124 end
135end
Original file line number Diff line number Diff line change @@ -214,4 +214,33 @@ def self.coerce_result(value, _ctx)
214214 end
215215 end
216216 end
217+
218+ it "handles coercing null" do
219+ class CoerceNullSchema < GraphQL ::Schema
220+ class CustomScalar < GraphQL ::Schema ::Scalar
221+ class << self
222+ def coerce_input ( input_value , _context )
223+ raise GraphQL ::CoercionError , "Invalid value: #{ input_value . inspect } "
224+ end
225+ end
226+ end
227+
228+ class QueryType < GraphQL ::Schema ::Object
229+ field :hello , String do
230+ argument :input , CustomScalar , required : false
231+ end
232+
233+ def hello ( input : nil )
234+ "hello world"
235+ end
236+ end
237+
238+ query ( QueryType )
239+ end
240+ result = CoerceNullSchema . execute ( '{ hello(input: 5) }' )
241+ assert_equal ( [ "Invalid value: 5" ] , result [ "errors" ] . map { |err | err [ "message" ] } )
242+
243+ null_input_result = CoerceNullSchema . execute ( '{ hello(input: null) }' )
244+ assert_equal ( [ "Invalid value: nil" ] , null_input_result [ "errors" ] . map { |err | err [ "message" ] } )
245+ end
217246end
You can’t perform that action at this time.
0 commit comments