diff --git a/graphene/types/tests/test_interface.py b/graphene/types/tests/test_interface.py index b524296b..3ee5503c 100644 --- a/graphene/types/tests/test_interface.py +++ b/graphene/types/tests/test_interface.py @@ -1,5 +1,7 @@ from ..field import Field from ..interface import Interface +from ..objecttype import ObjectType +from ..typemap import TypeMap, resolve_type from ..unmountedtype import UnmountedType @@ -88,3 +90,32 @@ def test_generate_interface_inherit_abstracttype_reversed(): assert list(MyInterface._meta.fields.keys()) == ["field1", "field2"] assert [type(x) for x in MyInterface._meta.fields.values()] == [Field, Field] + + +def test_generate_interface_with_resolve_type(): + class MyInterface(Interface): + field1 = MyScalar() + + @classmethod + def resolve_type(cls, data, info): + return MyInterfaceObject + + class MyInterfaceType(ObjectType): + field_other = MyScalar() + class Meta: + interfaces = (MyInterface,) + + class MyObject(ObjectType): + field2 = Field(MyInterface) + + assert list(MyInterface._meta.fields.keys()) == ["field1"] + assert [type(x) for x in MyInterface._meta.fields.values()] == [Field] + + assert list(MyInterfaceType._meta.fields.keys()) == ["field1", "field_other"] + assert [type(x) for x in MyInterfaceType._meta.fields.values()] == [Field, Field] + + assert list(MyObject._meta.fields.keys()) == ["field2"] + assert [type(x) for x in MyObject._meta.fields.values()] == [Field] + + field = [x for x in MyObject._meta.fields.values()][0] + assert field.type == MyInterfaceType diff --git a/graphene/types/tests/test_query.py b/graphene/types/tests/test_query.py index 8681e462..b1e9a31d 100644 --- a/graphene/types/tests/test_query.py +++ b/graphene/types/tests/test_query.py @@ -127,6 +127,50 @@ def test_query_interface(): } +def test_query_interface_resolve_type(): + class one_object(object): + pass + + class two_object(object): + pass + + class MyInterface(Interface): + base = String() + + @classmethod + def resolve_type(cls, data, info): + if isinstance(data, one_object): + return One + if isinstance(data, two_object): + return Two + + class One(ObjectType): + class Meta: + interfaces = (MyInterface,) + + one = String() + + class Two(ObjectType): + class Meta: + interfaces = (MyInterface,) + + two = String() + + class Query(ObjectType): + interfaces = List(MyInterface) + + def resolve_interfaces(self, info): + return [one_object(), two_object()] + + hello_schema = Schema(Query, types=[One, Two]) + + executed = hello_schema.execute("{ interfaces { __typename } }") + assert not executed.errors + assert executed.data == { + "interfaces": [{"__typename": "One"}, {"__typename": "Two"}] + } + + def test_query_dynamic(): class Query(ObjectType): hello = Dynamic(lambda: String(resolver=lambda *_: "World"))