From ae93499a37bdd9f7099b3c645ae36b7ed658f205 Mon Sep 17 00:00:00 2001 From: Alec Rosenbaum Date: Fri, 15 Jan 2021 13:01:43 -0500 Subject: [PATCH 1/4] add failing test for interface meta --- graphene/types/tests/test_interface.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/graphene/types/tests/test_interface.py b/graphene/types/tests/test_interface.py index c30a8a3af..3dd4fc4fb 100644 --- a/graphene/types/tests/test_interface.py +++ b/graphene/types/tests/test_interface.py @@ -25,13 +25,18 @@ class MyInterface(Interface): def test_generate_interface_with_meta(): + class MyFirstInterface(Interface): + pass + class MyInterface(Interface): class Meta: name = "MyOtherInterface" description = "Documentation" + interfaces = [MyFirstInterface] assert MyInterface._meta.name == "MyOtherInterface" assert MyInterface._meta.description == "Documentation" + assert MyInterface._meta.interfaces == [MyFirstInterface] def test_generate_interface_with_fields(): From 86b7e6ac86a8a3095ef0bb4164c84cf727f4d9b8 Mon Sep 17 00:00:00 2001 From: Alec Rosenbaum Date: Fri, 15 Jan 2021 13:02:08 -0500 Subject: [PATCH 2/4] update InterfaceOptions to fix failing test --- graphene/types/interface.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/graphene/types/interface.py b/graphene/types/interface.py index 77086dab0..6503b78b3 100644 --- a/graphene/types/interface.py +++ b/graphene/types/interface.py @@ -5,11 +5,12 @@ # For static type checking with Mypy MYPY = False if MYPY: - from typing import Dict # NOQA + from typing import Dict, Iterable, Type # NOQA class InterfaceOptions(BaseOptions): fields = None # type: Dict[str, Field] + interfaces = () # type: Iterable[Type[Interface]] class Interface(BaseType): @@ -45,7 +46,7 @@ class Meta: """ @classmethod - def __init_subclass_with_meta__(cls, _meta=None, **options): + def __init_subclass_with_meta__(cls, _meta=None, interfaces=(), **options): if not _meta: _meta = InterfaceOptions(cls) @@ -58,6 +59,9 @@ def __init_subclass_with_meta__(cls, _meta=None, **options): else: _meta.fields = fields + if not _meta.interfaces: + _meta.interfaces = interfaces + super(Interface, cls).__init_subclass_with_meta__(_meta=_meta, **options) @classmethod From a17f63cf039939242167dbff46b6b9f24ac04802 Mon Sep 17 00:00:00 2001 From: Alec Rosenbaum Date: Fri, 15 Jan 2021 13:10:14 -0500 Subject: [PATCH 3/4] add failing type_map test, bar_graphql_type has no interfaces --- graphene/types/tests/test_type_map.py | 30 +++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/graphene/types/tests/test_type_map.py b/graphene/types/tests/test_type_map.py index 334eb2415..cc1992ac3 100644 --- a/graphene/types/tests/test_type_map.py +++ b/graphene/types/tests/test_type_map.py @@ -270,3 +270,33 @@ class Meta: assert graphql_type.is_type_of assert graphql_type.is_type_of({}, None) is True assert graphql_type.is_type_of(MyObjectType(), None) is False + + +def test_interface_with_interfaces(): + class FooInterface(Interface): + foo = String() + + class BarInterface(Interface): + class Meta: + interfaces = [FooInterface] + + foo = String() + bar = String() + + type_map = create_type_map([FooInterface, BarInterface]) + assert "FooInterface" in type_map + foo_graphql_type = type_map["FooInterface"] + assert isinstance(foo_graphql_type, GraphQLInterfaceType) + assert foo_graphql_type.name == "FooInterface" + + assert "BarInterface" in type_map + bar_graphql_type = type_map["BarInterface"] + assert isinstance(bar_graphql_type, GraphQLInterfaceType) + assert bar_graphql_type.name == "BarInterface" + + fields = bar_graphql_type.fields + assert list(fields) == ["foo", "bar"] + assert isinstance(fields["foo"], GraphQLField) + assert isinstance(fields["bar"], GraphQLField) + + assert bar_graphql_type.interfaces == [foo_graphql_type] From 7004515f06024227f3e6068150fc5d275b0c4392 Mon Sep 17 00:00:00 2001 From: Alec Rosenbaum Date: Fri, 15 Jan 2021 13:13:05 -0500 Subject: [PATCH 4/4] implement interface interfaces on TypeMap, fix failing test --- graphene/types/schema.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/graphene/types/schema.py b/graphene/types/schema.py index 4fd71769c..8f27c9b9c 100644 --- a/graphene/types/schema.py +++ b/graphene/types/schema.py @@ -236,11 +236,20 @@ def create_interface(self, graphene_type): else None ) + def interfaces(): + interfaces = [] + for graphene_interface in graphene_type._meta.interfaces: + interface = self.add_type(graphene_interface) + assert interface.graphene_type == graphene_interface + interfaces.append(interface) + return interfaces + return GrapheneInterfaceType( graphene_type=graphene_type, name=graphene_type._meta.name, description=graphene_type._meta.description, fields=partial(self.create_fields_for_type, graphene_type), + interfaces=interfaces, resolve_type=resolve_type, )