Skip to content

Improve error messages for invalid input types #3133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/graphql/schema/traversal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def visit(schema, member, context_description)
prev_type = @type_map[type_defn.name]
# Continue to visit this type if it's the first time we've seen it:
if prev_type.nil?
validate_type(type_defn, context_description)
validate_type(type_defn)
@type_map[type_defn.name] = type_defn
case type_defn
when GraphQL::ObjectType
Expand Down Expand Up @@ -217,10 +217,10 @@ def visit_field_on_type(schema, type_defn, field_defn, dynamic_field: false)
GraphQL::Query::Arguments.construct_arguments_class(instrumented_field_defn)
end

def validate_type(member, context_description)
error_message = GraphQL::Schema::Validation.validate(member)
def validate_type(type)
error_message = GraphQL::Schema::Validation.validate(type)
if error_message
raise GraphQL::Schema::InvalidTypeError.new("#{context_description} is invalid: #{error_message}")
raise GraphQL::Schema::InvalidTypeError.new("#{type.name} is invalid: #{error_message}")
end
end
end
Expand Down
22 changes: 22 additions & 0 deletions spec/graphql/schema/traversal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,28 @@ def traversal(types)
it "raises an InvalidTypeError when passed an object that isnt a GraphQL::BaseType" do
assert_raises(GraphQL::Schema::InvalidTypeError) { traversal([another_invalid_type]) }
end

it "raises an InvalidTypeError for object field arguments referencing invalid input objects" do
other_type = GraphQL::ObjectType.define do
name "MyOtherType"
field :someField, GraphQL::STRING_TYPE
end

invalid_input_type = GraphQL::InputObjectType.define do
name "MyInput"
input_field :someField, other_type
end

type = GraphQL::ObjectType.define do
name "MyType"
field :anotherField, !GraphQL::STRING_TYPE do |field|
field.argument :anArgument, invalid_input_type
end
end

error = assert_raises(GraphQL::Schema::InvalidTypeError) { traversal([type]) }
assert_equal("MyInput is invalid: argument \"someField\" type must be a valid input type (Scalar or InputObject), not GraphQL::ObjectType (MyOtherType)", error.message)
end
end

describe "when a schema has multiple types with the same name" do
Expand Down