Skip to content

Commit 51c7e83

Browse files
committed
fix(Schema#resolve_type) also thread Query::Context through type resolution
1 parent 197331b commit 51c7e83

File tree

8 files changed

+16
-35
lines changed

8 files changed

+16
-35
lines changed

guides/defining_your_schema.md

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,15 @@ CoffeeType = GraphQL::ObjectType.define do
5151
end
5252
```
5353

54-
In order for your schema to expose members of an interface, it must be able to determine the GraphQL type for a given Ruby object. `InterfaceType` has a default `resolve_type` definition, or you can provide your own. Here's the default:
54+
In order for your schema to expose members of an interface, it must be able to determine the GraphQL type for a given Ruby object. You must define `resolve_type` in your schema:
5555

5656
```ruby
57-
BeverageInterface = GraphQL::InterfaceType.define do
57+
MySchema = GraphQL::Schema.define do
5858
# ...
5959
resolve_type -> (object, ctx) {
60+
# for example, look up types by class name
6061
type_name = object.class.name
61-
# you can access the interface's `possible_types` inside the proc
62-
possible_types = ctx.schema.possible_types(self)
63-
possible_types.find {|t| t.name == type_name}
62+
MySchema.types[type_name]
6463
}
6564
end
6665
```
@@ -77,18 +76,7 @@ MediaSearchResultUnion = GraphQL::UnionType.define do
7776
end
7877
```
7978

80-
In order to expose a union, you must also define how the concrete type of each object can be determined. `UnionType` provides a default, shown here:
81-
82-
```ruby
83-
MediaSearchResultUnion = GraphQL::UnionType.define do
84-
# This is the default if you don't provide a custom `resolve_type` proc:
85-
resolve_type -> (object, ctx) {
86-
type_name = object.class.name
87-
# You can access the union's `possible_types` inside the proc
88-
possible_types.find {|t| t.name == type_name}
89-
}
90-
end
91-
```
79+
In order to expose a union, you must also define how the concrete type of each object can be determined. This is defined with `Schema`'s `resolve_type` function (see Interface docs).
9280

9381
### Enum Types
9482

@@ -379,13 +367,13 @@ end
379367
Access `type.metadata` later:
380368

381369
```ruby
382-
SearchResultUnion = GraphQL::Union.define do
370+
MySchema = GraphQL::Schema.define do
383371
# ...
384372
# Use the type's declared `resolves_to_class_names`
385373
# to figure out if `obj` is a member of that type
386374
resolve_type -> (obj, ctx) {
387375
class_name = obj.class.name
388-
possible_types.find { |type| type.metadata[:resolves_to_class_names].include?(class_name) }
376+
MySchema.types.values.find { |type| type.metadata[:resolves_to_class_names].include?(class_name) }
389377
}
390378
end
391379
```

lib/graphql/base_type.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def legacy_resolve_type(object, ctx)
5656
if @resolve_type_proc
5757
resolve_type(object, ctx)
5858
else
59-
ctx.schema.resolve_type(object)
59+
ctx.schema.resolve_type(object, ctx)
6060
end
6161
end
6262

lib/graphql/object_type.rb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
module GraphQL
2-
# An object type has _fields_ which expose values from your application.
3-
# The fields are _typed_. Fields may return object types (including the same object type).
4-
#
5-
# See {GraphQL::Field} for details about defining fields.
6-
#
7-
# Objects which fulfill many of the same fields may implement _interfaces_, see {GraphQL::InterfaceType}.
8-
#
9-
# ObjectTypes which occur in the same place in the schema may be grouped in a {GraphQL::UnionType}.
2+
# This type exposes fields on an object.
103
#
114
# @example defining a type for your IMDB clone
125
# MovieType = GraphQL::ObjectType.define do

lib/graphql/schema.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,9 @@ def execution_strategy_for_operation(operation)
199199
# Determine the GraphQL type for a given object.
200200
# This is required for unions and interfaces (include Relay's node interface)
201201
# @return [GraphQL::ObjectType] The type for exposing `object` in GraphQL
202-
def resolve_type(object)
202+
def resolve_type(object, ctx)
203203
ensure_defined
204-
type_result = @resolve_type_proc.call(object)
204+
type_result = @resolve_type_proc.call(object, ctx)
205205
if type_result.nil?
206206
nil
207207
elsif !type_result.is_a?(GraphQL::BaseType)

spec/graphql/query/serial_execution/value_resolution_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
GraphQL::Schema.define do
3333
query(query_root)
34-
resolve_type -> (obj) { nil }
34+
resolve_type -> (obj, ctx) { nil }
3535
end
3636
}
3737

spec/graphql/schema_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@
2424
describe "#resolve_type" do
2525
describe "when the return value is nil" do
2626
it "returns nil" do
27-
result = StarWarsSchema.resolve_type(123)
27+
result = StarWarsSchema.resolve_type(123, nil)
2828
assert_equal(nil, result)
2929
end
3030
end
3131

3232
describe "when the return value is not a BaseType" do
3333
it "raises an error " do
3434
err = assert_raises(RuntimeError) {
35-
StarWarsSchema.resolve_type(:test_error)
35+
StarWarsSchema.resolve_type(:test_error, nil)
3636
}
3737
assert_includes err.message, "not_a_type (Symbol)"
3838
end

spec/support/dairy_app.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ def self.create(type:, data:)
342342

343343
rescue_from(NoSuchDairyError) { |err| err.message }
344344

345-
resolve_type -> (obj) {
345+
resolve_type -> (obj, ctx) {
346346
DummySchema.types[obj.class.name]
347347
}
348348
end

spec/support/star_wars_schema.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def upcased_parent_name
205205
query(QueryType)
206206
mutation(MutationType)
207207
node_identification(NodeIdentification)
208-
resolve_type -> (object) {
208+
resolve_type -> (object, ctx) {
209209
if object == :test_error
210210
:not_a_type
211211
elsif object.is_a?(Base)

0 commit comments

Comments
 (0)