Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

findDangerousChanges: sort fields inside 'defaultValue' #2151

Merged
merged 1 commit into from
Sep 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/utilities/__tests__/findBreakingChanges-test.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For input type default values we apparently just compare it's stringified value instead of doing a deep comparison.

@Neitsch It's intentional since we want to detect only changes observable by the client.
So we should compare defaultValues in the same representation as used by introspection:

defaultValue: {
type: GraphQLString,
description:
'A GraphQL-formatted string representing the default value for this input value.',
resolve(inputVal) {
const valueAST = astFromValue(inputVal.defaultValue, inputVal.type);
return valueAST ? print(valueAST) : null;
},
},

It's important because defaultValue is always specified in "internal" format (same format as returned by parseLiteral/parseValue and passed into serialize) that mean it can be for example instances of Date class and === wouldn't work for them but if you pass both values into serialize you can compare resulting strings without any problems.

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