mirror of
https://github.com/graphql-python/graphene.git
synced 2025-02-02 20:54:16 +03:00
Added tests for dynamic field and make more consistent.
This commit is contained in:
parent
88ccaec8fa
commit
822b030938
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!]'
|
|
@ -10,6 +10,7 @@ from ..objecttype import ObjectType
|
||||||
from ..scalars import Int, String
|
from ..scalars import Int, String
|
||||||
from ..schema import Schema
|
from ..schema import Schema
|
||||||
from ..structures import List
|
from ..structures import List
|
||||||
|
from ..dynamic import Dynamic
|
||||||
|
|
||||||
|
|
||||||
def test_query():
|
def test_query():
|
||||||
|
@ -23,6 +24,19 @@ def test_query():
|
||||||
assert executed.data == {'hello': 'World'}
|
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():
|
def test_query_default_value():
|
||||||
class MyType(ObjectType):
|
class MyType(ObjectType):
|
||||||
field = String()
|
field = String()
|
||||||
|
|
|
@ -13,12 +13,14 @@ from ..utils.str_converters import to_camel_case
|
||||||
from ..utils.get_unbound_function import get_unbound_function
|
from ..utils.get_unbound_function import get_unbound_function
|
||||||
from .dynamic import Dynamic
|
from .dynamic import Dynamic
|
||||||
from .enum import Enum
|
from .enum import Enum
|
||||||
|
from .field import Field
|
||||||
from .inputobjecttype import InputObjectType
|
from .inputobjecttype import InputObjectType
|
||||||
from .interface import Interface
|
from .interface import Interface
|
||||||
from .objecttype import ObjectType
|
from .objecttype import ObjectType
|
||||||
from .scalars import ID, Boolean, Float, Int, Scalar, String
|
from .scalars import ID, Boolean, Float, Int, Scalar, String
|
||||||
from .structures import List, NonNull
|
from .structures import List, NonNull
|
||||||
from .union import Union
|
from .union import Union
|
||||||
|
from .utils import get_field_as
|
||||||
|
|
||||||
|
|
||||||
def is_graphene_type(_type):
|
def is_graphene_type(_type):
|
||||||
|
@ -202,7 +204,7 @@ class TypeMap(GraphQLTypeMap):
|
||||||
fields = OrderedDict()
|
fields = OrderedDict()
|
||||||
for name, field in type._meta.fields.items():
|
for name, field in type._meta.fields.items():
|
||||||
if isinstance(field, Dynamic):
|
if isinstance(field, Dynamic):
|
||||||
field = field.get_type()
|
field = get_field_as(field.get_type(), _as=Field)
|
||||||
if not field:
|
if not field:
|
||||||
continue
|
continue
|
||||||
map = self.reducer(map, field.type)
|
map = self.reducer(map, field.type)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user