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

Detect added nullable input field or added nullable argument #1096

Merged
merged 1 commit into from
Dec 1, 2017
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
107 changes: 107 additions & 0 deletions src/utilities/__tests__/findBreakingChanges-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
findBreakingChanges,
findDangerousChanges,
findFieldsThatChangedType,
findFieldsThatChangedTypeOnInputObjectTypes,
findRemovedTypes,
findTypesRemovedFromUnions,
findTypesAddedToUnions,
Expand Down Expand Up @@ -1481,6 +1482,56 @@ describe('findDangerousChanges', () => {
]);
});

it('should detect if a nullable field was added to an input', () => {
const oldInputType = new GraphQLInputObjectType({
name: 'InputType1',
fields: {
field1: {
type: GraphQLString,
},
},
});

const newInputType = new GraphQLInputObjectType({
name: 'InputType1',
fields: {
field1: {
type: GraphQLString,
},
field2: {
type: GraphQLInt,
},
},
});

const oldSchema = new GraphQLSchema({
query: queryType,
types: [
oldInputType,
]
});

const newSchema = new GraphQLSchema({
query: queryType,
types: [
newInputType,
]
});

const expectedFieldChanges = [
{
type: DangerousChangeType.NULLABLE_INPUT_FIELD_ADDED,
description: 'A nullable field field2 on input type ' +
'InputType1 was added.',
},
];

expect(findFieldsThatChangedTypeOnInputObjectTypes(
oldSchema,
newSchema
).dangerousFieldChanges).to.eql(expectedFieldChanges);
});

it('should find all dangerous changes', () => {
const enumThatGainsAValueOld = new GraphQLEnumType({
name: 'EnumType1',
Expand Down Expand Up @@ -1589,4 +1640,60 @@ describe('findDangerousChanges', () => {
expectedDangerousChanges
);
});

it('should detect if a nullable field argument was added', () => {
const oldType = new GraphQLObjectType({
name: 'Type1',
fields: {
field1: {
type: GraphQLString,
args: {
arg1: {
type: GraphQLString,
},
},
},
},
});

const newType = new GraphQLObjectType({
name: 'Type1',
fields: {
field1: {
type: GraphQLString,
args: {
arg1: {
type: GraphQLString,
},
arg2: {
type: GraphQLString,
},
},
},
},
});

const oldSchema = new GraphQLSchema({
query: queryType,
types: [
oldType,
]
});

const newSchema = new GraphQLSchema({
query: queryType,
types: [
newType,
]
});

expect(
findArgChanges(oldSchema, newSchema).dangerousChanges
).to.eql([
{
type: DangerousChangeType.NULLABLE_ARG_ADDED,
description: 'A nullable arg arg2 on Type1.field1 was added',
},
]);
});
});
66 changes: 46 additions & 20 deletions src/utilities/findBreakingChanges.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export const DangerousChangeType = {
ARG_DEFAULT_VALUE_CHANGE: 'ARG_DEFAULT_VALUE_CHANGE',
VALUE_ADDED_TO_ENUM: 'VALUE_ADDED_TO_ENUM',
TYPE_ADDED_TO_UNION: 'TYPE_ADDED_TO_UNION',
NULLABLE_INPUT_FIELD_ADDED: 'NULLABLE_INPUT_FIELD_ADDED',
NULLABLE_ARG_ADDED: 'NULLABLE_ARG_ADDED',
};

export type BreakingChange = {
Expand Down Expand Up @@ -87,7 +89,9 @@ export function findDangerousChanges(
return [
...findArgChanges(oldSchema, newSchema).dangerousChanges,
...findValuesAddedToEnums(oldSchema, newSchema),
...findTypesAddedToUnions(oldSchema, newSchema)
...findTypesAddedToUnions(oldSchema, newSchema),
...findFieldsThatChangedTypeOnInputObjectTypes(oldSchema, newSchema)
.dangerousFieldChanges
];
}

Expand Down Expand Up @@ -222,12 +226,20 @@ export function findArgChanges(
const oldArgDef = oldArgs.find(
arg => arg.name === newArgDef.name
);
if (!oldArgDef && newArgDef.type instanceof GraphQLNonNull) {
breakingChanges.push({
type: BreakingChangeType.NON_NULL_ARG_ADDED,
description: `A non-null arg ${newArgDef.name} on ` +
`${newType.name}.${fieldName} was added`,
});
if (!oldArgDef) {
if (newArgDef.type instanceof GraphQLNonNull) {
breakingChanges.push({
type: BreakingChangeType.NON_NULL_ARG_ADDED,
description: `A non-null arg ${newArgDef.name} on ` +
`${newType.name}.${fieldName} was added`,
});
} else {
dangerousChanges.push({
type: DangerousChangeType.NULLABLE_ARG_ADDED,
description: `A nullable arg ${newArgDef.name} on ` +
`${newType.name}.${fieldName} was added`,
});
}
}
});
});
Expand Down Expand Up @@ -273,7 +285,8 @@ export function findFieldsThatChangedType(
): Array<BreakingChange> {
return [
...findFieldsThatChangedTypeOnObjectOrInterfaceTypes(oldSchema, newSchema),
...findFieldsThatChangedTypeOnInputObjectTypes(oldSchema, newSchema),
...findFieldsThatChangedTypeOnInputObjectTypes(oldSchema, newSchema)
.breakingFieldChanges,
];
}

