Skip to content

Commit

Permalink
findDangerousChanges: sort fields inside 'defaultValue' (#2151)
Browse files Browse the repository at this point in the history
Fixes #2150
  • Loading branch information
IvanGoncharov committed Sep 3, 2019
1 parent 0d2220f commit 6609d39
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
32 changes: 32 additions & 0 deletions src/utilities/__tests__/findBreakingChanges-test.js
Expand Up @@ -921,6 +921,38 @@ describe('findDangerousChanges', () => {
expect(findDangerousChanges(oldSchema, newSchema)).to.deep.equal([]);
});

it('should ignore changes in field definitions order', () => {
const oldSchema = buildSchema(`
input Input1 {
a: String
b: String
c: String
}
type Type1 {
field1(
arg1: Input1 = { a: "a", b: "b", c: "c" }
): String
}
`);

const newSchema = buildSchema(`
input Input1 {
c: String
b: String
a: String
}
type Type1 {
field1(
arg1: Input1 = { a: "a", b: "b", c: "c" }
): String
}
`);

expect(findDangerousChanges(oldSchema, newSchema)).to.deep.equal([]);
});

it('should detect if a value was added to an enum type', () => {
const oldSchema = buildSchema(`
enum EnumType1 {
Expand Down
16 changes: 15 additions & 1 deletion src/utilities/findBreakingChanges.js
Expand Up @@ -7,6 +7,7 @@ import inspect from '../jsutils/inspect';
import invariant from '../jsutils/invariant';

import { print } from '../language/printer';
import { visit } from '../language/visitor';

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

Expand Down Expand Up @@ -518,7 +522,17 @@ function typeKindName(type: GraphQLNamedType): string {
function stringifyValue(value: mixed, type: GraphQLInputType): string {
const ast = astFromValue(value, type);
invariant(ast != null);
return print(ast);

const sortedAST = visit(ast, {
ObjectValue(objectNode) {
const fields = [...objectNode.fields].sort((fieldA, fieldB) =>
fieldA.name.value.localeCompare(fieldB.name.value),
);
return { ...objectNode, fields };
},
});

return print(sortedAST);
}

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

0 comments on commit 6609d39

Please sign in to comment.