Skip to content

Commit 03c18b7

Browse files
committed
find_dangerous_changes: sort fields inside 'defaultValue'
Replicates graphql/graphql-js@6609d39
1 parent 2133aff commit 03c18b7

File tree

4 files changed

+54
-4
lines changed

4 files changed

+54
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The current version 3.0.0b0 of GraphQL-core is up-to-date
1616
with GraphQL.js version 14.5.0.
1717

1818
All parts of the API are covered by an extensive test suite
19-
of currently 1987 unit tests.
19+
of currently 1988 unit tests.
2020

2121

2222
## Documentation

src/graphql/language/visitor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ def visit(root: Node, visitor: Visitor, visitor_keys=None) -> Any:
211211
if not isinstance(root, Node):
212212
raise TypeError(f"Not an AST Node: {inspect(root)}")
213213
if not isinstance(visitor, Visitor):
214-
raise TypeError(f"Not an AST Visitor class: {inspect(visitor)}")
214+
raise TypeError(f"Not an AST Visitor: {inspect(visitor)}")
215215
if visitor_keys is None:
216216
visitor_keys = QUERY_DOCUMENT_KEYS
217217
stack: Any = None

src/graphql/utilities/find_breaking_changes.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from enum import Enum
2+
from operator import attrgetter
23
from typing import Any, Dict, List, NamedTuple, Union, cast
34

45
from ..error import INVALID
5-
from ..language import print_ast
6+
from ..language import print_ast, visit, Visitor
67
from ..pyutils import inspect
78
from ..type import (
89
GraphQLEnumType,
@@ -399,6 +400,9 @@ def find_arg_changes(
399400
)
400401
)
401402
else:
403+
# Since we are looking only for client's observable changes we should
404+
# compare default values in the same representation as they are
405+
# represented inside introspection.
402406
old_value_str = stringify_value(old_arg.default_value, old_arg.type)
403407
new_value_str = stringify_value(new_arg.default_value, new_arg.type)
404408

@@ -532,7 +536,18 @@ def stringify_value(value: Any, type_: GraphQLInputType) -> str:
532536
ast = ast_from_value(value, type_)
533537
if ast is None:
534538
raise TypeError(f"Invalid value: {inspect(value)}")
535-
return print_ast(ast)
539+
540+
# noinspection PyMethodMayBeStatic
541+
class SortVisitor(Visitor):
542+
def enter_object_value(self, object_node, *_args):
543+
object_node.fields = sorted(
544+
object_node.fields, key=attrgetter("name.value")
545+
)
546+
return object_node
547+
548+
sorted_ast = visit(ast, SortVisitor())
549+
550+
return print_ast(sorted_ast)
536551

537552

538553
class ListDiff(NamedTuple):

tests/utilities/test_find_breaking_changes.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,41 @@ def should_ignore_changes_in_field_order_of_default_value():
936936

937937
assert find_dangerous_changes(old_schema, new_schema) == []
938938

939+
def should_ignore_changes_in_field_definitions_order():
940+
old_schema = build_schema(
941+
"""
942+
input Input1 {
943+
a: String
944+
b: String
945+
c: String
946+
}
947+
948+
type Type1 {
949+
field1(
950+
arg1: Input1 = { a: "a", b: "b", c: "c" }
951+
): String
952+
}
953+
"""
954+
)
955+
956+
new_schema = build_schema(
957+
"""
958+
input Input1 {
959+
c: String
960+
b: String
961+
a: String
962+
}
963+
964+
type Type1 {
965+
field1(
966+
arg1: Input1 = { a: "a", b: "b", c: "c" }
967+
): String
968+
}
969+
"""
970+
)
971+
972+
assert find_dangerous_changes(old_schema, new_schema) == []
973+
939974
def should_detect_if_a_value_was_added_to_an_enum_type():
940975
old_schema = build_schema(
941976
"""

0 commit comments

Comments
 (0)