Skip to content

Commit f8e636d

Browse files
authored
Merge pull request #324 from Globegitter/add-dynamic-tests
Added tests for dynamic field and make more consistent.
2 parents 5cce7de + 0408591 commit f8e636d

File tree

5 files changed

+57
-4
lines changed

5 files changed

+57
-4
lines changed

graphene/types/argument.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from ..utils.orderedtype import OrderedType
55
from .structures import NonNull
6+
from .dynamic import Dynamic
67

78

89
class Argument(OrderedType):
@@ -33,6 +34,9 @@ def to_arguments(args, extra_args):
3334
iter_arguments = chain(args.items(), extra_args)
3435
arguments = OrderedDict()
3536
for default_name, arg in iter_arguments:
37+
if isinstance(arg, Dynamic):
38+
arg = arg.get_type()
39+
3640
if isinstance(arg, UnmountedType):
3741
arg = arg.Argument()
3842

graphene/types/tests/test_dynamic.py

Lines changed: 27 additions & 0 deletions
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_mutation.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from ..objecttype import ObjectType
55
from ..schema import Schema
66
from ..scalars import String
7+
from ..dynamic import Dynamic
78

89

910
def test_generate_mutation_no_args():
@@ -47,12 +48,15 @@ def test_mutation_execution():
4748
class CreateUser(Mutation):
4849
class Input:
4950
name = String()
51+
dynamic = Dynamic(lambda: String())
5052

5153
name = String()
54+
dynamic = Dynamic(lambda: String())
5255

5356
def mutate(self, args, context, info):
5457
name = args.get('name')
55-
return CreateUser(name=name)
58+
dynamic = args.get('dynamic')
59+
return CreateUser(name=name, dynamic=dynamic)
5660

5761
class Query(ObjectType):
5862
a = String()
@@ -62,14 +66,16 @@ class MyMutation(ObjectType):
6266

6367
schema = Schema(query=Query, mutation=MyMutation)
6468
result = schema.execute(''' mutation mymutation {
65-
createUser(name:"Peter") {
69+
createUser(name:"Peter", dynamic: "dynamic") {
6670
name
71+
dynamic
6772
}
6873
}
6974
''')
7075
assert not result.errors
7176
assert result.data == {
7277
'createUser': {
73-
'name': "Peter"
78+
'name': 'Peter',
79+
'dynamic': 'dynamic',
7480
}
7581
}

graphene/types/tests/test_query.py

Lines changed: 14 additions & 0 deletions
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

Lines changed: 3 additions & 1 deletion
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)