Skip to content

Commit c019734

Browse files
committed
Interface
1 parent 8703d6b commit c019734

File tree

6 files changed

+85
-14
lines changed

6 files changed

+85
-14
lines changed

src/changes/argument.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { GraphQLArgument, GraphQLObjectType, GraphQLField } from 'graphql';
1+
import { GraphQLArgument, GraphQLObjectType, GraphQLField, GraphQLInterfaceType } from 'graphql';
22

33
import { Change, CriticalityLevel } from './change';
44
import { safeChangeForInputValue } from '../utils/graphql';
55

66
export function fieldArgumentDescriptionChanged(
7-
type: GraphQLObjectType,
7+
type: GraphQLObjectType | GraphQLInterfaceType,
88
field: GraphQLField<any, any, any>,
99
oldArg: GraphQLArgument,
1010
newArg: GraphQLArgument,
@@ -23,7 +23,7 @@ export function fieldArgumentDescriptionChanged(
2323
}
2424

2525
export function fieldArgumentDefaultChanged(
26-
type: GraphQLObjectType,
26+
type: GraphQLObjectType | GraphQLInterfaceType,
2727
field: GraphQLField<any, any, any>,
2828
oldArg: GraphQLArgument,
2929
newArg: GraphQLArgument,
@@ -47,7 +47,7 @@ export function fieldArgumentDefaultChanged(
4747
}
4848

4949
export function fieldArgumentTypeChanged(
50-
type: GraphQLObjectType,
50+
type: GraphQLObjectType | GraphQLInterfaceType,
5151
field: GraphQLField<any, any, any>,
5252
oldArg: GraphQLArgument,
5353
newArg: GraphQLArgument,

src/changes/field.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import {
33
GraphQLField,
44
GraphQLArgument,
55
isNonNullType,
6+
GraphQLInterfaceType,
67
} from 'graphql';
78

89
import { Change, CriticalityLevel } from './change';
910
import { safeChangeForField } from '../utils/graphql';
1011

1112
export function fieldRemoved(
12-
type: GraphQLObjectType,
13+
type: GraphQLObjectType | GraphQLInterfaceType,
1314
field: GraphQLField<any, any, any>,
1415
): Change {
1516
return {
@@ -27,7 +28,7 @@ export function fieldRemoved(
2728
}
2829

2930
export function fieldAdded(
30-
type: GraphQLObjectType,
31+
type: GraphQLObjectType | GraphQLInterfaceType,
3132
field: GraphQLField<any, any, any>,
3233
): Change {
3334
return {
@@ -40,7 +41,7 @@ export function fieldAdded(
4041
}
4142

4243
export function fieldDescriptionChanged(
43-
type: GraphQLObjectType,
44+
type: GraphQLObjectType | GraphQLInterfaceType,
4445
oldField: GraphQLField<any, any>,
4546
newField: GraphQLField<any, any>,
4647
): Change {
@@ -56,7 +57,7 @@ export function fieldDescriptionChanged(
5657
}
5758

5859
export function fieldDeprecationReasonChanged(
59-
type: GraphQLObjectType,
60+
type: GraphQLObjectType | GraphQLInterfaceType,
6061
oldField: GraphQLField<any, any>,
6162
newField: GraphQLField<any, any>,
6263
): Change {
@@ -74,7 +75,7 @@ export function fieldDeprecationReasonChanged(
7475
}
7576

7677
export function fieldTypeChanged(
77-
type: GraphQLObjectType,
78+
type: GraphQLObjectType | GraphQLInterfaceType,
7879
oldField: GraphQLField<any, any, any>,
7980
newField: GraphQLField<any, any, any>,
8081
): Change {
@@ -92,7 +93,7 @@ export function fieldTypeChanged(
9293
}
9394

9495
export function fieldArgumentAdded(
95-
type: GraphQLObjectType,
96+
type: GraphQLObjectType | GraphQLInterfaceType,
9697
field: GraphQLField<any, any, any>,
9798
arg: GraphQLArgument,
9899
): Change {
@@ -113,7 +114,7 @@ export function fieldArgumentAdded(
113114
}
114115

115116
export function fieldArgumentRemoved(
116-
type: GraphQLObjectType,
117+
type: GraphQLObjectType | GraphQLInterfaceType,
117118
field: GraphQLField<any, any, any>,
118119
arg: GraphQLArgument,
119120
): Change {

src/diff/argument.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
GraphQLObjectType,
44
GraphQLField,
55
isEqualType,
6+
GraphQLInterfaceType,
67
} from 'graphql';
78

89
import { Change } from '../changes/change';
@@ -13,7 +14,7 @@ import {
1314
} from '../changes/argument';
1415

1516
export function changesInArgument(
16-
type: GraphQLObjectType,
17+
type: GraphQLObjectType | GraphQLInterfaceType,
1718
field: GraphQLField<any, any, any>,
1819
oldArg: GraphQLArgument,
1920
newArg: GraphQLArgument,

src/diff/field.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
GraphQLObjectType,
44
GraphQLArgument,
55
isEqualType,
6+
GraphQLInterfaceType,
67
} from 'graphql';
78

89
import { Change } from '../changes/change';
@@ -17,7 +18,7 @@ import { changesInArgument } from './argument';
1718
import { unionArrays, diffArrays } from '../utils/arrays';
1819

1920
export function changesInField(
20-
type: GraphQLObjectType,
21+
type: GraphQLObjectType | GraphQLInterfaceType,
2122
oldField: GraphQLField<any, any>,
2223
newField: GraphQLField<any, any>,
2324
): Change[] {

src/diff/interface.ts

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { GraphQLInterfaceType } from 'graphql';
2+
3+
import { Change } from '../changes/change';
4+
import { fieldRemoved, fieldAdded } from '../changes/field';
5+
import { changesInField } from './field';
6+
import { diffArrays, unionArrays } from '../utils/arrays';
7+
8+
export function changesInInterface(
9+
oldInterface: GraphQLInterfaceType,
10+
newInterface: GraphQLInterfaceType,
11+
): Change[] {
12+
const changes: Change[] = [];
13+
14+
changes.push(...addedFields(oldInterface, newInterface));
15+
changes.push(...removedFields(oldInterface, newInterface));
16+
changedFields(oldInterface, newInterface).forEach(({ inOld, inNew }) => {
17+
changes.push(...changesInField(oldInterface, inOld, inNew));
18+
});
19+
20+
return changes;
21+
}
22+
23+
function addedFields(
24+
oldInterface: GraphQLInterfaceType,
25+
newInterface: GraphQLInterfaceType,
26+
): Change[] {
27+
const oldFields = oldInterface.getFields();
28+
const newFields = newInterface.getFields();
29+
const oldNames = Object.keys(oldFields);
30+
const newNames = Object.keys(newFields);
31+
32+
return diffArrays(newNames, oldNames)
33+
.map(name => newFields[name])
34+
.map(f => fieldAdded(newInterface, f));
35+
}
36+
37+
function removedFields(
38+
oldInterface: GraphQLInterfaceType,
39+
newInterface: GraphQLInterfaceType,
40+
): Change[] {
41+
const oldFields = oldInterface.getFields();
42+
const newFields = newInterface.getFields();
43+
const oldNames = Object.keys(oldFields);
44+
const newNames = Object.keys(newFields);
45+
46+
return diffArrays(oldNames, newNames)
47+
.map(name => newFields[name])
48+
.map(f => fieldRemoved(oldInterface, f));
49+
}
50+
51+
function changedFields(
52+
oldInterface: GraphQLInterfaceType,
53+
newInterface: GraphQLInterfaceType,
54+
) {
55+
const oldFields = oldInterface.getFields();
56+
const newFields = newInterface.getFields();
57+
const oldNames = Object.keys(oldFields);
58+
const newNames = Object.keys(newFields);
59+
60+
return unionArrays(oldNames, newNames).map(name => ({
61+
inOld: oldFields[name],
62+
inNew: newFields[name],
63+
}));
64+
}

src/diff/schema.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
isInputObjectType,
1212
isObjectType,
1313
isInterfaceType,
14+
GraphQLInterfaceType,
1415
} from 'graphql';
1516

1617
import { unionArrays, diffArrays } from '../utils/arrays';
@@ -32,6 +33,7 @@ import { changesInEnum } from './enum';
3233
import { changesInUnion } from './union';
3334
import { changesInInputObject } from './input';
3435
import { changesInObject } from './object';
36+
import { changesInInterface } from './interface';
3537

3638
export function diff(
3739
oldSchema: GraphQLSchema,
@@ -184,7 +186,9 @@ function changesInType(
184186
} else if (isObjectType(oldType)) {
185187
changes.push(...changesInObject(oldType, newType as GraphQLObjectType));
186188
} else if (isInterfaceType(oldType)) {
187-
// TODO: interface
189+
changes.push(
190+
...changesInInterface(oldType, newType as GraphQLInterfaceType),
191+
);
188192
}
189193
}
190194

0 commit comments

Comments
 (0)