Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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!' was added to input object type 'Foo'",
Copy link

Choose a reason for hiding this comment

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

Should this message include include that is has a default?

Input field 'e' of type 'String!' with a default value was added to input object type 'Foo'

Copy link
Author

Choose a reason for hiding this comment

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

ack. Updated the logic to handle this.

);
});
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
12 changes: 8 additions & 4 deletions packages/core/src/diff/changes/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ export function buildInputFieldAddedMessage(args: InputFieldAddedChange['meta'])
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 +75,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