Skip to content

Commit acdc50e

Browse files
committed
Improve error for false as type, fixes rmosolgo#1334
1 parent d256dd2 commit acdc50e

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

lib/graphql/schema/member/build_type.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def parse_type(type_expr, null:)
4242
if maybe_type.respond_to?(:graphql_definition)
4343
maybe_type
4444
else
45-
raise "Unexpected class/module found for GraphQL type: #{type_expr} (must be type definition class/module)"
45+
raise ArgumentError, "Unexpected class/module found for GraphQL type: #{type_expr} (must be type definition class/module)"
4646
end
4747
end
4848
end
@@ -72,6 +72,8 @@ def parse_type(type_expr, null:)
7272
# Eg `String` => GraphQL::STRING_TYPE
7373
parse_type(type_expr.name, null: true)
7474
end
75+
when false
76+
raise ArgumentError, "Received `false` instead of a type, maybe a `!` should be replaced with `null: true` (for fields) or `required: true` (for arguments)"
7577
end
7678

7779
if return_type.nil?

spec/graphql/schema/field_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,21 @@
174174

175175
assert_includes err.message, "Thing.stuff"
176176
end
177+
178+
it "makes a suggestion when the type is false" do
179+
thing = Class.new(GraphQL::Schema::Object) do
180+
graphql_name "Thing"
181+
# False might come from an invalid `!`
182+
field :stuff, false, null: false
183+
end
184+
185+
err = assert_raises ArgumentError do
186+
thing.fields["stuff"].type
187+
end
188+
189+
assert_includes err.message, "Thing.stuff"
190+
assert_includes err.message, "Received `false` instead of a type, maybe a `!` should be replaced with `null: true` (for fields) or `required: true` (for arguments)"
191+
end
177192
end
178193

179194
describe "mutation" do

0 commit comments

Comments
 (0)