findBreakingChanges.mjs 20 KB

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