findBreakingChanges.mjs 20 KB

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