Skip to content

Commit f2c1275

Browse files
committed
Added out_name to input types (Argument and InputField)
1 parent e503d50 commit f2c1275

File tree

4 files changed

+87
-10
lines changed

4 files changed

+87
-10
lines changed

graphql/execution/tests/test_resolve.py

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
from graphql import graphql
55
from graphql.type import (GraphQLArgument, GraphQLField, GraphQLInt,
6-
GraphQLObjectType, GraphQLSchema, GraphQLString)
6+
GraphQLObjectType, GraphQLSchema, GraphQLString,
7+
GraphQLInputObjectType, GraphQLInputObjectField,
8+
GraphQLNonNull, GraphQLList)
79

810

911
def _test_schema(test_field):
@@ -69,3 +71,68 @@ def resolver(source, args, *_):
6971
{'test': '["Source!",{"aStr":"String!","aInt":-123}]'},
7072
{'test': '["Source!",{"aInt":-123,"aStr":"String!"}]'}
7173
]
74+
75+
76+
def test_maps_argument_out_names_well():
77+
def resolver(source, args, *_):
78+
return json.dumps([source, args], separators=(',', ':'))
79+
80+
schema = _test_schema(GraphQLField(
81+
GraphQLString,
82+
args=OrderedDict([
83+
('aStr', GraphQLArgument(GraphQLString, out_name="a_str")),
84+
('aInt', GraphQLArgument(GraphQLInt, out_name="a_int")),
85+
]),
86+
resolver=resolver
87+
))
88+
89+
result = graphql(schema, '{ test }', None)
90+
assert not result.errors
91+
assert result.data == {'test': '[null,{}]'}
92+
93+
result = graphql(schema, '{ test(aStr: "String!") }', 'Source!')
94+
assert not result.errors
95+
assert result.data == {'test': '["Source!",{"a_str":"String!"}]'}
96+
97+
result = graphql(schema, '{ test(aInt: -123, aStr: "String!",) }', 'Source!')
98+
assert not result.errors
99+
assert result.data in [
100+
{'test': '["Source!",{"a_str":"String!","a_int":-123}]'},
101+
{'test': '["Source!",{"a_int":-123,"a_str":"String!"}]'}
102+
]
103+
104+
105+
def test_maps_argument_out_names_well_with_input():
106+
def resolver(source, args, *_):
107+
return json.dumps([source, args], separators=(',', ':'))
108+
109+
110+
TestInputObject = GraphQLInputObjectType('TestInputObject', lambda: OrderedDict([
111+
('inputOne', GraphQLInputObjectField(GraphQLString, out_name="input_one")),
112+
('inputRecursive', GraphQLInputObjectField(TestInputObject, out_name="input_recursive")),
113+
]))
114+
115+
schema = _test_schema(GraphQLField(
116+
GraphQLString,
117+
args=OrderedDict([
118+
('aStr', GraphQLArgument(GraphQLString, out_name="a_str")),
119+
('aInt', GraphQLArgument(GraphQLInt, out_name="a_int")),
120+
('aInput', GraphQLArgument(TestInputObject, out_name="a_input"))
121+
]),
122+
resolver=resolver
123+
))
124+
125+
result = graphql(schema, '{ test }', None)
126+
assert not result.errors
127+
assert result.data == {'test': '[null,{}]'}
128+
129+
result = graphql(schema, '{ test(aInput: {inputOne: "String!"} ) }', 'Source!')
130+
assert not result.errors
131+
assert result.data == {'test': '["Source!",{"a_input":{"input_one":"String!"}}]'}
132+
133+
result = graphql(schema, '{ test(aInput: {inputOne: "String!", inputRecursive:{inputOne: "SourceRecursive!"}} ) }', 'Source!')
134+
assert not result.errors
135+
assert result.data in [
136+
{'test': '["Source!",{"a_input":{"input_one":"String!","input_recursive":{"input_one":"SourceRecursive!"}}}]'},
137+
{'test': '["Source!",{"a_input":{"input_recursive":{"input_one":"SourceRecursive!"}},"input_one":"String!"}]'}
138+
]

graphql/execution/values.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ def get_argument_values(arg_defs, arg_asts, variables):
5656
value = arg_def.default_value
5757

5858
if value is not None:
59-
result[name] = value
59+
# We use out_name as the output name for the
60+
# dict if exists
61+
result[arg_def.out_name or name] = value
6062

6163
return result
6264

@@ -133,7 +135,9 @@ def coerce_value(type, value):
133135
field_value = field.default_value
134136

135137
if field_value is not None:
136-
obj[field_name] = field_value
138+
# We use out_name as the output name for the
139+
# dict if exists
140+
obj[field.out_name or field_name] = field_value
137141

138142
return obj
139143

graphql/type/definition.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -264,20 +264,22 @@ def __hash__(self):
264264

265265

266266
class GraphQLArgument(object):
267-
__slots__ = 'type', 'default_value', 'description'
267+
__slots__ = 'type', 'default_value', 'description', 'out_name'
268268

269-
def __init__(self, type, default_value=None, description=None):
269+
def __init__(self, type, default_value=None, description=None, out_name=None):
270270
self.type = type
271271
self.default_value = default_value
272272
self.description = description
273+
self.out_name = out_name
273274

274275
def __eq__(self, other):
275276
return (
276277
self is other or (
277278
isinstance(other, GraphQLArgument) and
278279
self.type == other.type and
279280
self.default_value == other.default_value and
280-
self.description == other.description
281+
self.description == other.description and
282+
self.out_name == other.out_name
281283
)
282284
)
283285

@@ -536,19 +538,21 @@ def _define_field_map(self):
536538

537539

538540
class GraphQLInputObjectField(object):
539-
__slots__ = 'type', 'default_value', 'description'
541+
__slots__ = 'type', 'default_value', 'description', 'out_name'
540542

541-
def __init__(self, type, default_value=None, description=None):
543+
def __init__(self, type, default_value=None, description=None, out_name=None):
542544
self.type = type
543545
self.default_value = default_value
544546
self.description = description
547+
self.out_name = out_name
545548

546549
def __eq__(self, other):
547550
return (
548551
self is other or (
549552
isinstance(other, GraphQLInputObjectField) and
550553
self.type == other.type and
551-
self.description == other.description
554+
self.description == other.description and
555+
self.out_name == other.out_name
552556
)
553557
)
554558

graphql/utils/value_from_ast.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ def value_from_ast(value_ast, type, variables=None):
5757
field_value = field.default_value
5858

5959
if field_value is not None:
60-
obj[field_name] = field_value
60+
# We use out_name as the output name for the
61+
# dict if exists
62+
obj[field.out_name or field_name] = field_value
6163

6264
return obj
6365

0 commit comments

Comments
 (0)