Skip to content
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

Use base class for errors in anonymous enum classes #5022

Merged
merged 1 commit into from
Jul 16, 2024
Merged
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
6 changes: 5 additions & 1 deletion lib/graphql/schema/enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,11 @@ def coerce_input(value_name, ctx)
end

def inherited(child_class)
child_class.const_set(:UnresolvedValueError, Class.new(Schema::Enum::UnresolvedValueError))
if child_class.name
# Don't assign a custom error class to anonymous classes
# because they would end up with names like `#<Class0x1234>::UnresolvedValueError` which messes up bug trackers
child_class.const_set(:UnresolvedValueError, Class.new(Schema::Enum::UnresolvedValueError))
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

I suspect that the same issue exists here:

https://github.com/rmosolgo/graphql-ruby/blob/v2.3.8/lib/graphql/schema/member/has_unresolved_type_error.rb#L10

Does it make sense to fix it as well?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good call! #5023

super
end

Expand Down
15 changes: 15 additions & 0 deletions spec/graphql/schema/enum_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,21 @@ def empty_enum
}
expected_isolated_message = "`:nonsense` was returned for `DairyAnimal`, but this isn't a valid value for `DairyAnimal`. Update the field or resolver to return one of `DairyAnimal`'s values instead."
assert_equal expected_isolated_message, err2.message
assert_equal "Dummy::DairyAnimal::UnresolvedValueError", err2.class.name

anon_enum = Class.new(GraphQL::Schema::Enum) do
graphql_name "AnonEnum"
value :one
value :two
end

err3 = assert_raises(GraphQL::Schema::Enum::UnresolvedValueError) {
anon_enum.coerce_isolated_result(:nonsense)
}

expected_anonymous_message = "`:nonsense` was returned for `AnonEnum`, but this isn't a valid value for `AnonEnum`. Update the field or resolver to return one of `AnonEnum`'s values instead."
assert_equal expected_anonymous_message, err3.message
assert_equal "GraphQL::Schema::Enum::UnresolvedValueError", err3.class.name
end

describe "resolving with a warden" do
Expand Down
Loading