OverlappingFieldsCanBeMerged.js 24 KB

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