From 6543f9272ae53a0f42594d6a222f7a5bf646bb7c Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Mon, 22 Aug 2016 22:08:49 -0700 Subject: [PATCH] Updated input field names with out_name --- graphene/types/tests/test_query.py | 54 ++++++++++++++++++++++++++++ graphene/types/tests/test_typemap.py | 12 +++---- graphene/types/typemap.py | 6 ++-- 3 files changed, 64 insertions(+), 8 deletions(-) diff --git a/graphene/types/tests/test_query.py b/graphene/types/tests/test_query.py index a8c21987..dd6de01c 100644 --- a/graphene/types/tests/test_query.py +++ b/graphene/types/tests/test_query.py @@ -1,8 +1,11 @@ +import json from functools import partial from graphql import execute, Source, parse from ..objecttype import ObjectType +from ..inputfield import InputField +from ..inputobjecttype import InputObjectType from ..scalars import String, Int from ..schema import Schema from ..structures import List @@ -33,6 +36,57 @@ def test_query_resolve_function(): assert executed.data == {'hello': 'World'} +def test_query_arguments(): + class Query(ObjectType): + test = String(a_str=String(), a_int=Int()) + + def resolve_test(self, args, context, info): + return json.dumps([self, args], separators=(',', ':')) + + test_schema = Schema(Query) + + result = test_schema.execute('{ test }', None) + assert not result.errors + assert result.data == {'test': '[null,{}]'} + + result = test_schema.execute('{ test(aStr: "String!") }', 'Source!') + assert not result.errors + assert result.data == {'test': '["Source!",{"a_str":"String!"}]'} + + result = test_schema.execute('{ test(aInt: -123, aStr: "String!") }', 'Source!') + assert not result.errors + assert result.data in [ + {'test': '["Source!",{"a_str":"String!","a_int":-123}]'}, + {'test': '["Source!",{"a_int":-123,"a_str":"String!"}]'} + ] + + +def test_query_input_field(): + class Input(InputObjectType): + a_field = String() + recursive_field = InputField(lambda: Input) + + class Query(ObjectType): + test = String(a_input=Input()) + + def resolve_test(self, args, context, info): + return json.dumps([self, args], separators=(',', ':')) + + test_schema = Schema(Query) + + result = test_schema.execute('{ test }', None) + assert not result.errors + assert result.data == {'test': '[null,{}]'} + + result = test_schema.execute('{ test(aInput: {aField: "String!"} ) }', 'Source!') + assert not result.errors + assert result.data == {'test': '["Source!",{"a_input":{"a_field":"String!"}}]'} + + result = test_schema.execute('{ test(aInput: {recursiveField: {aField: "String!"}}) }', 'Source!') + assert not result.errors + assert result.data == {'test': '["Source!",{"a_input":{"recursive_field":{"a_field":"String!"}}}]'} + + def test_query_middlewares(): class Query(ObjectType): hello = String() diff --git a/graphene/types/tests/test_typemap.py b/graphene/types/tests/test_typemap.py index 10f06af8..dce92b00 100644 --- a/graphene/types/tests/test_typemap.py +++ b/graphene/types/tests/test_typemap.py @@ -67,7 +67,7 @@ def test_objecttype(): f = MyObjectType.resolve_foo assert foo_field.resolver == getattr(f, '__func__', f) assert foo_field.args == { - 'bar': GraphQLArgument(GraphQLString, description='Argument description', default_value='x') + 'bar': GraphQLArgument(GraphQLString, description='Argument description', default_value='x', out_name='bar') } @@ -114,18 +114,18 @@ def test_interface(): assert foo_field.description == 'Field description' assert not foo_field.resolver # Resolver not attached in interfaces assert foo_field.args == { - 'bar': GraphQLArgument(GraphQLString, description='Argument description', default_value='x') + 'bar': GraphQLArgument(GraphQLString, description='Argument description', default_value='x', out_name='bar') } def test_inputobject(): class MyInputObjectType(InputObjectType): '''Description''' - foo = String(description='Field description') + foo_bar = String(description='Field description') bar = String(name='gizmo') own = InputField(lambda: MyInputObjectType) - def resolve_foo(self, args, info): + def resolve_foo_bar(self, args, info): return args.get('bar') typemap = TypeMap([MyInputObjectType]) @@ -136,8 +136,8 @@ def test_inputobject(): assert graphql_type.description == 'Description' fields = graphql_type.fields - assert list(fields.keys()) == ['foo', 'gizmo', 'own'] + assert list(fields.keys()) == ['fooBar', 'gizmo', 'own'] assert fields['own'].type == graphql_type - foo_field = fields['foo'] + foo_field = fields['fooBar'] assert isinstance(foo_field, GraphQLInputObjectField) assert foo_field.description == 'Field description' diff --git a/graphene/types/typemap.py b/graphene/types/typemap.py index 0d0260ee..47107777 100644 --- a/graphene/types/typemap.py +++ b/graphene/types/typemap.py @@ -206,6 +206,7 @@ class TypeMap(GraphQLTypeMap): _field = GraphQLInputObjectField( field_type, default_value=field.default_value, + out_name=field.name or name, description=field.description ) else: @@ -213,9 +214,10 @@ class TypeMap(GraphQLTypeMap): for arg_name, arg in field.args.items(): map = cls.reducer(map, arg.type) arg_type = cls.get_field_type(map, arg.type) - arg_name = arg.name or cls.process_field_name(arg_name) - args[arg_name] = GraphQLArgument( + processed_arg_name = arg.name or cls.process_field_name(arg_name) + args[processed_arg_name] = GraphQLArgument( arg_type, + out_name=arg.name or arg_name, description=arg.description, default_value=arg.default_value )