1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
import objectValues from "../polyfills/objectValues.mjs";
import keyMap from "../jsutils/keyMap.mjs";
import inspect from "../jsutils/inspect.mjs";
import invariant from "../jsutils/invariant.mjs";
import naturalCompare from "../jsutils/naturalCompare.mjs";
import { print } from "../language/printer.mjs";
import { visit } from "../language/visitor.mjs";
import { isSpecifiedScalarType } from "../type/scalars.mjs";
import { isScalarType, isObjectType, isInterfaceType, isUnionType, isEnumType, isInputObjectType, isNonNullType, isListType, isNamedType, isRequiredArgument, isRequiredInputField } from "../type/definition.mjs";
import { astFromValue } from "./astFromValue.mjs";
export var BreakingChangeType = Object.freeze({
TYPE_REMOVED: 'TYPE_REMOVED',
TYPE_CHANGED_KIND: 'TYPE_CHANGED_KIND',
TYPE_REMOVED_FROM_UNION: 'TYPE_REMOVED_FROM_UNION',
VALUE_REMOVED_FROM_ENUM: 'VALUE_REMOVED_FROM_ENUM',
REQUIRED_INPUT_FIELD_ADDED: 'REQUIRED_INPUT_FIELD_ADDED',
IMPLEMENTED_INTERFACE_REMOVED: 'IMPLEMENTED_INTERFACE_REMOVED',
FIELD_REMOVED: 'FIELD_REMOVED',
FIELD_CHANGED_KIND: 'FIELD_CHANGED_KIND',
REQUIRED_ARG_ADDED: 'REQUIRED_ARG_ADDED',
ARG_REMOVED: 'ARG_REMOVED',
ARG_CHANGED_KIND: 'ARG_CHANGED_KIND',
DIRECTIVE_REMOVED: 'DIRECTIVE_REMOVED',
DIRECTIVE_ARG_REMOVED: 'DIRECTIVE_ARG_REMOVED',
REQUIRED_DIRECTIVE_ARG_ADDED: 'REQUIRED_DIRECTIVE_ARG_ADDED',
DIRECTIVE_REPEATABLE_REMOVED: 'DIRECTIVE_REPEATABLE_REMOVED',
DIRECTIVE_LOCATION_REMOVED: 'DIRECTIVE_LOCATION_REMOVED'
});
export var DangerousChangeType = Object.freeze({
VALUE_ADDED_TO_ENUM: 'VALUE_ADDED_TO_ENUM',
TYPE_ADDED_TO_UNION: 'TYPE_ADDED_TO_UNION',
OPTIONAL_INPUT_FIELD_ADDED: 'OPTIONAL_INPUT_FIELD_ADDED',
OPTIONAL_ARG_ADDED: 'OPTIONAL_ARG_ADDED',
IMPLEMENTED_INTERFACE_ADDED: 'IMPLEMENTED_INTERFACE_ADDED',
ARG_DEFAULT_VALUE_CHANGE: 'ARG_DEFAULT_VALUE_CHANGE'
});
/**
* Given two schemas, returns an Array containing descriptions of all the types
* of breaking changes covered by the other functions down below.
*/
export function findBreakingChanges(oldSchema, newSchema) {
var breakingChanges = findSchemaChanges(oldSchema, newSchema).filter(function (change) {
return change.type in BreakingChangeType;
});
return breakingChanges;
}
/**
* Given two schemas, returns an Array containing descriptions of all the types
* of potentially dangerous changes covered by the other functions down below.
*/
export function findDangerousChanges(oldSchema, newSchema) {
var dangerousChanges = findSchemaChanges(oldSchema, newSchema).filter(function (change) {
return change.type in DangerousChangeType;
});
return dangerousChanges;
}
function findSchemaChanges(oldSchema, newSchema) {
return [].concat(findTypeChanges(oldSchema, newSchema), findDirectiveChanges(oldSchema, newSchema));
}
function findDirectiveChanges(oldSchema, newSchema) {
var schemaChanges = [];
var directivesDiff = diff(oldSchema.getDirectives(), newSchema.getDirectives());
for (var _i2 = 0, _directivesDiff$remov2 = directivesDiff.removed; _i2 < _directivesDiff$remov2.length; _i2++) {
var oldDirective = _directivesDiff$remov2[_i2];
schemaChanges.push({
type: BreakingChangeType.DIRECTIVE_REMOVED,
description: "".concat(oldDirective.name, " was removed.")
});
}
for (var _i4 = 0, _directivesDiff$persi2 = directivesDiff.persisted; _i4 < _directivesDiff$persi2.length; _i4++) {
var _ref2 = _directivesDiff$persi2[_i4];
var _oldDirective = _ref2[0];
var newDirective = _ref2[1];
var argsDiff = diff(_oldDirective.args, newDirective.args);
for (var _i6 = 0, _argsDiff$added2 = argsDiff.added; _i6 < _argsDiff$added2.length; _i6++) {
var newArg = _argsDiff$added2[_i6];
if (isRequiredArgument(newArg)) {
schemaChanges.push({
type: BreakingChangeType.REQUIRED_DIRECTIVE_ARG_ADDED,
description: "A required arg ".concat(newArg.name, " on directive ").concat(_oldDirective.name, " was added.")
});
}
}
for (var _i8 = 0, _argsDiff$removed2 = argsDiff.removed; _i8 < _argsDiff$removed2.length; _i8++) {
var oldArg = _argsDiff$removed2[_i8];
schemaChanges.push({
type: BreakingChangeType.DIRECTIVE_ARG_REMOVED,
description: "".concat(oldArg.name, " was removed from ").concat(_oldDirective.name, ".")
});
}
if (_oldDirective.isRepeatable && !newDirective.isRepeatable) {
schemaChanges.push({
type: BreakingChangeType.DIRECTIVE_REPEATABLE_REMOVED,
description: "Repeatable flag was removed from ".concat(_oldDirective.name, ".")
});
}
for (var _i10 = 0, _oldDirective$locatio2 = _oldDirective.locations; _i10 < _oldDirective$locatio2.length; _i10++) {
var location = _oldDirective$locatio2[_i10];
if (newDirective.locations.indexOf(location) === -1) {
schemaChanges.push({
type: BreakingChangeType.DIRECTIVE_LOCATION_REMOVED,
description: "".concat(location, " was removed from ").concat(_oldDirective.name, ".")
});
}
}
}
return schemaChanges;
}
function findTypeChanges(oldSchema, newSchema) {
var schemaChanges = [];
var typesDiff = diff(objectValues(oldSchema.getTypeMap()), objectValues(newSchema.getTypeMap()));
for (var _i12 = 0, _typesDiff$removed2 = typesDiff.removed; _i12 < _typesDiff$removed2.length; _i12++) {
var oldType = _typesDiff$removed2[_i12];
schemaChanges.push({
type: BreakingChangeType.TYPE_REMOVED,
description: isSpecifiedScalarType(oldType) ? "Standard scalar ".concat(oldType.name, " was removed because it is not referenced anymore.") : "".concat(oldType.name, " was removed.")
});
}
for (var _i14 = 0, _typesDiff$persisted2 = typesDiff.persisted; _i14 < _typesDiff$persisted2.length; _i14++) {
var _ref4 = _typesDiff$persisted2[_i14];
var _oldType = _ref4[0];
var newType = _ref4[1];
if (isEnumType(_oldType) && isEnumType(newType)) {
schemaChanges.push.apply(schemaChanges, findEnumTypeChanges(_oldType, newType));
} else if (isUnionType(_oldType) && isUnionType(newType)) {
schemaChanges.push.apply(schemaChanges, findUnionTypeChanges(_oldType, newType));
} else if (isInputObjectType(_oldType) && isInputObjectType(newType)) {
schemaChanges.push.apply(schemaChanges, findInputObjectTypeChanges(_oldType, newType));
} else if (isObjectType(_oldType) && isObjectType(newType)) {
schemaChanges.push.apply(schemaChanges, findFieldChanges(_oldType, newType).concat(findImplementedInterfacesChanges(_oldType, newType)));
} else if (isInterfaceType(_oldType) && isInterfaceType(newType)) {
schemaChanges.push.apply(schemaChanges, findFieldChanges(_oldType, newType).concat(findImplementedInterfacesChanges(_oldType, newType)));
} else if (_oldType.constructor !== newType.constructor) {
schemaChanges.push({
type: BreakingChangeType.TYPE_CHANGED_KIND,
description: "".concat(_oldType.name, " changed from ") + "".concat(typeKindName(_oldType), " to ").concat(typeKindName(newType), ".")
});
}
}
return schemaChanges;
}
function findInputObjectTypeChanges(oldType, newType) {
var schemaChanges = [];
var fieldsDiff = diff(objectValues(oldType.getFields()), objectValues(newType.getFields()));
for (var _i16 = 0, _fieldsDiff$added2 = fieldsDiff.added; _i16 < _fieldsDiff$added2.length; _i16++) {
var newField = _fieldsDiff$added2[_i16];
if (isRequiredInputField(newField)) {
schemaChanges.push({
type: BreakingChangeType.REQUIRED_INPUT_FIELD_ADDED,
description: "A required field ".concat(newField.name, " on input type ").concat(oldType.name, " was added.")
});
} else {
schemaChanges.push({
type: DangerousChangeType.OPTIONAL_INPUT_FIELD_ADDED,
description: "An optional field ".concat(newField.name, " on input type ").concat(oldType.name, " was added.")
});
}
}
for (var _i18 = 0, _fieldsDiff$removed2 = fieldsDiff.removed; _i18 < _fieldsDiff$removed2.length; _i18++) {
var oldField = _fieldsDiff$removed2[_i18];
schemaChanges.push({
type: BreakingChangeType.FIELD_REMOVED,
description: "".concat(oldType.name, ".").concat(oldField.name, " was removed.")
});
}
for (var _i20 = 0, _fieldsDiff$persisted2 = fieldsDiff.persisted; _i20 < _fieldsDiff$persisted2.length; _i20++) {
var _ref6 = _fieldsDiff$persisted2[_i20];
var _oldField = _ref6[0];
var _newField = _ref6[1];
var isSafe = isChangeSafeForInputObjectFieldOrFieldArg(_oldField.type, _newField.type);
if (!isSafe) {
schemaChanges.push({
type: BreakingChangeType.FIELD_CHANGED_KIND,
description: "".concat(oldType.name, ".").concat(_oldField.name, " changed type from ") + "".concat(String(_oldField.type), " to ").concat(String(_newField.type), ".")
});
}
}
return schemaChanges;
}
function findUnionTypeChanges(oldType, newType) {
var schemaChanges = [];
var possibleTypesDiff = diff(oldType.getTypes(), newType.getTypes());
for (var _i22 = 0, _possibleTypesDiff$ad2 = possibleTypesDiff.added; _i22 < _possibleTypesDiff$ad2.length; _i22++) {
var newPossibleType = _possibleTypesDiff$ad2[_i22];
schemaChanges.push({
type: DangerousChangeType.TYPE_ADDED_TO_UNION,
description: "".concat(newPossibleType.name, " was added to union type ").concat(oldType.name, ".")
});
}
for (var _i24 = 0, _possibleTypesDiff$re2 = possibleTypesDiff.removed; _i24 < _possibleTypesDiff$re2.length; _i24++) {
var oldPossibleType = _possibleTypesDiff$re2[_i24];
schemaChanges.push({
type: BreakingChangeType.TYPE_REMOVED_FROM_UNION,
description: "".concat(oldPossibleType.name, " was removed from union type ").concat(oldType.name, ".")
});
}
return schemaChanges;
}
function findEnumTypeChanges(oldType, newType) {
var schemaChanges = [];
var valuesDiff = diff(oldType.getValues(), newType.getValues());
for (var _i26 = 0, _valuesDiff$added2 = valuesDiff.added; _i26 < _valuesDiff$added2.length; _i26++) {
var newValue = _valuesDiff$added2[_i26];
schemaChanges.push({
type: DangerousChangeType.VALUE_ADDED_TO_ENUM,
description: "".concat(newValue.name, " was added to enum type ").concat(oldType.name, ".")
});
}
for (var _i28 = 0, _valuesDiff$removed2 = valuesDiff.removed; _i28 < _valuesDiff$removed2.length; _i28++) {
var oldValue = _valuesDiff$removed2[_i28];
schemaChanges.push({
type: BreakingChangeType.VALUE_REMOVED_FROM_ENUM,
description: "".concat(oldValue.name, " was removed from enum type ").concat(oldType.name, ".")
});
}
return schemaChanges;
}
function findImplementedInterfacesChanges(oldType, newType) {
var schemaChanges = [];
var interfacesDiff = diff(oldType.getInterfaces(), newType.getInterfaces());
for (var _i30 = 0, _interfacesDiff$added2 = interfacesDiff.added; _i30 < _interfacesDiff$added2.length; _i30++) {
var newInterface = _interfacesDiff$added2[_i30];
schemaChanges.push({
type: DangerousChangeType.IMPLEMENTED_INTERFACE_ADDED,
description: "".concat(newInterface.name, " added to interfaces implemented by ").concat(oldType.name, ".")
});
}
for (var _i32 = 0, _interfacesDiff$remov2 = interfacesDiff.removed; _i32 < _interfacesDiff$remov2.length; _i32++) {
var oldInterface = _interfacesDiff$remov2[_i32];
schemaChanges.push({
type: BreakingChangeType.IMPLEMENTED_INTERFACE_REMOVED,
description: "".concat(oldType.name, " no longer implements interface ").concat(oldInterface.name, ".")
});
}
return schemaChanges;
}
function findFieldChanges(oldType, newType) {
var schemaChanges = [];
var fieldsDiff = diff(objectValues(oldType.getFields()), objectValues(newType.getFields()));
for (var _i34 = 0, _fieldsDiff$removed4 = fieldsDiff.removed; _i34 < _fieldsDiff$removed4.length; _i34++) {
var oldField = _fieldsDiff$removed4[_i34];
schemaChanges.push({
type: BreakingChangeType.FIELD_REMOVED,
description: "".concat(oldType.name, ".").concat(oldField.name, " was removed.")
});
}
for (var _i36 = 0, _fieldsDiff$persisted4 = fieldsDiff.persisted; _i36 < _fieldsDiff$persisted4.length; _i36++) {
var _ref8 = _fieldsDiff$persisted4[_i36];
var _oldField2 = _ref8[0];
var newField = _ref8[1];
schemaChanges.push.apply(schemaChanges, findArgChanges(oldType, _oldField2, newField));
var isSafe = isChangeSafeForObjectOrInterfaceField(_oldField2.type, newField.type);
if (!isSafe) {
schemaChanges.push({
type: BreakingChangeType.FIELD_CHANGED_KIND,
description: "".concat(oldType.name, ".").concat(_oldField2.name, " changed type from ") + "".concat(String(_oldField2.type), " to ").concat(String(newField.type), ".")
});
}
}
return schemaChanges;
}
function findArgChanges(oldType, oldField, newField) {
var schemaChanges = [];
var argsDiff = diff(oldField.args, newField.args);
for (var _i38 = 0, _argsDiff$removed4 = argsDiff.removed; _i38 < _argsDiff$removed4.length; _i38++) {
var oldArg = _argsDiff$removed4[_i38];
schemaChanges.push({
type: BreakingChangeType.ARG_REMOVED,
description: "".concat(oldType.name, ".").concat(oldField.name, " arg ").concat(oldArg.name, " was removed.")
});
}
for (var _i40 = 0, _argsDiff$persisted2 = argsDiff.persisted; _i40 < _argsDiff$persisted2.length; _i40++) {
var _ref10 = _argsDiff$persisted2[_i40];
var _oldArg = _ref10[0];
var newArg = _ref10[1];
var isSafe = isChangeSafeForInputObjectFieldOrFieldArg(_oldArg.type, newArg.type);
if (!isSafe) {
schemaChanges.push({
type: BreakingChangeType.ARG_CHANGED_KIND,
description: "".concat(oldType.name, ".").concat(oldField.name, " arg ").concat(_oldArg.name, " has changed type from ") + "".concat(String(_oldArg.type), " to ").concat(String(newArg.type), ".")
});
} else if (_oldArg.defaultValue !== undefined) {
if (newArg.defaultValue === undefined) {
schemaChanges.push({
type: DangerousChangeType.ARG_DEFAULT_VALUE_CHANGE,
description: "".concat(oldType.name, ".").concat(oldField.name, " arg ").concat(_oldArg.name, " defaultValue was removed.")
});
} else {
// Since we looking only for client's observable changes we should
// compare default values in the same representation as they are
// represented inside introspection.
var oldValueStr = stringifyValue(_oldArg.defaultValue, _oldArg.type);
var newValueStr = stringifyValue(newArg.defaultValue, newArg.type);
if (oldValueStr !== newValueStr) {
schemaChanges.push({
type: DangerousChangeType.ARG_DEFAULT_VALUE_CHANGE,
description: "".concat(oldType.name, ".").concat(oldField.name, " arg ").concat(_oldArg.name, " has changed defaultValue from ").concat(oldValueStr, " to ").concat(newValueStr, ".")
});
}
}
}
}
for (var _i42 = 0, _argsDiff$added4 = argsDiff.added; _i42 < _argsDiff$added4.length; _i42++) {
var _newArg = _argsDiff$added4[_i42];
if (isRequiredArgument(_newArg)) {
schemaChanges.push({
type: BreakingChangeType.REQUIRED_ARG_ADDED,
description: "A required arg ".concat(_newArg.name, " on ").concat(oldType.name, ".").concat(oldField.name, " was added.")
});
} else {
schemaChanges.push({
type: DangerousChangeType.OPTIONAL_ARG_ADDED,
description: "An optional arg ".concat(_newArg.name, " on ").concat(oldType.name, ".").concat(oldField.name, " was added.")
});
}
}
return schemaChanges;
}
function isChangeSafeForObjectOrInterfaceField(oldType, newType) {
if (isListType(oldType)) {
return (// if they're both lists, make sure the underlying types are compatible
isListType(newType) && isChangeSafeForObjectOrInterfaceField(oldType.ofType, newType.ofType) || // moving from nullable to non-null of the same underlying type is safe
isNonNullType(newType) && isChangeSafeForObjectOrInterfaceField(oldType, newType.ofType)
);
}
if (isNonNullType(oldType)) {
// if they're both non-null, make sure the underlying types are compatible
return isNonNullType(newType) && isChangeSafeForObjectOrInterfaceField(oldType.ofType, newType.ofType);
}
return (// if they're both named types, see if their names are equivalent
isNamedType(newType) && oldType.name === newType.name || // moving from nullable to non-null of the same underlying type is safe
isNonNullType(newType) && isChangeSafeForObjectOrInterfaceField(oldType, newType.ofType)
);
}
function isChangeSafeForInputObjectFieldOrFieldArg(oldType, newType) {
if (isListType(oldType)) {
// if they're both lists, make sure the underlying types are compatible
return isListType(newType) && isChangeSafeForInputObjectFieldOrFieldArg(oldType.ofType, newType.ofType);
}
if (isNonNullType(oldType)) {
return (// if they're both non-null, make sure the underlying types are
// compatible
isNonNullType(newType) && isChangeSafeForInputObjectFieldOrFieldArg(oldType.ofType, newType.ofType) || // moving from non-null to nullable of the same underlying type is safe
!isNonNullType(newType) && isChangeSafeForInputObjectFieldOrFieldArg(oldType.ofType, newType)
);
} // if they're both named types, see if their names are equivalent
return isNamedType(newType) && oldType.name === newType.name;
}
function typeKindName(type) {
if (isScalarType(type)) {
return 'a Scalar type';
}
if (isObjectType(type)) {
return 'an Object type';
}
if (isInterfaceType(type)) {
return 'an Interface type';
}
if (isUnionType(type)) {
return 'a Union type';
}
if (isEnumType(type)) {
return 'an Enum type';
} // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618')
if (isInputObjectType(type)) {
return 'an Input type';
} // istanbul ignore next (Not reachable. All possible named types have been considered)
false || invariant(0, 'Unexpected type: ' + inspect(type));
}
function stringifyValue(value, type) {
var ast = astFromValue(value, type);
ast != null || invariant(0);
var sortedAST = visit(ast, {
ObjectValue: function ObjectValue(objectNode) {
// Make a copy since sort mutates array
var fields = [].concat(objectNode.fields);
fields.sort(function (fieldA, fieldB) {
return naturalCompare(fieldA.name.value, fieldB.name.value);
});
return _objectSpread(_objectSpread({}, objectNode), {}, {
fields: fields
});
}
});
return print(sortedAST);
}
function diff(oldArray, newArray) {
var added = [];
var removed = [];
var persisted = [];
var oldMap = keyMap(oldArray, function (_ref11) {
var name = _ref11.name;
return name;
});
var newMap = keyMap(newArray, function (_ref12) {
var name = _ref12.name;
return name;
});
for (var _i44 = 0; _i44 < oldArray.length; _i44++) {
var oldItem = oldArray[_i44];
var newItem = newMap[oldItem.name];
if (newItem === undefined) {
removed.push(oldItem);
} else {
persisted.push([oldItem, newItem]);
}
}
for (var _i46 = 0; _i46 < newArray.length; _i46++) {
var _newItem = newArray[_i46];
if (oldMap[_newItem.name] === undefined) {
added.push(_newItem);
}
}
return {
added: added,
persisted: persisted,
removed: removed
};
}
|