Expand Down Expand Up @@ -332,11 +345,15 @@ function findFieldsThatChangedTypeOnObjectOrInterfaceTypes(
export function findFieldsThatChangedTypeOnInputObjectTypes(
oldSchema: GraphQLSchema,
newSchema: GraphQLSchema
): Array<BreakingChange> {
): {
breakingFieldChanges: Array<BreakingChange>,
dangerousFieldChanges: Array<DangerousChange>
} {
const oldTypeMap = oldSchema.getTypeMap();
const newTypeMap = newSchema.getTypeMap();

const breakingFieldChanges = [];
const dangerousFieldChanges = [];
Object.keys(oldTypeMap).forEach(typeName => {
const oldType = oldTypeMap[typeName];
const newType = newTypeMap[typeName];
Expand Down Expand Up @@ -377,21 +394,29 @@ export function findFieldsThatChangedTypeOnInputObjectTypes(
}
}
});
// Check if a non-null field was added to the input object type
// Check if a field was added to the input object type
Object.keys(newTypeFieldsDef).forEach(fieldName => {
if (
!(fieldName in oldTypeFieldsDef) &&
newTypeFieldsDef[fieldName].type instanceof GraphQLNonNull
) {
breakingFieldChanges.push({
type: BreakingChangeType.NON_NULL_INPUT_FIELD_ADDED,
description: `A non-null field ${fieldName} on ` +
`input type ${newType.name} was added.`,
});
if (!(fieldName in oldTypeFieldsDef)) {
if (newTypeFieldsDef[fieldName].type instanceof GraphQLNonNull) {
breakingFieldChanges.push({
type: BreakingChangeType.NON_NULL_INPUT_FIELD_ADDED,
description: `A non-null field ${fieldName} on ` +
`input type ${newType.name} was added.`,
});
} else {
dangerousFieldChanges.push({
type: DangerousChangeType.NULLABLE_INPUT_FIELD_ADDED,
description: `A nullable field ${fieldName} on ` +
`input type ${newType.name} was added.`,
});
}
}
});
});
return breakingFieldChanges;
return {
breakingFieldChanges,
dangerousFieldChanges,
};
}

function isChangeSafeForObjectOrInterfaceField(
Expand Down Expand Up @@ -647,3 +672,4 @@ export function findInterfacesRemovedFromObjectTypes(
});
return breakingChanges;
}