From c339afc1cea5631b9399cc0749aaf63913352cc1 Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Sat, 13 Aug 2016 13:38:05 -0700 Subject: [PATCH] Added TypeMap objecttype test. Fixed Field arguments --- graphene/new_types/tests/test_typemap.py | 30 ++++++++++- graphene/new_types/typemap.py | 68 ++++++++++++++---------- 2 files changed, 68 insertions(+), 30 deletions(-) diff --git a/graphene/new_types/tests/test_typemap.py b/graphene/new_types/tests/test_typemap.py index 7a47edbf..a5ceb497 100644 --- a/graphene/new_types/tests/test_typemap.py +++ b/graphene/new_types/tests/test_typemap.py @@ -4,8 +4,10 @@ from ..objecttype import ObjectType from ..union import Union from ..enum import Enum 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(): @@ -34,3 +36,29 @@ def test_enum(): GraphQLEnumValue(name='foo', value=1, description='Description foo=1', deprecation_reason='Is deprecated'), 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') + } diff --git a/graphene/new_types/typemap.py b/graphene/new_types/typemap.py index 918f216a..1f09871c 100644 --- a/graphene/new_types/typemap.py +++ b/graphene/new_types/typemap.py @@ -11,7 +11,7 @@ from .structures import List, NonNull from .enum import Enum 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 @@ -115,7 +115,32 @@ class TypeMap(GraphQLTypeMap): map = cls.construct_interface(map, i) interfaces.append(map[i._meta.name]) 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 @classmethod @@ -134,31 +159,6 @@ class TypeMap(GraphQLTypeMap): map[type._meta.name].types = types 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 def construct_fields_for_type(cls, map, type, is_input_type=False): fields = OrderedDict() @@ -175,11 +175,21 @@ class TypeMap(GraphQLTypeMap): args = OrderedDict() for arg_name, arg in field.args.items(): 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_type, args=args, - resolver=field.resolver, + resolver=resolver, deprecation_reason=field.deprecation_reason, description=field.description )