Updated input field names with out_name

This commit is contained in:
Syrus Akbary 2016-08-22 22:08:49 -07:00
parent 208651a4c0
commit 6543f9272a
3 changed files with 64 additions and 8 deletions

View File

@ -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()

View File

@ -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'

View File

@ -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
)