Skip to content

Commit 6f2863e

Browse files
authored
Add some more tests for Interface (#1154)
1 parent 00e36b5 commit 6f2863e

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

graphene/types/tests/test_interface.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
from ..field import Field
22
from ..interface import Interface
3+
from ..objecttype import ObjectType
4+
from ..scalars import String
5+
from ..schema import Schema
36
from ..unmountedtype import UnmountedType
47

58

@@ -88,3 +91,109 @@ class MyInterface(MyAbstractType, Interface):
8891

8992
assert list(MyInterface._meta.fields) == ["field1", "field2"]
9093
assert [type(x) for x in MyInterface._meta.fields.values()] == [Field, Field]
94+
95+
96+
def test_resolve_type_default():
97+
class MyInterface(Interface):
98+
field2 = String()
99+
100+
class MyTestType(ObjectType):
101+
class Meta:
102+
interfaces = (MyInterface,)
103+
104+
class Query(ObjectType):
105+
test = Field(MyInterface)
106+
107+
def resolve_test(_, info):
108+
return MyTestType()
109+
110+
schema = Schema(query=Query, types=[MyTestType])
111+
112+
result = schema.execute(
113+
"""
114+
query {
115+
test {
116+
__typename
117+
}
118+
}
119+
"""
120+
)
121+
assert not result.errors
122+
assert result.data == {"test": {"__typename": "MyTestType"}}
123+
124+
125+
def test_resolve_type_custom():
126+
class MyInterface(Interface):
127+
field2 = String()
128+
129+
@classmethod
130+
def resolve_type(cls, instance, info):
131+
if instance["type"] == 1:
132+
return MyTestType1
133+
return MyTestType2
134+
135+
class MyTestType1(ObjectType):
136+
class Meta:
137+
interfaces = (MyInterface,)
138+
139+
class MyTestType2(ObjectType):
140+
class Meta:
141+
interfaces = (MyInterface,)
142+
143+
class Query(ObjectType):
144+
test = Field(MyInterface)
145+
146+
def resolve_test(_, info):
147+
return {"type": 1}
148+
149+
schema = Schema(query=Query, types=[MyTestType1, MyTestType2])
150+
151+
result = schema.execute(
152+
"""
153+
query {
154+
test {
155+
__typename
156+
}
157+
}
158+
"""
159+
)
160+
assert not result.errors
161+
assert result.data == {"test": {"__typename": "MyTestType1"}}
162+
163+
164+
def test_resolve_type_custom_interferes():
165+
class MyInterface(Interface):
166+
field2 = String()
167+
type_ = String(name="type")
168+
169+
def resolve_type_(_, info):
170+
return "foo"
171+
172+
class MyTestType1(ObjectType):
173+
class Meta:
174+
interfaces = (MyInterface,)
175+
176+
class MyTestType2(ObjectType):
177+
class Meta:
178+
interfaces = (MyInterface,)
179+
180+
class Query(ObjectType):
181+
test = Field(MyInterface)
182+
183+
def resolve_test(_, info):
184+
return MyTestType1()
185+
186+
schema = Schema(query=Query, types=[MyTestType1, MyTestType2])
187+
188+
result = schema.execute(
189+
"""
190+
query {
191+
test {
192+
__typename
193+
type
194+
}
195+
}
196+
"""
197+
)
198+
assert not result.errors
199+
assert result.data == {"test": {"__typename": "MyTestType1", "type": "foo"}}

0 commit comments

Comments
 (0)