Skip to content
Open
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
22 changes: 19 additions & 3 deletions lib/graphql/unresolved_type_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,27 @@ def initialize(value, field, parent_type, resolved_type, possible_types)
@parent_type = parent_type
@resolved_type = resolved_type
@possible_types = possible_types
message = "The value from \"#{field.graphql_name}\" on \"#{parent_type.graphql_name}\" could not be resolved to \"#{field.type.to_type_signature}\". " \
"(Received: `#{resolved_type.inspect}`, Expected: [#{possible_types.map(&:graphql_name).join(", ")}]) " \
"Make sure you have defined a `resolve_type` proc on your schema and that value `#{value.inspect}` " \
abstract_type = field.type.unwrap
message = "The value from \"#{field.graphql_name}\" on \"#{parent_type.graphql_name}\" could not be resolved to \"#{abstract_type.to_type_signature}\". " \
"(Received: `#{resolved_type.name ? resolved_type.inspect : resolved_type.graphql_name}`, Expected: [#{possible_types.map(&:graphql_name).join(", ")}]) " \
"Make sure you have defined a `resolve_type` method on your schema and that value `#{value.inspect}` " \
"gets resolved to a valid type. You may need to add your type to `orphan_types` if it implements an " \
"interface but isn't a return type of any other field."

if abstract_type.kind.interface? && (multiplex = Fiber[:__graphql_current_multiplex])
types = multiplex.queries.first.types # rubocop:disable Development/ContextIsPassedCop
if types.is_a?(Schema::Visibility::Profile)
message = message.dup
visibility = types.instance_variable_get(:@visibility)
cached_vis = types.instance_variable_get(:@cached_visible)
message << "\n\n`#{abstract_type.graphql_name}.orphan_types`: #{abstract_type.orphan_types}"
impls = visibility.all_interface_type_memberships[abstract_type]
message << "\n`Schema.visibility.all_interface_type_memberships[#{abstract_type.graphql_name}]` (#{impls.size}):"
impls.each do |(impl_type, memberships)|
message << "\n - `#{impl_type.graphql_name}` | Object? #{impl_type.kind.object?} | referenced? #{types.send(:referenced?, impl_type)} | visible? #{cached_vis[impl_type]} | membership_visible? #{memberships.map { |itm| cached_vis[itm]}}"
end
end
end
super(message)
end
end
Expand Down
9 changes: 8 additions & 1 deletion spec/graphql/schema/member/has_unresolved_type_error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

schema = Class.new(GraphQL::Schema) do
query(query_type)

use GraphQL::Schema::Visibility
def self.resolve_type(abs_t, obj, ctx)
ctx.schema.query
end
Expand All @@ -52,5 +52,12 @@ def self.resolve_type(abs_t, obj, ctx)
schema.execute("{ anonInt { __typename } }")
end
assert_equal "GraphQL::UnresolvedTypeError", err.class.name
assert_equal <<~ERR.chomp, err.message
The value from "anonInt" on "Query" could not be resolved to "AnonInt". (Received: `Query`, Expected: [Obj]) Make sure you have defined a `resolve_type` method on your schema and that value `1` gets resolved to a valid type. You may need to add your type to `orphan_types` if it implements an interface but isn\'t a return type of any other field.

`AnonInt.orphan_types`: []
`Schema.visibility.all_interface_type_memberships[AnonInt]` (1):
- `Obj` | Object? true | referenced? true | visible? true | membership_visible? [true]
ERR
end
end