Added TypeMap objecttype test. Fixed Field arguments

This commit is contained in:
Syrus Akbary 2016-08-13 13:38:05 -07:00
parent ec4a49498d
commit c339afc1ce
2 changed files with 68 additions and 30 deletions

View File

@ -4,8 +4,10 @@ from ..objecttype import ObjectType
from ..union import Union from ..union import Union
from ..enum import Enum from ..enum import Enum
from ..typemap import TypeMap from ..typemap import TypeMap
from ..scalars import String
from graphql.type import GraphQLEnumType, GraphQLEnumValue
from graphql.type import GraphQLEnumType, GraphQLEnumValue, GraphQLObjectType, GraphQLField, GraphQLArgument, GraphQLString
def test_enum(): def test_enum():
@ -34,3 +36,29 @@ def test_enum():
GraphQLEnumValue(name='foo', value=1, description='Description foo=1', deprecation_reason='Is deprecated'), GraphQLEnumValue(name='foo', value=1, description='Description foo=1', deprecation_reason='Is deprecated'),
GraphQLEnumValue(name='bar', value=2, description='Description bar=2'), GraphQLEnumValue(name='bar', value=2, description='Description bar=2'),
] ]
def test_objecttype():
class MyObjectType(ObjectType):
'''Description'''
foo = String(bar=String(description='Argument description', default_value='x'), description='Field description')
def resolve_foo(self, args, info):
return args.get('bar')
typemap = TypeMap([MyObjectType])
assert 'MyObjectType' in typemap
graphql_type = typemap['MyObjectType']
assert isinstance(graphql_type, GraphQLObjectType)
assert graphql_type.name == 'MyObjectType'
assert graphql_type.description == 'Description'
fields = graphql_type.get_fields()
assert 'foo' in fields
foo_field = fields['foo']
assert isinstance(foo_field, GraphQLField)
assert foo_field.description == 'Field description'
assert foo_field.resolver == MyObjectType.resolve_foo.__func__
assert foo_field.args == {
'bar': GraphQLArgument(GraphQLString, description='Argument description', default_value='x')
}

View File

@ -11,7 +11,7 @@ from .structures import List, NonNull
from .enum import Enum from .enum import Enum
from .scalars import Scalar, String, Boolean, Int, Float, ID from .scalars import Scalar, String, Boolean, Int, Float, ID
from graphql import GraphQLString, GraphQLField, GraphQLList, GraphQLBoolean, GraphQLInt, GraphQLFloat, GraphQLID, GraphQLNonNull, GraphQLInputObjectField from graphql import GraphQLString, GraphQLField, GraphQLList, GraphQLBoolean, GraphQLInt, GraphQLFloat, GraphQLID, GraphQLNonNull, GraphQLInputObjectField, GraphQLArgument
from graphql.type import GraphQLEnumValue from graphql.type import GraphQLEnumValue
@ -115,7 +115,32 @@ class TypeMap(GraphQLTypeMap):
map = cls.construct_interface(map, i) map = cls.construct_interface(map, i)
interfaces.append(map[i._meta.name]) interfaces.append(map[i._meta.name])
map[type._meta.name].interfaces = interfaces map[type._meta.name].interfaces = interfaces
map[type._meta.name].fields = cls.construct_fields_for_type(map, type) map[type._meta.name]._fields = cls.construct_fields_for_type(map, type)
return map
@classmethod
def construct_interface(cls, map, type):
from ..generators.definitions import GrapheneInterfaceType
map[type._meta.name] = GrapheneInterfaceType(
graphene_type=type,
name=type._meta.name,
description=type._meta.description,
fields=None,
resolve_type=type.resolve_type,
)
map[type._meta.name]._fields = cls.construct_fields_for_type(map, type)
return map
@classmethod
def construct_inputobjecttype(cls, map, type):
from ..generators.definitions import GrapheneInputObjectType
map[type._meta.name] = GrapheneInputObjectType(
graphene_type=type,
name=type._meta.name,
description=type._meta.description,
fields=None,
)
map[type._meta.name]._fields = cls.construct_fields_for_type(map, type, is_input_type=True)
return map return map
@classmethod @classmethod
@ -134,31 +159,6 @@ class TypeMap(GraphQLTypeMap):
map[type._meta.name].types = types map[type._meta.name].types = types
return map return map
@classmethod
def construct_interface(cls, map, type):
from ..generators.definitions import GrapheneInterfaceType
map[type._meta.name] = GrapheneInterfaceType(
graphene_type=type,
name=type._meta.name,
description=type._meta.description,
fields=None,
resolve_type=type.resolve_type,
)
map[type._meta.name].fields = cls.construct_fields_for_type(map, type)
return map
@classmethod
def construct_inputobjecttype(cls, map, type):
from ..generators.definitions import GrapheneInputObjectType
map[type._meta.name] = GrapheneInputObjectType(
graphene_type=type,
name=type._meta.name,
description=type._meta.description,
fields=None,
)
map[type._meta.name].fields = cls.construct_fields_for_type(map, type, is_input_type=True)
return map
@classmethod @classmethod
def construct_fields_for_type(cls, map, type, is_input_type=False): def construct_fields_for_type(cls, map, type, is_input_type=False):
fields = OrderedDict() fields = OrderedDict()
@ -175,11 +175,21 @@ class TypeMap(GraphQLTypeMap):
args = OrderedDict() args = OrderedDict()
for arg_name, arg in field.args.items(): for arg_name, arg in field.args.items():
map = cls.reducer(map, arg.type) map = cls.reducer(map, arg.type)
args[arg_name] = cls.get_field_type(map, arg.type) arg_type = cls.get_field_type(map, arg.type)
args[arg_name] = GraphQLArgument(
arg_type,
description=arg.description,
default_value=arg.default_value
)
resolver = field.resolver
resolver_type = getattr(type, 'resolve_{}'.format(name), None)
if resolver_type:
resolver = resolver_type.__func__
_field = GraphQLField( _field = GraphQLField(
field_type, field_type,
args=args, args=args,
resolver=field.resolver, resolver=resolver,
deprecation_reason=field.deprecation_reason, deprecation_reason=field.deprecation_reason,
description=field.description description=field.description
) )