Skip to content

InputFieldAddedChange when adding a non-null type field with a default value is not a breaking change #2881

New issue

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

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

Already on GitHub? # to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 12 additions & 3 deletions packages/core/__tests__/diff/input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@ describe('input', () => {
b: String!
c: String!
d: String
e: String! = "Eee"
}
`);

const changes = await diff(a, b);
const change = {
c: findFirstChangeByPath(await diff(a, b), 'Foo.c'),
d: findFirstChangeByPath(await diff(a, b), 'Foo.d'),
c: findFirstChangeByPath(changes, 'Foo.c'),
d: findFirstChangeByPath(changes, 'Foo.d'),
e: findFirstChangeByPath(changes, 'Foo.e'),
};

// Non-nullable
// Non-nullable without default
expect(change.c.criticality.level).toEqual(CriticalityLevel.Breaking);
expect(change.c.type).toEqual('INPUT_FIELD_ADDED');
expect(change.c.message).toEqual(
Expand All @@ -37,6 +40,12 @@ describe('input', () => {
expect(change.d.message).toEqual(
"Input field 'd' of type 'String' was added to input object type 'Foo'",
);
// Non-nullable with default
expect(change.e.criticality.level).toEqual(CriticalityLevel.Dangerous);
Copy link

Choose a reason for hiding this comment

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

Why is this dangerous? This seems simply non-breaking to me. What's the definition of dangerous vs critical?

Copy link
Author

Choose a reason for hiding this comment

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

Dangerous is the original criticality level defined by core for this rule. So I would like to keep it as the same. For our internal rule, we will override it as non-breaking.

expect(change.e.type).toEqual('INPUT_FIELD_ADDED');
expect(change.e.message).toEqual(
"Input field 'e' of type 'String!' with a default value was added to input object type 'Foo'",
);
});
test('removed', async () => {
const a = buildSchema(/* GraphQL */ `
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/diff/changes/change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,8 @@ export type InputFieldAddedChange = {
addedInputFieldName: string;
isAddedInputFieldTypeNullable: boolean;
addedInputFieldType: string;
hasDefaultValue: boolean;
isAddedInputFieldBreaking: boolean;
};
};

Expand Down
15 changes: 10 additions & 5 deletions packages/core/src/diff/changes/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,21 @@ export function inputFieldRemoved(
}

export function buildInputFieldAddedMessage(args: InputFieldAddedChange['meta']) {
return `Input field '${args.addedInputFieldName}' of type '${args.addedInputFieldType}' was added to input object type '${args.inputName}'`;
const defaultValueSuffix = args.hasDefaultValue ? ' with a default value' : '';
return `Input field '${args.addedInputFieldName}' of type '${args.addedInputFieldType}'${defaultValueSuffix} was added to input object type '${args.inputName}'`;
}

export function inputFieldAddedFromMeta(args: InputFieldAddedChange) {
return {
type: ChangeType.InputFieldAdded,
criticality: args.meta.isAddedInputFieldTypeNullable
criticality: args.meta.isAddedInputFieldBreaking
? {
level: CriticalityLevel.Dangerous,
}
: {
level: CriticalityLevel.Breaking,
reason:
'Adding a required input field to an existing input object type is a breaking change because it will cause existing uses of this input object type to error.',
}
: {
level: CriticalityLevel.Dangerous,
},
message: buildInputFieldAddedMessage(args.meta),
meta: args.meta,
Expand All @@ -75,13 +76,17 @@ export function inputFieldAdded(
input: GraphQLInputObjectType,
field: GraphQLInputField,
): Change<typeof ChangeType.InputFieldAdded> {
const isBreaking = isNonNullType(field.type) && typeof field.defaultValue === 'undefined';

return inputFieldAddedFromMeta({
type: ChangeType.InputFieldAdded,
meta: {
inputName: input.name,
addedInputFieldName: field.name,
isAddedInputFieldTypeNullable: !isNonNullType(field.type),
addedInputFieldType: field.type.toString(),
hasDefaultValue: field.defaultValue != null,
isAddedInputFieldBreaking: isBreaking,
},
});
}
Expand Down