Skip to content

Commit 6609d39

Browse files
findDangerousChanges: sort fields inside 'defaultValue' (#2151)
Fixes #2150
1 parent 0d2220f commit 6609d39

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/utilities/__tests__/findBreakingChanges-test.js

+32
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,38 @@ describe('findDangerousChanges', () => {
921921
expect(findDangerousChanges(oldSchema, newSchema)).to.deep.equal([]);
922922
});
923923

924+
it('should ignore changes in field definitions order', () => {
925+
const oldSchema = buildSchema(`
926+
input Input1 {
927+
a: String
928+
b: String
929+
c: String
930+
}
931+
932+
type Type1 {
933+
field1(
934+
arg1: Input1 = { a: "a", b: "b", c: "c" }
935+
): String
936+
}
937+
`);
938+
939+
const newSchema = buildSchema(`
940+
input Input1 {
941+
c: String
942+
b: String
943+
a: String
944+
}
945+
946+
type Type1 {
947+
field1(
948+
arg1: Input1 = { a: "a", b: "b", c: "c" }
949+
): String
950+
}
951+
`);
952+
953+
expect(findDangerousChanges(oldSchema, newSchema)).to.deep.equal([]);
954+
});
955+
924956
it('should detect if a value was added to an enum type', () => {
925957
const oldSchema = buildSchema(`
926958
enum EnumType1 {

src/utilities/findBreakingChanges.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import inspect from '../jsutils/inspect';
77
import invariant from '../jsutils/invariant';
88

99
import { print } from '../language/printer';
10+
import { visit } from '../language/visitor';
1011

1112
import { type GraphQLSchema } from '../type/schema';
1213
import {
@@ -395,6 +396,9 @@ function findArgChanges(
395396
description: `${oldType.name}.${oldField.name} arg ${oldArg.name} defaultValue was removed.`,
396397
});
397398
} else {
399+
// Since we looking only for client's observable changes we should
400+
// compare default values in the same representation as they are
401+
// represented inside introspection.
398402
const oldValueStr = stringifyValue(oldArg.defaultValue, oldArg.type);
399403
const newValueStr = stringifyValue(newArg.defaultValue, newArg.type);
400404

@@ -518,7 +522,17 @@ function typeKindName(type: GraphQLNamedType): string {
518522
function stringifyValue(value: mixed, type: GraphQLInputType): string {
519523
const ast = astFromValue(value, type);
520524
invariant(ast != null);
521-
return print(ast);
525+
526+
const sortedAST = visit(ast, {
527+
ObjectValue(objectNode) {
528+
const fields = [...objectNode.fields].sort((fieldA, fieldB) =>
529+
fieldA.name.value.localeCompare(fieldB.name.value),
530+
);
531+
return { ...objectNode, fields };
532+
},
533+
});
534+
535+
return print(sortedAST);
522536
}
523537

524538
function diff<T: { name: string, ... }>(

0 commit comments

Comments
 (0)