Skip to content

Non-Scalar default dangerous change: Fix for #1566 #1593

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

Closed
wants to merge 3 commits into from
Closed
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
56 changes: 56 additions & 0 deletions src/utilities/__tests__/findBreakingChanges-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,62 @@ describe('findDangerousChanges', () => {
},
]);
});
it("should return empty result if an argument's defaultValue is an Array and it's not changed", () => {
const oldSchema = buildSchema(`
input Input1 {
inner_array: [String]
inner_input: Input2
inner_input_array: [Input2]
}

input Input2 {
string_field: String
int_field: Int
}

type Type1 {
empty_array_default(arr: [String!] = []): String
value_array_default(arr: [[String]] = [["a", "b"], ["c", "d"]]): String
input_default(input: Input1 = {
inner_array: ["a", "b"]
inner_input: {string_field: "abc", int_field: 32}
inner_input_array: [{int_field: 10}]
}): String
}

type Query {
t: Type1
}
`);

const newSchema = buildSchema(`
input Input1 {
inner_array: [String]
inner_input: Input2
inner_input_array: [Input2]
}

input Input2 {
string_field: String
int_field: Int
}

type Type1 {
empty_array_default(arr: [String!] = []): String
value_array_default(arr: [[String]] = [["a", "b"], ["c", "d"]]): String
input_default(input: Input1 = {
inner_array: ["a", "b"]
inner_input: {string_field: "abc", int_field: 32}
inner_input_array: [{int_field: 10}]
}): String
}

type Query {
t: Type1
}
`);
expect(findArgChanges(oldSchema, newSchema).dangerousChanges).to.eql([]);
});
});

it('should detect if a value was added to an enum type', () => {
Expand Down
41 changes: 39 additions & 2 deletions src/utilities/findBreakingChanges.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,10 @@ export function findArgChanges(
`${oldArgDef.type.toString()} to ${newArgDef.type.toString()}`,
});
} else if (
oldArgDef.defaultValue !== undefined &&
oldArgDef.defaultValue !== newArgDef.defaultValue
!isChangeSafeForDefaultValue(
oldArgDef.defaultValue,
newArgDef.defaultValue,
)
) {
dangerousChanges.push({
type: DangerousChangeType.ARG_DEFAULT_VALUE_CHANGE,
Expand Down Expand Up @@ -496,6 +498,41 @@ function isChangeSafeForInputObjectFieldOrFieldArg(
return false;
}

function isChangeSafeForDefaultValue(
oldDefaultValue: ?mixed,
newDefaultValue: ?mixed,
): boolean {
if (oldDefaultValue === undefined) {
return true;
}

if (oldDefaultValue === newDefaultValue) {
return true;
}

if (
oldDefaultValue != null &&
newDefaultValue != null &&
typeof oldDefaultValue === 'object' &&
typeof newDefaultValue === 'object'
) {
if (Array.isArray(oldDefaultValue) !== Array.isArray(newDefaultValue)) {
return false;
}

for (const key in oldDefaultValue) {
if (
!isChangeSafeForDefaultValue(oldDefaultValue[key], newDefaultValue[key])
) {
return false;
}
Copy link
Member

Choose a reason for hiding this comment

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

@mjmahone According to current implementation adding properties to object is safe.
For example isChangeSafeForDefaultValue({}, {a: 1}) => true or isChangeSafeForDefaultValue({a: undefined}, {a: 1}) => true.

Copy link

Choose a reason for hiding this comment

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

I second this. Can we add a couple more testcases to make sure that check works correctly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That should be correct: adding a previously undefined value is OK, but removing a previously undefined value is not (recursively).

}
return true;
}

return false;
}

/**
* Given two schemas, returns an Array containing descriptions of any breaking
* changes in the newSchema related to removing types from a union type.
Expand Down