Skip to content

Commit e12c55b

Browse files
committed
Sort fields, args and values in GraphQLObjectType, GraphQLInputObjectType and GraphQLEnumValue if they are not an ordered dict.
Closes #25
1 parent 87eb526 commit e12c55b

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

graphql/core/type/definition.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ def define_field_map(type, field_map):
210210
'function which returns such a mapping.'
211211
).format(type)
212212

213+
if not isinstance(field_map, collections.OrderedDict):
214+
field_map = collections.OrderedDict(sorted(list(field_map.items())))
215+
213216
result_field_map = collections.OrderedDict()
214217
for field_name, field in field_map.items():
215218
assert_valid_name(field_name)
@@ -234,6 +237,9 @@ def define_field_map(type, field_map):
234237
field_name)
235238
)
236239
args = []
240+
if not isinstance(field_args, collections.OrderedDict):
241+
field_args = collections.OrderedDict(sorted(list(field_args.items())))
242+
237243
for arg_name, arg in field_args.items():
238244
assert_valid_name(arg_name)
239245
assert isinstance(arg, GraphQLArgument), (
@@ -538,6 +544,9 @@ def define_enum_values(type, value_map):
538544
)
539545

540546
values = []
547+
if not isinstance(value_map, collections.OrderedDict):
548+
value_map = collections.OrderedDict(sorted(list(value_map.items())))
549+
541550
for value_name, value in value_map.items():
542551
assert_valid_name(value_name)
543552
assert isinstance(value, GraphQLEnumValue), (

tests/core_type/test_definition.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from collections import OrderedDict
12
from py.test import raises
23
from graphql.core.type import (
34
GraphQLSchema,
@@ -275,3 +276,45 @@ def test_does_not_mutate_passed_field_definitions():
275276
'field1': GraphQLInputObjectField(GraphQLString),
276277
'field2': GraphQLInputObjectField(GraphQLString),
277278
}
279+
280+
281+
def test_sorts_fields_and_argument_keys_if_not_using_ordered_dict():
282+
fields = {
283+
'b': GraphQLField(GraphQLString),
284+
'c': GraphQLField(GraphQLString),
285+
'a': GraphQLField(GraphQLString),
286+
'd': GraphQLField(GraphQLString, args={
287+
'q': GraphQLArgument(GraphQLString),
288+
'x': GraphQLArgument(GraphQLString),
289+
'v': GraphQLArgument(GraphQLString),
290+
'a': GraphQLArgument(GraphQLString),
291+
'n': GraphQLArgument(GraphQLString)
292+
})
293+
}
294+
295+
test_object = GraphQLObjectType(name='Test', fields=fields)
296+
ordered_fields = test_object.get_fields()
297+
assert list(ordered_fields.keys()) == ['a', 'b', 'c', 'd']
298+
field_with_args = test_object.get_fields().get('d')
299+
assert [a.name for a in field_with_args.args] == ['a', 'n', 'q', 'v', 'x']
300+
301+
302+
def test_does_not_sort_fields_and_argument_keys_when_using_ordered_dict():
303+
fields = OrderedDict([
304+
('b', GraphQLField(GraphQLString)),
305+
('c', GraphQLField(GraphQLString)),
306+
('a', GraphQLField(GraphQLString)),
307+
('d', GraphQLField(GraphQLString, args=OrderedDict([
308+
('q', GraphQLArgument(GraphQLString)),
309+
('x', GraphQLArgument(GraphQLString)),
310+
('v', GraphQLArgument(GraphQLString)),
311+
('a', GraphQLArgument(GraphQLString)),
312+
('n', GraphQLArgument(GraphQLString))
313+
])))
314+
])
315+
316+
test_object = GraphQLObjectType(name='Test', fields=fields)
317+
ordered_fields = test_object.get_fields()
318+
assert list(ordered_fields.keys()) == ['b', 'c', 'a', 'd']
319+
field_with_args = test_object.get_fields().get('d')
320+
assert [a.name for a in field_with_args.args] == ['q', 'x', 'v', 'a', 'n']

tests/core_type/test_enum_type.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,25 @@ def test_enum_inputs_may_be_nullable():
159159
result = graphql(Schema, '{ colorEnum colorInt }')
160160
assert not result.errors
161161
assert result.data == {'colorEnum': None, 'colorInt': None}
162+
163+
164+
def test_sorts_values_if_not_using_ordered_dict():
165+
enum = GraphQLEnumType(name='Test', values={
166+
'c': GraphQLEnumValue(),
167+
'b': GraphQLEnumValue(),
168+
'a': GraphQLEnumValue(),
169+
'd': GraphQLEnumValue()
170+
})
171+
172+
assert [v.name for v in enum.get_values()] == ['a', 'b', 'c', 'd']
173+
174+
175+
def test_does_not_sort_values_when_using_ordered_dict():
176+
enum = GraphQLEnumType(name='Test', values=OrderedDict([
177+
('c', GraphQLEnumValue()),
178+
('b', GraphQLEnumValue()),
179+
('a', GraphQLEnumValue()),
180+
('d', GraphQLEnumValue()),
181+
]))
182+
183+
assert [v.name for v in enum.get_values()] == ['c', 'b', 'a', 'd']

0 commit comments

Comments
 (0)