OverlappingFieldsCanBeMergedRule.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.OverlappingFieldsCanBeMergedRule = OverlappingFieldsCanBeMergedRule;
  6. var _find = _interopRequireDefault(require("../../polyfills/find.js"));
  7. var _objectEntries3 = _interopRequireDefault(require("../../polyfills/objectEntries.js"));
  8. var _inspect = _interopRequireDefault(require("../../jsutils/inspect.js"));
  9. var _GraphQLError = require("../../error/GraphQLError.js");
  10. var _kinds = require("../../language/kinds.js");
  11. var _printer = require("../../language/printer.js");
  12. var _definition = require("../../type/definition.js");
  13. var _typeFromAST = require("../../utilities/typeFromAST.js");
  14. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  15. function reasonMessage(reason) {
  16. if (Array.isArray(reason)) {
  17. return reason.map(function (_ref) {
  18. var responseName = _ref[0],
  19. subReason = _ref[1];
  20. return "subfields \"".concat(responseName, "\" conflict because ") + reasonMessage(subReason);
  21. }).join(' and ');
  22. }
  23. return reason;
  24. }
  25. /**
  26. * Overlapping fields can be merged
  27. *
  28. * A selection set is only valid if all fields (including spreading any
  29. * fragments) either correspond to distinct response names or can be merged
  30. * without ambiguity.
  31. */
  32. function OverlappingFieldsCanBeMergedRule(context) {
  33. // A memoization for when two fragments are compared "between" each other for
  34. // conflicts. Two fragments may be compared many times, so memoizing this can
  35. // dramatically improve the performance of this validator.
  36. var comparedFragmentPairs = new PairSet(); // A cache for the "field map" and list of fragment names found in any given
  37. // selection set. Selection sets may be asked for this information multiple
  38. // times, so this improves the performance of this validator.
  39. var cachedFieldsAndFragmentNames = new Map();
  40. return {
  41. SelectionSet: function SelectionSet(selectionSet) {
  42. var conflicts = findConflictsWithinSelectionSet(context, cachedFieldsAndFragmentNames, comparedFragmentPairs, context.getParentType(), selectionSet);
  43. for (var _i2 = 0; _i2 < conflicts.length; _i2++) {
  44. var _ref3 = conflicts[_i2];
  45. var _ref2$ = _ref3[0];
  46. var responseName = _ref2$[0];
  47. var reason = _ref2$[1];
  48. var fields1 = _ref3[1];
  49. var fields2 = _ref3[2];
  50. var reasonMsg = reasonMessage(reason);
  51. context.reportError(new _GraphQLError.GraphQLError("Fields \"".concat(responseName, "\" conflict because ").concat(reasonMsg, ". Use different aliases on the fields to fetch both if this was intentional."), fields1.concat(fields2)));
  52. }
  53. }
  54. };
  55. }
  56. /**
  57. * Algorithm:
  58. *
  59. * Conflicts occur when two fields exist in a query which will produce the same
  60. * response name, but represent differing values, thus creating a conflict.
  61. * The algorithm below finds all conflicts via making a series of comparisons
  62. * between fields. In order to compare as few fields as possible, this makes
  63. * a series of comparisons "within" sets of fields and "between" sets of fields.
  64. *
  65. * Given any selection set, a collection produces both a set of fields by
  66. * also including all inline fragments, as well as a list of fragments
  67. * referenced by fragment spreads.
  68. *
  69. * A) Each selection set represented in the document first compares "within" its
  70. * collected set of fields, finding any conflicts between every pair of
  71. * overlapping fields.
  72. * Note: This is the *only time* that a the fields "within" a set are compared
  73. * to each other. After this only fields "between" sets are compared.
  74. *
  75. * B) Also, if any fragment is referenced in a selection set, then a
  76. * comparison is made "between" the original set of fields and the
  77. * referenced fragment.
  78. *
  79. * C) Also, if multiple fragments are referenced, then comparisons
  80. * are made "between" each referenced fragment.
  81. *
  82. * D) When comparing "between" a set of fields and a referenced fragment, first
  83. * a comparison is made between each field in the original set of fields and
  84. * each field in the the referenced set of fields.
  85. *
  86. * E) Also, if any fragment is referenced in the referenced selection set,
  87. * then a comparison is made "between" the original set of fields and the
  88. * referenced fragment (recursively referring to step D).
  89. *
  90. * F) When comparing "between" two fragments, first a comparison is made between
  91. * each field in the first referenced set of fields and each field in the the
  92. * second referenced set of fields.
  93. *
  94. * G) Also, any fragments referenced by the first must be compared to the
  95. * second, and any fragments referenced by the second must be compared to the
  96. * first (recursively referring to step F).
  97. *
  98. * H) When comparing two fields, if both have selection sets, then a comparison
  99. * is made "between" both selection sets, first comparing the set of fields in
  100. * the first selection set with the set of fields in the second.
  101. *
  102. * I) Also, if any fragment is referenced in either selection set, then a
  103. * comparison is made "between" the other set of fields and the
  104. * referenced fragment.
  105. *
  106. * J) Also, if two fragments are referenced in both selection sets, then a
  107. * comparison is made "between" the two fragments.
  108. *
  109. */
  110. // Find all conflicts found "within" a selection set, including those found
  111. // via spreading in fragments. Called when visiting each SelectionSet in the
  112. // GraphQL Document.
  113. function findConflictsWithinSelectionSet(context, cachedFieldsAndFragmentNames, comparedFragmentPairs, parentType, selectionSet) {
  114. var conflicts = [];
  115. var _getFieldsAndFragment = getFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, parentType, selectionSet),
  116. fieldMap = _getFieldsAndFragment[0],
  117. fragmentNames = _getFieldsAndFragment[1]; // (A) Find find all conflicts "within" the fields of this selection set.
  118. // Note: this is the *only place* `collectConflictsWithin` is called.
  119. collectConflictsWithin(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, fieldMap);
  120. if (fragmentNames.length !== 0) {
  121. // (B) Then collect conflicts between these fields and those represented by
  122. // each spread fragment name found.
  123. for (var i = 0; i < fragmentNames.length; i++) {
  124. collectConflictsBetweenFieldsAndFragment(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, false, fieldMap, fragmentNames[i]); // (C) Then compare this fragment with all other fragments found in this
  125. // selection set to collect conflicts between fragments spread together.
  126. // This compares each item in the list of fragment names to every other
  127. // item in that same list (except for itself).
  128. for (var j = i + 1; j < fragmentNames.length; j++) {
  129. collectConflictsBetweenFragments(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, false, fragmentNames[i], fragmentNames[j]);
  130. }
  131. }
  132. }
  133. return conflicts;
  134. } // Collect all conflicts found between a set of fields and a fragment reference
  135. // including via spreading in any nested fragments.
  136. function collectConflictsBetweenFieldsAndFragment(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fieldMap, fragmentName) {
  137. var fragment = context.getFragment(fragmentName);
  138. if (!fragment) {
  139. return;
  140. }
  141. var _getReferencedFieldsA = getReferencedFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, fragment),
  142. fieldMap2 = _getReferencedFieldsA[0],
  143. fragmentNames2 = _getReferencedFieldsA[1]; // Do not compare a fragment's fieldMap to itself.
  144. if (fieldMap === fieldMap2) {
  145. return;
  146. } // (D) First collect any conflicts between the provided collection of fields
  147. // and the collection of fields represented by the given fragment.
  148. collectConflictsBetween(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fieldMap, fieldMap2); // (E) Then collect any conflicts between the provided collection of fields
  149. // and any fragment names found in the given fragment.
  150. for (var i = 0; i < fragmentNames2.length; i++) {
  151. collectConflictsBetweenFieldsAndFragment(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fieldMap, fragmentNames2[i]);
  152. }
  153. } // Collect all conflicts found between two fragments, including via spreading in
  154. // any nested fragments.
  155. function collectConflictsBetweenFragments(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fragmentName1, fragmentName2) {
  156. // No need to compare a fragment to itself.
  157. if (fragmentName1 === fragmentName2) {
  158. return;
  159. } // Memoize so two fragments are not compared for conflicts more than once.
  160. if (comparedFragmentPairs.has(fragmentName1, fragmentName2, areMutuallyExclusive)) {
  161. return;
  162. }
  163. comparedFragmentPairs.add(fragmentName1, fragmentName2, areMutuallyExclusive);
  164. var fragment1 = context.getFragment(fragmentName1);
  165. var fragment2 = context.getFragment(fragmentName2);
  166. if (!fragment1 || !fragment2) {
  167. return;
  168. }
  169. var _getReferencedFieldsA2 = getReferencedFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, fragment1),
  170. fieldMap1 = _getReferencedFieldsA2[0],
  171. fragmentNames1 = _getReferencedFieldsA2[1];
  172. var _getReferencedFieldsA3 = getReferencedFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, fragment2),
  173. fieldMap2 = _getReferencedFieldsA3[0],
  174. fragmentNames2 = _getReferencedFieldsA3[1]; // (F) First, collect all conflicts between these two collections of fields
  175. // (not including any nested fragments).
  176. collectConflictsBetween(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fieldMap1, fieldMap2); // (G) Then collect conflicts between the first fragment and any nested
  177. // fragments spread in the second fragment.
  178. for (var j = 0; j < fragmentNames2.length; j++) {
  179. collectConflictsBetweenFragments(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fragmentName1, fragmentNames2[j]);
  180. } // (G) Then collect conflicts between the second fragment and any nested
  181. // fragments spread in the first fragment.
  182. for (var i = 0; i < fragmentNames1.length; i++) {
  183. collectConflictsBetweenFragments(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fragmentNames1[i], fragmentName2);
  184. }
  185. } // Find all conflicts found between two selection sets, including those found
  186. // via spreading in fragments. Called when determining if conflicts exist
  187. // between the sub-fields of two overlapping fields.
  188. function findConflictsBetweenSubSelectionSets(context, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, parentType1, selectionSet1, parentType2, selectionSet2) {
  189. var conflicts = [];
  190. var _getFieldsAndFragment2 = getFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, parentType1, selectionSet1),
  191. fieldMap1 = _getFieldsAndFragment2[0],
  192. fragmentNames1 = _getFieldsAndFragment2[1];
  193. var _getFieldsAndFragment3 = getFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, parentType2, selectionSet2),
  194. fieldMap2 = _getFieldsAndFragment3[0],
  195. fragmentNames2 = _getFieldsAndFragment3[1]; // (H) First, collect all conflicts between these two collections of field.
  196. collectConflictsBetween(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fieldMap1, fieldMap2); // (I) Then collect conflicts between the first collection of fields and
  197. // those referenced by each fragment name associated with the second.
  198. if (fragmentNames2.length !== 0) {
  199. for (var j = 0; j < fragmentNames2.length; j++) {
  200. collectConflictsBetweenFieldsAndFragment(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fieldMap1, fragmentNames2[j]);
  201. }
  202. } // (I) Then collect conflicts between the second collection of fields and
  203. // those referenced by each fragment name associated with the first.
  204. if (fragmentNames1.length !== 0) {
  205. for (var i = 0; i < fragmentNames1.length; i++) {
  206. collectConflictsBetweenFieldsAndFragment(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fieldMap2, fragmentNames1[i]);
  207. }
  208. } // (J) Also collect conflicts between any fragment names by the first and
  209. // fragment names by the second. This compares each item in the first set of
  210. // names to each item in the second set of names.
  211. for (var _i3 = 0; _i3 < fragmentNames1.length; _i3++) {
  212. for (var _j = 0; _j < fragmentNames2.length; _j++) {
  213. collectConflictsBetweenFragments(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fragmentNames1[_i3], fragmentNames2[_j]);
  214. }
  215. }
  216. return conflicts;
  217. } // Collect all Conflicts "within" one collection of fields.
  218. function collectConflictsWithin(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, fieldMap) {
  219. // A field map is a keyed collection, where each key represents a response
  220. // name and the value at that key is a list of all fields which provide that
  221. // response name. For every response name, if there are multiple fields, they
  222. // must be compared to find a potential conflict.
  223. for (var _i5 = 0, _objectEntries2 = (0, _objectEntries3.default)(fieldMap); _i5 < _objectEntries2.length; _i5++) {
  224. var _ref5 = _objectEntries2[_i5];
  225. var responseName = _ref5[0];
  226. var fields = _ref5[1];
  227. // This compares every field in the list to every other field in this list
  228. // (except to itself). If the list only has one item, nothing needs to
  229. // be compared.
  230. if (fields.length > 1) {
  231. for (var i = 0; i < fields.length; i++) {
  232. for (var j = i + 1; j < fields.length; j++) {
  233. var conflict = findConflict(context, cachedFieldsAndFragmentNames, comparedFragmentPairs, false, // within one collection is never mutually exclusive
  234. responseName, fields[i], fields[j]);
  235. if (conflict) {
  236. conflicts.push(conflict);
  237. }
  238. }
  239. }
  240. }
  241. }
  242. } // Collect all Conflicts between two collections of fields. This is similar to,
  243. // but different from the `collectConflictsWithin` function above. This check
  244. // assumes that `collectConflictsWithin` has already been called on each
  245. // provided collection of fields. This is true because this validator traverses
  246. // each individual selection set.
  247. function collectConflictsBetween(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, parentFieldsAreMutuallyExclusive, fieldMap1, fieldMap2) {
  248. // A field map is a keyed collection, where each key represents a response
  249. // name and the value at that key is a list of all fields which provide that
  250. // response name. For any response name which appears in both provided field
  251. // maps, each field from the first field map must be compared to every field
  252. // in the second field map to find potential conflicts.
  253. for (var _i7 = 0, _Object$keys2 = Object.keys(fieldMap1); _i7 < _Object$keys2.length; _i7++) {
  254. var responseName = _Object$keys2[_i7];
  255. var fields2 = fieldMap2[responseName];
  256. if (fields2) {
  257. var fields1 = fieldMap1[responseName];
  258. for (var i = 0; i < fields1.length; i++) {
  259. for (var j = 0; j < fields2.length; j++) {
  260. var conflict = findConflict(context, cachedFieldsAndFragmentNames, comparedFragmentPairs, parentFieldsAreMutuallyExclusive, responseName, fields1[i], fields2[j]);
  261. if (conflict) {
  262. conflicts.push(conflict);
  263. }
  264. }
  265. }
  266. }
  267. }
  268. } // Determines if there is a conflict between two particular fields, including
  269. // comparing their sub-fields.
  270. function findConflict(context, cachedFieldsAndFragmentNames, comparedFragmentPairs, parentFieldsAreMutuallyExclusive, responseName, field1, field2) {
  271. var parentType1 = field1[0],
  272. node1 = field1[1],
  273. def1 = field1[2];
  274. var parentType2 = field2[0],
  275. node2 = field2[1],
  276. def2 = field2[2]; // If it is known that two fields could not possibly apply at the same
  277. // time, due to the parent types, then it is safe to permit them to diverge
  278. // in aliased field or arguments used as they will not present any ambiguity
  279. // by differing.
  280. // It is known that two parent types could never overlap if they are
  281. // different Object types. Interface or Union types might overlap - if not
  282. // in the current state of the schema, then perhaps in some future version,
  283. // thus may not safely diverge.
  284. var areMutuallyExclusive = parentFieldsAreMutuallyExclusive || parentType1 !== parentType2 && (0, _definition.isObjectType)(parentType1) && (0, _definition.isObjectType)(parentType2);
  285. if (!areMutuallyExclusive) {
  286. var _node1$arguments, _node2$arguments;
  287. // Two aliases must refer to the same field.
  288. var name1 = node1.name.value;
  289. var name2 = node2.name.value;
  290. if (name1 !== name2) {
  291. return [[responseName, "\"".concat(name1, "\" and \"").concat(name2, "\" are different fields")], [node1], [node2]];
  292. } // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
  293. var args1 = (_node1$arguments = node1.arguments) !== null && _node1$arguments !== void 0 ? _node1$arguments : []; // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
  294. var args2 = (_node2$arguments = node2.arguments) !== null && _node2$arguments !== void 0 ? _node2$arguments : []; // Two field calls must have the same arguments.
  295. if (!sameArguments(args1, args2)) {
  296. return [[responseName, 'they have differing arguments'], [node1], [node2]];
  297. }
  298. } // The return type for each field.
  299. var type1 = def1 === null || def1 === void 0 ? void 0 : def1.type;
  300. var type2 = def2 === null || def2 === void 0 ? void 0 : def2.type;
  301. if (type1 && type2 && doTypesConflict(type1, type2)) {
  302. return [[responseName, "they return conflicting types \"".concat((0, _inspect.default)(type1), "\" and \"").concat((0, _inspect.default)(type2), "\"")], [node1], [node2]];
  303. } // Collect and compare sub-fields. Use the same "visited fragment names" list
  304. // for both collections so fields in a fragment reference are never
  305. // compared to themselves.
  306. var selectionSet1 = node1.selectionSet;
  307. var selectionSet2 = node2.selectionSet;
  308. if (selectionSet1 && selectionSet2) {
  309. var conflicts = findConflictsBetweenSubSelectionSets(context, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, (0, _definition.getNamedType)(type1), selectionSet1, (0, _definition.getNamedType)(type2), selectionSet2);
  310. return subfieldConflicts(conflicts, responseName, node1, node2);
  311. }
  312. }
  313. function sameArguments(arguments1, arguments2) {
  314. if (arguments1.length !== arguments2.length) {
  315. return false;
  316. }
  317. return arguments1.every(function (argument1) {
  318. var argument2 = (0, _find.default)(arguments2, function (argument) {
  319. return argument.name.value === argument1.name.value;
  320. });
  321. if (!argument2) {
  322. return false;
  323. }
  324. return sameValue(argument1.value, argument2.value);
  325. });
  326. }
  327. function sameValue(value1, value2) {
  328. return (0, _printer.print)(value1) === (0, _printer.print)(value2);
  329. } // Two types conflict if both types could not apply to a value simultaneously.
  330. // Composite types are ignored as their individual field types will be compared
  331. // later recursively. However List and Non-Null types must match.
  332. function doTypesConflict(type1, type2) {
  333. if ((0, _definition.isListType)(type1)) {
  334. return (0, _definition.isListType)(type2) ? doTypesConflict(type1.ofType, type2.ofType) : true;
  335. }
  336. if ((0, _definition.isListType)(type2)) {
  337. return true;
  338. }
  339. if ((0, _definition.isNonNullType)(type1)) {
  340. return (0, _definition.isNonNullType)(type2) ? doTypesConflict(type1.ofType, type2.ofType) : true;
  341. }
  342. if ((0, _definition.isNonNullType)(type2)) {
  343. return true;
  344. }
  345. if ((0, _definition.isLeafType)(type1) || (0, _definition.isLeafType)(type2)) {
  346. return type1 !== type2;
  347. }
  348. return false;
  349. } // Given a selection set, return the collection of fields (a mapping of response
  350. // name to field nodes and definitions) as well as a list of fragment names
  351. // referenced via fragment spreads.
  352. function getFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, parentType, selectionSet) {
  353. var cached = cachedFieldsAndFragmentNames.get(selectionSet);
  354. if (!cached) {
  355. var nodeAndDefs = Object.create(null);
  356. var fragmentNames = Object.create(null);
  357. _collectFieldsAndFragmentNames(context, parentType, selectionSet, nodeAndDefs, fragmentNames);
  358. cached = [nodeAndDefs, Object.keys(fragmentNames)];
  359. cachedFieldsAndFragmentNames.set(selectionSet, cached);
  360. }
  361. return cached;
  362. } // Given a reference to a fragment, return the represented collection of fields
  363. // as well as a list of nested fragment names referenced via fragment spreads.
  364. function getReferencedFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, fragment) {
  365. // Short-circuit building a type from the node if possible.
  366. var cached = cachedFieldsAndFragmentNames.get(fragment.selectionSet);
  367. if (cached) {
  368. return cached;
  369. }
  370. var fragmentType = (0, _typeFromAST.typeFromAST)(context.getSchema(), fragment.typeCondition);
  371. return getFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, fragmentType, fragment.selectionSet);
  372. }
  373. function _collectFieldsAndFragmentNames(context, parentType, selectionSet, nodeAndDefs, fragmentNames) {
  374. for (var _i9 = 0, _selectionSet$selecti2 = selectionSet.selections; _i9 < _selectionSet$selecti2.length; _i9++) {
  375. var selection = _selectionSet$selecti2[_i9];
  376. switch (selection.kind) {
  377. case _kinds.Kind.FIELD:
  378. {
  379. var fieldName = selection.name.value;
  380. var fieldDef = void 0;
  381. if ((0, _definition.isObjectType)(parentType) || (0, _definition.isInterfaceType)(parentType)) {
  382. fieldDef = parentType.getFields()[fieldName];
  383. }
  384. var responseName = selection.alias ? selection.alias.value : fieldName;
  385. if (!nodeAndDefs[responseName]) {
  386. nodeAndDefs[responseName] = [];
  387. }
  388. nodeAndDefs[responseName].push([parentType, selection, fieldDef]);
  389. break;
  390. }
  391. case _kinds.Kind.FRAGMENT_SPREAD:
  392. fragmentNames[selection.name.value] = true;
  393. break;
  394. case _kinds.Kind.INLINE_FRAGMENT:
  395. {
  396. var typeCondition = selection.typeCondition;
  397. var inlineFragmentType = typeCondition ? (0, _typeFromAST.typeFromAST)(context.getSchema(), typeCondition) : parentType;
  398. _collectFieldsAndFragmentNames(context, inlineFragmentType, selection.selectionSet, nodeAndDefs, fragmentNames);
  399. break;
  400. }
  401. }
  402. }
  403. } // Given a series of Conflicts which occurred between two sub-fields, generate
  404. // a single Conflict.
  405. function subfieldConflicts(conflicts, responseName, node1, node2) {
  406. if (conflicts.length > 0) {
  407. return [[responseName, conflicts.map(function (_ref6) {
  408. var reason = _ref6[0];
  409. return reason;
  410. })], conflicts.reduce(function (allFields, _ref7) {
  411. var fields1 = _ref7[1];
  412. return allFields.concat(fields1);
  413. }, [node1]), conflicts.reduce(function (allFields, _ref8) {
  414. var fields2 = _ref8[2];
  415. return allFields.concat(fields2);
  416. }, [node2])];
  417. }
  418. }
  419. /**
  420. * A way to keep track of pairs of things when the ordering of the pair does
  421. * not matter. We do this by maintaining a sort of double adjacency sets.
  422. */
  423. var PairSet = /*#__PURE__*/function () {
  424. function PairSet() {
  425. this._data = Object.create(null);
  426. }
  427. var _proto = PairSet.prototype;
  428. _proto.has = function has(a, b, areMutuallyExclusive) {
  429. var first = this._data[a];
  430. var result = first && first[b];
  431. if (result === undefined) {
  432. return false;
  433. } // areMutuallyExclusive being false is a superset of being true,
  434. // hence if we want to know if this PairSet "has" these two with no
  435. // exclusivity, we have to ensure it was added as such.
  436. if (areMutuallyExclusive === false) {
  437. return result === false;
  438. }
  439. return true;
  440. };
  441. _proto.add = function add(a, b, areMutuallyExclusive) {
  442. this._pairSetAdd(a, b, areMutuallyExclusive);
  443. this._pairSetAdd(b, a, areMutuallyExclusive);
  444. };
  445. _proto._pairSetAdd = function _pairSetAdd(a, b, areMutuallyExclusive) {
  446. var map = this._data[a];
  447. if (!map) {
  448. map = Object.create(null);
  449. this._data[a] = map;
  450. }
  451. map[b] = areMutuallyExclusive;
  452. };
  453. return PairSet;
  454. }();