Skip to content

Commit 0dd2c5a

Browse files
committed
Preserve input field properties when sorting
1 parent e9a3987 commit 0dd2c5a

2 files changed

Lines changed: 117 additions & 8 deletions

File tree

src/graphql/utilities/lexicographic_sort_schema.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,13 @@ def sort_input_fields(
100100
) -> dict[str, GraphQLInputField]:
101101
return {
102102
name: GraphQLInputField(
103-
cast(
104-
"GraphQLInputType",
105-
replace_type(cast("GraphQLNamedType", field.type)),
106-
),
107-
description=field.description,
108-
default_value=field.default_value,
109-
extensions=field.extensions,
110-
ast_node=field.ast_node,
103+
**merge_kwargs(
104+
field.to_kwargs(),
105+
type_=cast(
106+
"GraphQLInputType",
107+
replace_type(cast("GraphQLNamedType", field.type)),
108+
),
109+
)
111110
)
112111
for name, field in sorted(fields_map.items())
113112
}

tests/utilities/test_lexicographic_sort_schema.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
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+
)
112
from graphql.utilities import build_schema, lexicographic_sort_schema, print_schema
213

314
from ..utils import dedent
@@ -382,3 +393,102 @@ def sort_recursive_types():
382393
}
383394
"""
384395
)
396+
397+
def describe_input_field_properties():
398+
def preserves_input_field_deprecation_reason():
399+
input_type = GraphQLInputObjectType(
400+
"TestInput",
401+
{
402+
"zField": GraphQLInputField(GraphQLString),
403+
"aField": GraphQLInputField(
404+
GraphQLString, deprecation_reason="Use bField instead"
405+
),
406+
"mField": GraphQLInputField(
407+
GraphQLString, deprecation_reason="Deprecated field"
408+
),
409+
},
410+
)
411+
query_type = GraphQLObjectType(
412+
"Query",
413+
{
414+
"dummy": GraphQLField(
415+
GraphQLString, args={"input": GraphQLArgument(input_type)}
416+
)
417+
},
418+
)
419+
schema = GraphQLSchema(query=query_type, types=[input_type])
420+
sorted_schema = lexicographic_sort_schema(schema)
421+
422+
sorted_input_type = cast(
423+
"GraphQLInputObjectType", sorted_schema.type_map["TestInput"]
424+
)
425+
field_names = list(sorted_input_type.fields)
426+
assert field_names == ["aField", "mField", "zField"]
427+
428+
assert (
429+
sorted_input_type.fields["aField"].deprecation_reason
430+
== "Use bField instead"
431+
)
432+
assert (
433+
sorted_input_type.fields["mField"].deprecation_reason
434+
== "Deprecated field"
435+
)
436+
assert sorted_input_type.fields["zField"].deprecation_reason is None
437+
438+
def preserves_input_field_extensions():
439+
input_type = GraphQLInputObjectType(
440+
"TestInput",
441+
{
442+
"zField": GraphQLInputField(GraphQLString, extensions={"x": 1}),
443+
"aField": GraphQLInputField(
444+
GraphQLString, extensions={"custom": "value"}
445+
),
446+
},
447+
)
448+
query_type = GraphQLObjectType(
449+
"Query",
450+
{
451+
"dummy": GraphQLField(
452+
GraphQLString, args={"input": GraphQLArgument(input_type)}
453+
)
454+
},
455+
)
456+
schema = GraphQLSchema(query=query_type, types=[input_type])
457+
sorted_schema = lexicographic_sort_schema(schema)
458+
sorted_input_type = cast(
459+
"GraphQLInputObjectType", sorted_schema.type_map["TestInput"]
460+
)
461+
462+
field_names = list(sorted_input_type.fields)
463+
assert field_names == ["aField", "zField"]
464+
465+
assert sorted_input_type.fields["aField"].extensions == {"custom": "value"}
466+
assert sorted_input_type.fields["zField"].extensions == {"x": 1}
467+
468+
def preserves_input_field_out_name():
469+
input_type = GraphQLInputObjectType(
470+
"TestInput",
471+
{
472+
"zField": GraphQLInputField(GraphQLString, out_name="z_field"),
473+
"aField": GraphQLInputField(GraphQLString, out_name="a_field"),
474+
},
475+
)
476+
query_type = GraphQLObjectType(
477+
"Query",
478+
{
479+
"dummy": GraphQLField(
480+
GraphQLString, args={"input": GraphQLArgument(input_type)}
481+
)
482+
},
483+
)
484+
schema = GraphQLSchema(query=query_type, types=[input_type])
485+
sorted_schema = lexicographic_sort_schema(schema)
486+
sorted_input_type = cast(
487+
"GraphQLInputObjectType", sorted_schema.type_map["TestInput"]
488+
)
489+
490+
field_names = list(sorted_input_type.fields)
491+
assert field_names == ["aField", "zField"]
492+
493+
assert sorted_input_type.fields["aField"].out_name == "a_field"
494+
assert sorted_input_type.fields["zField"].out_name == "z_field"

0 commit comments

Comments
 (0)