Skip to content

Commit b10eeb4

Browse files
committed
schema_printer: preserve order of types
Replicates graphql/graphql-js@057510b
1 parent 6f324d8 commit b10eeb4

File tree

5 files changed

+163
-167
lines changed

5 files changed

+163
-167
lines changed

src/graphql/utilities/extend_schema.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -623,13 +623,12 @@ def build_type(ast_node: TypeDefinitionNode) -> GraphQLNamedType:
623623
return build_function(ast_node)
624624

625625
type_map: Dict[str, GraphQLNamedType] = {}
626+
for existing_type in schema_kwargs["types"]:
627+
type_map[existing_type.name] = extend_named_type(existing_type)
626628
for type_node in type_defs:
627629
name = type_node.name.value
628630
type_map[name] = std_type_map.get(name) or build_type(type_node)
629631

630-
for existing_type in schema_kwargs["types"]:
631-
type_map[existing_type.name] = extend_named_type(existing_type)
632-
633632
# Get the extended root operation types.
634633
operation_types: Dict[OperationType, GraphQLNamedType] = {}
635634
for operation_type in OperationType:

src/graphql/utilities/schema_printer.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,14 @@ def print_filtered_schema(
5555
type_filter: Callable[[GraphQLNamedType], bool],
5656
) -> str:
5757
directives = filter(directive_filter, schema.directives)
58-
type_map = schema.type_map
59-
types = filter(type_filter, map(type_map.get, sorted(type_map))) # type: ignore
58+
types = filter(type_filter, schema.type_map.values())
6059

6160
return (
6261
"\n\n".join(
6362
chain(
6463
filter(None, [print_schema_definition(schema)]),
6564
(print_directive(directive) for directive in directives),
66-
(print_type(type_) for type_ in types), # type: ignore
65+
(print_type(type_) for type_ in types),
6766
)
6867
)
6968
+ "\n"

tests/type/test_schema.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ def define_sample_schema():
109109

110110
assert print_schema(schema) == dedent(
111111
"""
112+
type Query {
113+
article(id: String): Article
114+
feed: [Article]
115+
}
116+
112117
type Article {
113118
id: String
114119
isPublished: Boolean
@@ -134,11 +139,6 @@ def define_sample_schema():
134139
writeArticle: Article
135140
}
136141
137-
type Query {
138-
article(id: String): Article
139-
feed: [Article]
140-
}
141-
142142
type Subscription {
143143
articleSubscribe(id: String): Article
144144
}

tests/utilities/test_extend_schema.py

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -658,15 +658,15 @@ def extends_objects_by_adding_new_fields_with_arguments():
658658
assert validate_schema(extended_schema) == []
659659
assert print_schema_changes(schema, extended_schema) == dedent(
660660
"""
661+
type SomeObject {
662+
newField(arg1: String, arg2: NewInputObj!): String
663+
}
664+
661665
input NewInputObj {
662666
field1: Int
663667
field2: [Float]
664668
field3: String!
665669
}
666-
667-
type SomeObject {
668-
newField(arg1: String, arg2: NewInputObj!): String
669-
}
670670
"""
671671
)
672672

@@ -778,8 +778,7 @@ def extends_objects_by_including_new_types():
778778
assert validate_schema(extended_schema) == []
779779

780780
assert print_schema_changes(schema, extended_schema) == dedent(
781-
new_types_sdl
782-
+ """
781+
"""
783782
type SomeObject {
784783
oldField: String
785784
newObject: NewObject
@@ -788,8 +787,8 @@ def extends_objects_by_including_new_types():
788787
newScalar: NewScalar
789788
newEnum: NewEnum
790789
newTree: [SomeObject]!
791-
}
792-
"""
790+
}\n"""
791+
+ new_types_sdl
793792
)
794793

795794
def extends_objects_by_adding_implemented_new_interfaces():
@@ -824,12 +823,12 @@ def extends_objects_by_adding_implemented_new_interfaces():
824823
assert validate_schema(extended_schema) == []
825824
assert print_schema_changes(schema, extended_schema) == dedent(
826825
"""
827-
interface NewInterface {
826+
type SomeObject implements OldInterface & NewInterface {
827+
oldField: String
828828
newField: String
829829
}
830830
831-
type SomeObject implements OldInterface & NewInterface {
832-
oldField: String
831+
interface NewInterface {
833832
newField: String
834833
}
835834
"""
@@ -917,28 +916,27 @@ def extends_different_types_multiple_times():
917916

918917
assert validate_schema(extended_schema) == []
919918
assert print_schema_changes(schema, extended_schema) == dedent(
920-
new_types_sdl
921-
+ """
919+
"""
920+
type SomeObject implements SomeInterface & NewInterface & AnotherNewInterface {
921+
oldField: String
922+
newField: String
923+
anotherNewField: String
924+
}
925+
922926
enum SomeEnum {
923927
OLD_VALUE
924928
NEW_VALUE
925929
ANOTHER_NEW_VALUE
926930
}
927931
928-
input SomeInput {
929-
oldField: String
930-
newField: String
931-
anotherNewField: String
932-
}
932+
union SomeUnion = SomeObject | NewObject | AnotherNewObject
933933
934-
type SomeObject implements SomeInterface & NewInterface & AnotherNewInterface {
934+
input SomeInput {
935935
oldField: String
936936
newField: String
937937
anotherNewField: String
938-
}
939-
940-
union SomeUnion = SomeObject | NewObject | AnotherNewObject
941-
""" # noqa: E501
938+
}\n""" # noqa: E501
939+
+ new_types_sdl
942940
)
943941

944942
def extends_interfaces_by_adding_new_fields():
@@ -981,12 +979,12 @@ def extends_interfaces_by_adding_new_fields():
981979
assert validate_schema(extended_schema) == []
982980
assert print_schema_changes(schema, extended_schema) == dedent(
983981
"""
984-
interface AnotherInterface implements SomeInterface {
982+
interface SomeInterface {
985983
oldField: String
986984
newField: String
987985
}
988986
989-
interface SomeInterface {
987+
interface AnotherInterface implements SomeInterface {
990988
oldField: String
991989
newField: String
992990
}
@@ -1043,12 +1041,12 @@ def extends_interfaces_by_adding_new_implemented_interfaces():
10431041
newField: String
10441042
}
10451043
1046-
interface NewInterface {
1044+
type SomeObject implements SomeInterface & AnotherInterface & NewInterface {
1045+
oldField: String
10471046
newField: String
10481047
}
10491048
1050-
type SomeObject implements SomeInterface & AnotherInterface & NewInterface {
1051-
oldField: String
1049+
interface NewInterface {
10521050
newField: String
10531051
}
10541052
"""
@@ -1162,16 +1160,16 @@ def may_extend_mutations_and_subscriptions():
11621160
assert print_schema(mutation_schema) == original_print
11631161
assert print_schema(extended_schema) == dedent(
11641162
"""
1165-
type Mutation {
1166-
mutationField: String
1167-
newMutationField: Int
1168-
}
1169-
11701163
type Query {
11711164
queryField: String
11721165
newQueryField: Int
11731166
}
11741167
1168+
type Mutation {
1169+
mutationField: String
1170+
newMutationField: Int
1171+
}
1172+
11751173
type Subscription {
11761174
subscriptionField: String
11771175
newSubscriptionField: Int

0 commit comments

Comments
 (0)