Skip to content

Commit c77d87d

Browse files
authored
Merge pull request #1304 from closeio/support-interface-implementations
Add support for interfaces on interfaces
2 parents 8bdcec5 + 7004515 commit c77d87d

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

graphene/types/interface.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
# For static type checking with Mypy
66
MYPY = False
77
if MYPY:
8-
from typing import Dict # NOQA
8+
from typing import Dict, Iterable, Type # NOQA
99

1010

1111
class InterfaceOptions(BaseOptions):
1212
fields = None # type: Dict[str, Field]
13+
interfaces = () # type: Iterable[Type[Interface]]
1314

1415

1516
class Interface(BaseType):
@@ -45,7 +46,7 @@ class Meta:
4546
"""
4647

4748
@classmethod
48-
def __init_subclass_with_meta__(cls, _meta=None, **options):
49+
def __init_subclass_with_meta__(cls, _meta=None, interfaces=(), **options):
4950
if not _meta:
5051
_meta = InterfaceOptions(cls)
5152

@@ -58,6 +59,9 @@ def __init_subclass_with_meta__(cls, _meta=None, **options):
5859
else:
5960
_meta.fields = fields
6061

62+
if not _meta.interfaces:
63+
_meta.interfaces = interfaces
64+
6165
super(Interface, cls).__init_subclass_with_meta__(_meta=_meta, **options)
6266

6367
@classmethod

graphene/types/schema.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,20 @@ def create_interface(self, graphene_type):
233233
else None
234234
)
235235

236+
def interfaces():
237+
interfaces = []
238+
for graphene_interface in graphene_type._meta.interfaces:
239+
interface = self.add_type(graphene_interface)
240+
assert interface.graphene_type == graphene_interface
241+
interfaces.append(interface)
242+
return interfaces
243+
236244
return GrapheneInterfaceType(
237245
graphene_type=graphene_type,
238246
name=graphene_type._meta.name,
239247
description=graphene_type._meta.description,
240248
fields=partial(self.create_fields_for_type, graphene_type),
249+
interfaces=interfaces,
241250
resolve_type=resolve_type,
242251
)
243252

graphene/types/tests/test_interface.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,18 @@ class MyInterface(Interface):
2525

2626

2727
def test_generate_interface_with_meta():
28+
class MyFirstInterface(Interface):
29+
pass
30+
2831
class MyInterface(Interface):
2932
class Meta:
3033
name = "MyOtherInterface"
3134
description = "Documentation"
35+
interfaces = [MyFirstInterface]
3236

3337
assert MyInterface._meta.name == "MyOtherInterface"
3438
assert MyInterface._meta.description == "Documentation"
39+
assert MyInterface._meta.interfaces == [MyFirstInterface]
3540

3641

3742
def test_generate_interface_with_fields():

graphene/types/tests/test_type_map.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,33 @@ class Meta:
289289
assert graphql_type.is_type_of
290290
assert graphql_type.is_type_of({}, None) is True
291291
assert graphql_type.is_type_of(MyObjectType(), None) is False
292+
293+
294+
def test_interface_with_interfaces():
295+
class FooInterface(Interface):
296+
foo = String()
297+
298+
class BarInterface(Interface):
299+
class Meta:
300+
interfaces = [FooInterface]
301+
302+
foo = String()
303+
bar = String()
304+
305+
type_map = create_type_map([FooInterface, BarInterface])
306+
assert "FooInterface" in type_map
307+
foo_graphql_type = type_map["FooInterface"]
308+
assert isinstance(foo_graphql_type, GraphQLInterfaceType)
309+
assert foo_graphql_type.name == "FooInterface"
310+
311+
assert "BarInterface" in type_map
312+
bar_graphql_type = type_map["BarInterface"]
313+
assert isinstance(bar_graphql_type, GraphQLInterfaceType)
314+
assert bar_graphql_type.name == "BarInterface"
315+
316+
fields = bar_graphql_type.fields
317+
assert list(fields) == ["foo", "bar"]
318+
assert isinstance(fields["foo"], GraphQLField)
319+
assert isinstance(fields["bar"], GraphQLField)
320+
321+
assert bar_graphql_type.interfaces == [foo_graphql_type]

0 commit comments

Comments
 (0)