8000 resolve_type: Tests for Interface by tony · Pull Request #868 · graphql-python/graphene · GitHub
[go: up one dir, main page]

Skip to content

resolve_type: Tests for Interface #868

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions graphene/types/tests/test_interface.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -88,3 +90,32 @@ class MyInterface(MyAbstractType, Interface):

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
44 changes: 44 additions & 0 deletions graphene/types/tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,50 @@ def resolve_interfaces(self, info):
}


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"))
Expand Down
0