Skip to content

Adding an interface to a type is now a dangerous change. #992

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

Merged
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
92 changes: 92 additions & 0 deletions src/utilities/__tests__/findBreakingChanges-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
findValuesAddedToEnums,
findArgChanges,
findInterfacesRemovedFromObjectTypes,
findInterfacesAddedToObjectTypes,
} from '../findBreakingChanges';

describe('findBreakingChanges', () => {
Expand Down Expand Up @@ -1427,6 +1428,60 @@ describe('findDangerousChanges', () => {
);
});

it('should detect interfaces added to types', () => {
const interface1 = new GraphQLInterfaceType({
name: 'Interface1',
fields: {
field1: { type: GraphQLString },
},
resolveType: () => null,
});
const oldType = new GraphQLObjectType({
name: 'Type1',
interfaces: [],
fields: {
field1: {
type: GraphQLString,
},
},
});

const newType = new GraphQLObjectType({
name: 'Type1',
interfaces: [
interface1
],
fields: {
field1: {
type: GraphQLString,
},
},
});

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

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

expect(
findInterfacesAddedToObjectTypes(oldSchema, newSchema)
).to.eql([
{
description: 'Interface1 added to interfaces implemented by Type1.',
type: DangerousChangeType.INTERFACE_ADDED_TO_OBJECT
}
]);
});

it('should detect if a type was added to a union type', () => {
const type1 = new GraphQLObjectType({
name: 'Type1',
Expand Down Expand Up @@ -1551,11 +1606,42 @@ describe('findDangerousChanges', () => {
},
});

const interface1 = new GraphQLInterfaceType({
name: 'Interface1',
fields: {
field1: { type: GraphQLString },
},
resolveType: () => null,
});

const typeThatGainsInterfaceOld = new GraphQLObjectType({
name: 'TypeThatGainsInterface1',
interfaces: [],
fields: {
field1: {
type: GraphQLString,
},
},
});

const typeThaGainsInterfaceNew = new GraphQLObjectType({
name: 'TypeThatGainsInterface1',
interfaces: [
interface1
],
fields: {
field1: {
type: GraphQLString,
},
},
});

const oldSchema = new GraphQLSchema({
query: queryType,
types: [
oldType,
enumThatGainsAValueOld,
typeThatGainsInterfaceOld,
unionTypeThatGainsATypeOld
]
});
Expand All @@ -1565,6 +1651,7 @@ describe('findDangerousChanges', () => {
types: [
newType,
enumThatGainsAValueNew,
typeThaGainsInterfaceNew,
unionTypeThatGainsATypeNew
]
});
Expand All @@ -1578,6 +1665,11 @@ describe('findDangerousChanges', () => {
description: 'VALUE2 was added to enum type EnumType1.',
type: 'VALUE_ADDED_TO_ENUM',
},
{
description: 'Interface1 added to interfaces implemented ' +
'by TypeThatGainsInterface1.',
type: DangerousChangeType.INTERFACE_ADDED_TO_OBJECT
},
{
type: DangerousChangeType.TYPE_ADDED_TO_UNION,
description: 'TypeInUnion2 was added to union type ' +
Expand Down
37 changes: 36 additions & 1 deletion src/utilities/findBreakingChanges.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const BreakingChangeType = {
export const DangerousChangeType = {
ARG_DEFAULT_VALUE_CHANGE: 'ARG_DEFAULT_VALUE_CHANGE',
VALUE_ADDED_TO_ENUM: 'VALUE_ADDED_TO_ENUM',
INTERFACE_ADDED_TO_OBJECT: 'INTERFACE_ADDED_TO_OBJECT',
TYPE_ADDED_TO_UNION: 'TYPE_ADDED_TO_UNION',
};

Expand Down Expand Up @@ -87,7 +88,8 @@ export function findDangerousChanges(
return [
...findArgChanges(oldSchema, newSchema).dangerousChanges,
...findValuesAddedToEnums(oldSchema, newSchema),
...findTypesAddedToUnions(oldSchema, newSchema)
...findInterfacesAddedToObjectTypes(oldSchema, newSchema),
...findTypesAddedToUnions(oldSchema, newSchema),
];
}

Expand Down Expand Up @@ -647,3 +649,36 @@ export function findInterfacesRemovedFromObjectTypes(
});
return breakingChanges;
}

export function findInterfacesAddedToObjectTypes(
oldSchema: GraphQLSchema,
newSchema: GraphQLSchema
): Array<DangerousChange> {
const oldTypeMap = oldSchema.getTypeMap();
const newTypeMap = newSchema.getTypeMap();
const interfacesAddedToObjectTypes = [];

Object.keys(newTypeMap).forEach(typeName => {
const oldType = oldTypeMap[typeName];
const newType = newTypeMap[typeName];
if (
!(oldType instanceof GraphQLObjectType) ||
!(newType instanceof GraphQLObjectType)
) {
return;
}

const oldInterfaces = oldType.getInterfaces();
const newInterfaces = newType.getInterfaces();
newInterfaces.forEach(newInterface => {
if (!oldInterfaces.some(int => int.name === newInterface.name)) {
interfacesAddedToObjectTypes.push({
type: DangerousChangeType.INTERFACE_ADDED_TO_OBJECT,
description: `${newInterface.name} added to interfaces implemented ` +
`by ${typeName}.`
});
}
});
});
return interfacesAddedToObjectTypes;
}