Skip to content

Commit 575be8e

Browse files
authored
Merge pull request #4799 from rmosolgo/fix-coerce-null
Fix CoercionError handling with null
2 parents dea624f + 09087c1 commit 575be8e

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

lib/graphql/coercion_error.rb

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
# frozen_string_literal: true
22
module 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
135
end

spec/graphql/schema/scalar_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff 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
217246
end

0 commit comments

Comments
 (0)