Skip to content

Commit f26afa2

Browse files
committed
Preserve input field properties when sorting
1 parent a78b548 commit f26afa2

3 files changed

Lines changed: 120 additions & 9 deletions

File tree

src/graphql/type/validate.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,8 @@ def __call__(self, input_obj: GraphQLInputObjectType) -> None:
589589

590590

591591
def get_all_implements_interface_nodes(
592-
type_: Union[GraphQLObjectType, GraphQLInterfaceType], iface: GraphQLInterfaceType
592+
type_: Union[GraphQLObjectType, GraphQLInterfaceType],
593+
iface: Union[GraphQLObjectType, GraphQLInterfaceType],
593594
) -> List[NamedTypeNode]:
594595
ast_node = type_.ast_node
595596
nodes = type_.extension_ast_nodes

src/graphql/utilities/lexicographic_sort_schema.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ def sort_input_fields(
9292
) -> Dict[str, GraphQLInputField]:
9393
return {
9494
name: GraphQLInputField(
95-
cast(
96-
GraphQLInputType, replace_type(cast(GraphQLNamedType, field.type))
97-
),
98-
description=field.description,
99-
default_value=field.default_value,
100-
extensions=field.extensions,
101-
ast_node=field.ast_node,
95+
**merge_kwargs(
96+
field.to_kwargs(),
97+
type_=cast(
98+
GraphQLInputType,
99+
replace_type(cast(GraphQLNamedType, field.type)),
100+
),
101+
)
102102
)
103103
for name, field in sorted(fields_map.items())
104104
}

tests/utilities/test_lexicographic_sort_schema.py

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
from graphql.utilities import build_schema, print_schema, lexicographic_sort_schema
1+
from typing import cast
2+
3+
from graphql.type import (
4+
GraphQLArgument,
5+
GraphQLField,
6+
GraphQLInputField,
7+
GraphQLInputObjectType,
8+
GraphQLObjectType,
9+
GraphQLSchema,
10+
GraphQLString,
11+
)
12+
from graphql.utilities import build_schema, lexicographic_sort_schema, print_schema
213

314
from ..utils import dedent
415

@@ -342,3 +353,102 @@ def sort_recursive_types():
342353
fooC: FooC
343354
}
344355
""")
356+
357+
def describe_input_field_properties():
358+
def preserves_input_field_deprecation_reason():
359+
input_type = GraphQLInputObjectType(
360+
"TestInput",
361+
{
362+
"zField": GraphQLInputField(GraphQLString),
363+
"aField": GraphQLInputField(
364+
GraphQLString, deprecation_reason="Use bField instead"
365+
),
366+
"mField": GraphQLInputField(
367+
GraphQLString, deprecation_reason="Deprecated field"
368+
),
369+
},
370+
)
371+
query_type = GraphQLObjectType(
372+
"Query",
373+
{
374+
"dummy": GraphQLField(
375+
GraphQLString, args={"input": GraphQLArgument(input_type)}
376+
)
377+
},
378+
)
379+
schema = GraphQLSchema(query=query_type, types=[input_type])
380+
sorted_schema = lexicographic_sort_schema(schema)
381+
382+
sorted_input_type = cast(
383+
GraphQLInputObjectType, sorted_schema.type_map["TestInput"]
384+
)
385+
field_names = list(sorted_input_type.fields)
386+
assert field_names == ["aField", "mField", "zField"]
387+
388+
assert (
389+
sorted_input_type.fields["aField"].deprecation_reason
390+
== "Use bField instead"
391+
)
392+
assert (
393+
sorted_input_type.fields["mField"].deprecation_reason
394+
== "Deprecated field"
395+
)
396+
assert sorted_input_type.fields["zField"].deprecation_reason is None
397+
398+
def preserves_input_field_extensions():
399+
input_type = GraphQLInputObjectType(
400+
"TestInput",
401+
{
402+
"zField": GraphQLInputField(GraphQLString, extensions={"x": 1}),
403+
"aField": GraphQLInputField(
404+
GraphQLString, extensions={"custom": "value"}
405+
),
406+
},
407+
)
408+
query_type = GraphQLObjectType(
409+
"Query",
410+
{
411+
"dummy": GraphQLField(
412+
GraphQLString, args={"input": GraphQLArgument(input_type)}
413+
)
414+
},
415+
)
416+
schema = GraphQLSchema(query=query_type, types=[input_type])
417+
sorted_schema = lexicographic_sort_schema(schema)
418+
sorted_input_type = cast(
419+
GraphQLInputObjectType, sorted_schema.type_map["TestInput"]
420+
)
421+
422+
field_names = list(sorted_input_type.fields)
423+
assert field_names == ["aField", "zField"]
424+
425+
assert sorted_input_type.fields["aField"].extensions == {"custom": "value"}
426+
assert sorted_input_type.fields["zField"].extensions == {"x": 1}
427+
428+
def preserves_input_field_out_name():
429+
input_type = GraphQLInputObjectType(
430+
"TestInput",
431+
{
432+
"zField": GraphQLInputField(GraphQLString, out_name="z_field"),
433+
"aField": GraphQLInputField(GraphQLString, out_name="a_field"),
434+
},
435+
)
436+
query_type = GraphQLObjectType(
437+
"Query",
438+
{
439+
"dummy": GraphQLField(
440+
GraphQLString, args={"input": GraphQLArgument(input_type)}
441+
)
442+
},
443+
)
444+
schema = GraphQLSchema(query=query_type, types=[input_type])
445+
sorted_schema = lexicographic_sort_schema(schema)
446+
sorted_input_type = cast(
447+
GraphQLInputObjectType, sorted_schema.type_map["TestInput"]
448+
)
449+
450+
field_names = list(sorted_input_type.fields)
451+
assert field_names == ["aField", "zField"]
452+
453+
assert sorted_input_type.fields["aField"].out_name == "a_field"
454+
assert sorted_input_type.fields["zField"].out_name == "z_field"

0 commit comments

Comments
 (0)