mirror of
https://github.com/graphql-python/graphene.git
synced 2025-02-08 23:50:38 +03:00
Merge pull request #324 from Globegitter/add-dynamic-tests
Added tests for dynamic field and make more consistent.
This commit is contained in:
commit
f8e636db80
|
@ -3,6 +3,7 @@ from itertools import chain
|
|||
|
||||
from ..utils.orderedtype import OrderedType
|
||||
from .structures import NonNull
|
||||
from .dynamic import Dynamic
|
||||
|
||||
|
||||
class Argument(OrderedType):
|
||||
|
@ -33,6 +34,9 @@ def to_arguments(args, extra_args):
|
|||
iter_arguments = chain(args.items(), extra_args)
|
||||
arguments = OrderedDict()
|
||||
for default_name, arg in iter_arguments:
|
||||
if isinstance(arg, Dynamic):
|
||||
arg = arg.get_type()
|
||||
|
||||
if isinstance(arg, UnmountedType):
|
||||
arg = arg.Argument()
|
||||
|
||||
|
|
27
graphene/types/tests/test_dynamic.py
Normal file
27
graphene/types/tests/test_dynamic.py
Normal file
|
@ -0,0 +1,27 @@
|
|||
from ..structures import List, NonNull
|
||||
from ..scalars import String
|
||||
from ..dynamic import Dynamic
|
||||
|
||||
|
||||
def test_dynamic():
|
||||
dynamic = Dynamic(lambda: String)
|
||||
assert dynamic.get_type() == String
|
||||
assert str(dynamic.get_type()) == 'String'
|
||||
|
||||
|
||||
def test_nonnull():
|
||||
dynamic = Dynamic(lambda: NonNull(String))
|
||||
assert dynamic.get_type().of_type == String
|
||||
assert str(dynamic.get_type()) == 'String!'
|
||||
|
||||
|
||||
def test_list():
|
||||
dynamic = Dynamic(lambda: List(String))
|
||||
assert dynamic.get_type().of_type == String
|
||||
assert str(dynamic.get_type()) == '[String]'
|
||||
|
||||
|
||||
def test_list_non_null():
|
||||
dynamic = Dynamic(lambda: List(NonNull(String)))
|
||||
assert dynamic.get_type().of_type.of_type == String
|
||||
assert str(dynamic.get_type()) == '[String!]'
|
|
@ -4,6 +4,7 @@ from ..mutation import Mutation
|
|||
from ..objecttype import ObjectType
|
||||
from ..schema import Schema
|
||||
from ..scalars import String
|
||||
from ..dynamic import Dynamic
|
||||
|
||||
|
||||
def test_generate_mutation_no_args():
|
||||
|
@ -47,12 +48,15 @@ def test_mutation_execution():
|
|||
class CreateUser(Mutation):
|
||||
class Input:
|
||||
name = String()
|
||||
dynamic = Dynamic(lambda: String())
|
||||
|
||||
name = String()
|
||||
dynamic = Dynamic(lambda: String())
|
||||
|
||||
def mutate(self, args, context, info):
|
||||
name = args.get('name')
|
||||
return CreateUser(name=name)
|
||||
dynamic = args.get('dynamic')
|
||||
return CreateUser(name=name, dynamic=dynamic)
|
||||
|
||||
class Query(ObjectType):
|
||||
a = String()
|
||||
|
@ -62,14 +66,16 @@ def test_mutation_execution():
|
|||
|
||||
schema = Schema(query=Query, mutation=MyMutation)
|
||||
result = schema.execute(''' mutation mymutation {
|
||||
createUser(name:"Peter") {
|
||||
createUser(name:"Peter", dynamic: "dynamic") {
|
||||
name
|
||||
dynamic
|
||||
}
|
||||
}
|
||||
''')
|
||||
assert not result.errors
|
||||
assert result.data == {
|
||||
'createUser': {
|
||||
'name': "Peter"
|
||||
'name': 'Peter',
|
||||
'dynamic': 'dynamic',
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@ from ..objecttype import ObjectType
|
|||
from ..scalars import Int, String
|
||||
from ..schema import Schema
|
||||
from ..structures import List
|
||||
from ..dynamic import Dynamic
|
||||
|
||||
|
||||
def test_query():
|
||||
|
@ -23,6 +24,19 @@ def test_query():
|
|||
assert executed.data == {'hello': 'World'}
|
||||
|
||||
|
||||
def test_query_dynamic():
|
||||
class Query(ObjectType):
|
||||
hello = Dynamic(lambda: String(resolver=lambda *_: 'World'))
|
||||
hellos = Dynamic(lambda: List(String, resolver=lambda *_: ['Worlds']))
|
||||
hello_field = Dynamic(lambda: Field(String, resolver=lambda *_: 'Field World'))
|
||||
|
||||
hello_schema = Schema(Query)
|
||||
|
||||
executed = hello_schema.execute('{ hello hellos helloField }')
|
||||
assert not executed.errors
|
||||
assert executed.data == {'hello': 'World', 'hellos': ['Worlds'], 'helloField': 'Field World'}
|
||||
|
||||
|
||||
def test_query_default_value():
|
||||
class MyType(ObjectType):
|
||||
field = String()
|
||||
|
|
|
@ -13,12 +13,14 @@ from ..utils.str_converters import to_camel_case
|
|||
from ..utils.get_unbound_function import get_unbound_function
|
||||
from .dynamic import Dynamic
|
||||
from .enum import Enum
|
||||
from .field import Field
|
||||
from .inputobjecttype import InputObjectType
|
||||
from .interface import Interface
|
||||
from .objecttype import ObjectType
|
||||
from .scalars import ID, Boolean, Float, Int, Scalar, String
|
||||
from .structures import List, NonNull
|
||||
from .union import Union
|
||||
from .utils import get_field_as
|
||||
|
||||
|
||||
def is_graphene_type(_type):
|
||||
|
@ -202,7 +204,7 @@ class TypeMap(GraphQLTypeMap):
|
|||
fields = OrderedDict()
|
||||
for name, field in type._meta.fields.items():
|
||||
if isinstance(field, Dynamic):
|
||||
field = field.get_type()
|
||||
field = get_field_as(field.get_type(), _as=Field)
|
||||
if not field:
|
||||
continue
|
||||
map = self.reducer(map, field.type)
|
||||
|
|
Loading…
Reference in New Issue
Block a user