Skip to content

Commit 822b030

Browse files
committed
Added tests for dynamic field and make more consistent.
1 parent 88ccaec commit 822b030

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

graphene/types/tests/test_dynamic.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from ..structures import List, NonNull
2+
from ..scalars import String
3+
from ..dynamic import Dynamic
4+
5+
6+
def test_dynamic():
7+
dynamic = Dynamic(lambda: String)
8+
assert dynamic.get_type() == String
9+
assert str(dynamic.get_type()) == 'String'
10+
11+
12+
def test_nonnull():
13+
dynamic = Dynamic(lambda: NonNull(String))
14+
assert dynamic.get_type().of_type == String
15+
assert str(dynamic.get_type()) == 'String!'
16+
17+
18+
def test_list():
19+
dynamic = Dynamic(lambda: List(String))
20+
assert dynamic.get_type().of_type == String
21+
assert str(dynamic.get_type()) == '[String]'
22+
23+
24+
def test_list_non_null():
25+
dynamic = Dynamic(lambda: List(NonNull(String)))
26+
assert dynamic.get_type().of_type.of_type == String
27+
assert str(dynamic.get_type()) == '[String!]'

graphene/types/tests/test_query.py

+14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from ..scalars import Int, String
1111
from ..schema import Schema
1212
from ..structures import List
13+
from ..dynamic import Dynamic
1314

1415

1516
def test_query():
@@ -23,6 +24,19 @@ class Query(ObjectType):
2324
assert executed.data == {'hello': 'World'}
2425

2526

27+
def test_query_dynamic():
28+
class Query(ObjectType):
29+
hello = Dynamic(lambda: String(resolver=lambda *_: 'World'))
30+
hellos = Dynamic(lambda: List(String, resolver=lambda *_: ['Worlds']))
31+
hello_field = Dynamic(lambda: Field(String, resolver=lambda *_: 'Field World'))
32+
33+
hello_schema = Schema(Query)
34+
35+
executed = hello_schema.execute('{ hello hellos helloField }')
36+
assert not executed.errors
37+
assert executed.data == {'hello': 'World', 'hellos': ['Worlds'], 'helloField': 'Field World'}
38+
39+
2640
def test_query_default_value():
2741
class MyType(ObjectType):
2842
field = String()

graphene/types/typemap.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
from ..utils.get_unbound_function import get_unbound_function
1414
from .dynamic import Dynamic
1515
from .enum import Enum
16+
from .field import Field
1617
from .inputobjecttype import InputObjectType
1718
from .interface import Interface
1819
from .objecttype import ObjectType
1920
from .scalars import ID, Boolean, Float, Int, Scalar, String
2021
from .structures import List, NonNull
2122
from .union import Union
23+
from .utils import get_field_as
2224

2325

2426
def is_graphene_type(_type):
@@ -202,7 +204,7 @@ def construct_fields_for_type(self, map, type, is_input_type=False):
202204
fields = OrderedDict()
203205
for name, field in type._meta.fields.items():
204206
if isinstance(field, Dynamic):
205-
field = field.get_type()
207+
field = get_field_as(field.get_type(), _as=Field)
206208
if not field:
207209
continue
208210
map = self.reducer(map, field.type)

0 commit comments

Comments
 (0)