Skip to content

Commit ba0655c

Browse files
committed
Support super in interfaces
1 parent f97d3fb commit ba0655c

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

lib/graphql/schema/interface.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ def included(child_class)
2929
child_class.own_interfaces.each do |interface_defn|
3030
child_class.extend(interface_defn::DefinitionMethods)
3131
end
32+
# Also, prepare a place for default field implementations
33+
default_resolve_module = Module.new
34+
child_class.const_set(:DefaultResolve, default_resolve_module)
35+
child_class.include(default_resolve_module)
3236
elsif child_class < GraphQL::Schema::Object
3337
# This is being included into an object type, make sure it's using `implements(...)`
3438
backtrace_line = caller(0, 10).find { |line| line.include?("schema/object.rb") && line.include?("in `implements'")}

spec/graphql/schema/object_spec.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
assert_equal "Ensemble", object_class.graphql_name
99
assert_equal "A group of musicians playing together", object_class.description
1010
assert_equal 6, object_class.fields.size
11-
assert_equal 2, object_class.interfaces.size
11+
assert_equal 3, object_class.interfaces.size
1212
# Compatibility methods are delegated to the underlying BaseType
1313
assert object_class.respond_to?(:connection_type)
1414
end
@@ -22,7 +22,7 @@
2222
# one more than the parent class
2323
assert_equal 7, new_object_class.fields.size
2424
# inherited interfaces are present
25-
assert_equal 2, new_object_class.interfaces.size
25+
assert_equal 3, new_object_class.interfaces.size
2626
# The new field is present
2727
assert new_object_class.fields.key?("newField")
2828
# The overridden field is present:
@@ -54,7 +54,8 @@
5454
GRAPHQL
5555
res = Jazz::Schema.execute(query_str)
5656
ensemble = res["data"]["hashyEnsemble"]
57-
assert_equal ["Jerry Garcia"], ensemble["musicians"].map { |m| m["name"] }
57+
# This also tests `super` in an interface field
58+
assert_equal ["Jerry Garcia", "Phil Lesh"], ensemble["musicians"].map { |m| m["name"] }
5859
assert_equal "May 5, 1965", ensemble["formedAt"]
5960
end
6061

spec/support/jazz.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,25 @@ def upcase_name
166166
end
167167
end
168168

169+
module HasMusicians
170+
include BaseInterface
171+
field :musicians, "[Jazz::Musician]", null: false
172+
173+
def musicians
174+
# Test that super works
175+
super + [Models::Musician.new("Phil Lesh")]
176+
end
177+
end
178+
179+
169180
# Here's a new-style GraphQL type definition
170181
class Ensemble < ObjectWithUpcasedName
171182
# Test string type names
172183
# This method should override inherited one
173184
field :name, "String", null: false, method: :overridden_name
174-
implements GloballyIdentifiableType, NamedEntity
185+
implements GloballyIdentifiableType, NamedEntity, HasMusicians
175186
description "A group of musicians playing together"
176187
config :config, :configged
177-
field :musicians, "[Jazz::Musician]", null: false
178188
field :formed_at, String, null: true, hash_key: "formedAtDate"
179189

180190
def overridden_name

0 commit comments

Comments
 (0)