graphql 15.10.3 → 15.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/error/GraphQLError.js +7 -6
  2. package/error/GraphQLError.js.flow +3 -3
  3. package/error/GraphQLError.mjs +8 -7
  4. package/execution/execute.d.ts +2 -0
  5. package/execution/execute.js +33 -12
  6. package/execution/execute.js.flow +32 -11
  7. package/execution/execute.mjs +33 -12
  8. package/execution/values.d.ts +1 -1
  9. package/execution/values.js +5 -5
  10. package/execution/values.js.flow +5 -2
  11. package/execution/values.mjs +4 -4
  12. package/graphql.d.ts +1 -0
  13. package/graphql.js +4 -1
  14. package/graphql.js.flow +5 -1
  15. package/graphql.mjs +4 -1
  16. package/jsutils/SetMap.js +56 -0
  17. package/jsutils/SetMap.js.flow +39 -0
  18. package/jsutils/SetMap.mjs +47 -0
  19. package/language/lexer.js +21 -2
  20. package/language/lexer.js.flow +24 -2
  21. package/language/lexer.mjs +21 -2
  22. package/language/location.d.ts +5 -0
  23. package/language/location.js +64 -0
  24. package/language/location.js.flow +39 -0
  25. package/language/location.mjs +62 -0
  26. package/language/parser.js +34 -19
  27. package/language/parser.js.flow +54 -10
  28. package/language/parser.mjs +34 -19
  29. package/language/printer.d.ts +7 -0
  30. package/language/printer.js +251 -118
  31. package/language/printer.js.flow +345 -141
  32. package/language/printer.mjs +246 -116
  33. package/language/reducer.js +99 -0
  34. package/language/reducer.js.flow +89 -0
  35. package/language/reducer.mjs +89 -0
  36. package/package.json +1 -1
  37. package/subscription/subscribe.d.ts +2 -0
  38. package/subscription/subscribe.js +4 -3
  39. package/subscription/subscribe.js.flow +6 -0
  40. package/subscription/subscribe.mjs +4 -3
  41. package/type/definition.d.ts +2 -1
  42. package/type/definition.js +8 -8
  43. package/type/definition.js.flow +13 -7
  44. package/type/definition.mjs +7 -7
  45. package/type/scalars.js +5 -5
  46. package/type/scalars.js.flow +1 -1
  47. package/type/scalars.mjs +1 -1
  48. package/utilities/buildASTSchema.js +3 -0
  49. package/utilities/buildASTSchema.js.flow +3 -0
  50. package/utilities/buildASTSchema.mjs +3 -0
  51. package/utilities/coerceInputValue.d.ts +1 -0
  52. package/utilities/coerceInputValue.js +9 -8
  53. package/utilities/coerceInputValue.js.flow +42 -9
  54. package/utilities/coerceInputValue.mjs +10 -9
  55. package/utilities/findBreakingChanges.js +3 -23
  56. package/utilities/findBreakingChanges.js.flow +2 -15
  57. package/utilities/findBreakingChanges.mjs +2 -21
  58. package/utilities/sortValueNode.js +52 -0
  59. package/utilities/sortValueNode.js.flow +42 -0
  60. package/utilities/sortValueNode.mjs +42 -0
  61. package/utilities/valueFromAST.js +2 -2
  62. package/utilities/valueFromAST.js.flow +8 -2
  63. package/utilities/valueFromAST.mjs +2 -2
  64. package/validation/ValidationContext.d.ts +8 -0
  65. package/validation/ValidationContext.js +82 -24
  66. package/validation/ValidationContext.js.flow +70 -21
  67. package/validation/ValidationContext.mjs +80 -24
  68. package/validation/rules/FieldsOnCorrectTypeRule.js +2 -2
  69. package/validation/rules/FieldsOnCorrectTypeRule.js.flow +8 -2
  70. package/validation/rules/FieldsOnCorrectTypeRule.mjs +2 -2
  71. package/validation/rules/KnownArgumentNamesRule.js +8 -9
  72. package/validation/rules/KnownArgumentNamesRule.js.flow +16 -6
  73. package/validation/rules/KnownArgumentNamesRule.mjs +8 -9
  74. package/validation/rules/KnownTypeNamesRule.js +1 -1
  75. package/validation/rules/KnownTypeNamesRule.js.flow +6 -4
  76. package/validation/rules/KnownTypeNamesRule.mjs +1 -1
  77. package/validation/rules/MaxIntrospectionDepthRule.js +37 -17
  78. package/validation/rules/MaxIntrospectionDepthRule.js.flow +46 -15
  79. package/validation/rules/MaxIntrospectionDepthRule.mjs +37 -17
  80. package/validation/rules/NoUnusedFragmentsRule.js +6 -13
  81. package/validation/rules/NoUnusedFragmentsRule.js.flow +6 -10
  82. package/validation/rules/NoUnusedFragmentsRule.mjs +6 -13
  83. package/validation/rules/OverlappingFieldsCanBeMergedRule.js +402 -208
  84. package/validation/rules/OverlappingFieldsCanBeMergedRule.js.flow +566 -330
  85. package/validation/rules/OverlappingFieldsCanBeMergedRule.mjs +400 -206
  86. package/validation/rules/ValuesOfCorrectTypeRule.js +6 -6
  87. package/validation/rules/ValuesOfCorrectTypeRule.js.flow +11 -6
  88. package/validation/rules/ValuesOfCorrectTypeRule.mjs +4 -4
  89. package/validation/validate.d.ts +1 -1
  90. package/validation/validate.js +6 -2
  91. package/validation/validate.js.flow +8 -2
  92. package/validation/validate.mjs +5 -2
  93. package/validation/validationAbortedError.d.ts +4 -0
  94. package/validation/validationAbortedError.js +12 -0
  95. package/validation/validationAbortedError.js.flow +7 -0
  96. package/validation/validationAbortedError.mjs +4 -0
  97. package/version.js +3 -3
  98. package/version.js.flow +3 -3
  99. package/version.mjs +3 -3
@@ -5,35 +5,30 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.OverlappingFieldsCanBeMergedRule = OverlappingFieldsCanBeMergedRule;
7
7
 
8
- var _find = _interopRequireDefault(require("../../polyfills/find.js"));
9
-
10
- var _objectEntries3 = _interopRequireDefault(require("../../polyfills/objectEntries.js"));
11
-
12
8
  var _inspect = _interopRequireDefault(require("../../jsutils/inspect.js"));
13
9
 
14
10
  var _GraphQLError = require("../../error/GraphQLError.js");
15
11
 
16
- var _kinds = require("../../language/kinds.js");
12
+ var _ast = require("../../language/ast.js");
17
13
 
18
- var _printer = require("../../language/printer.js");
14
+ var _kinds = require("../../language/kinds.js");
19
15
 
20
16
  var _definition = require("../../type/definition.js");
21
17
 
22
18
  var _typeFromAST = require("../../utilities/typeFromAST.js");
23
19
 
20
+ var _validationAbortedError = require("../validationAbortedError.js");
21
+
24
22
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
25
23
 
26
- function reasonMessage(reason) {
27
- if (Array.isArray(reason)) {
28
- return reason.map(function (_ref) {
29
- var responseName = _ref[0],
30
- subReason = _ref[1];
31
- return "subfields \"".concat(responseName, "\" conflict because ") + reasonMessage(subReason);
32
- }).join(' and ');
33
- }
24
+ // Keep diagnostic detail bounded independently of document size.
25
+ var MAX_CONFLICT_DETAIL_LENGTH = 1000;
26
+ var MAX_CONFLICT_NODES = 20; // v15 has no default validation error limit. Bound this rule's reporting locally.
34
27
 
35
- return reason;
36
- }
28
+ var MAX_REPORTED_CONFLICTS = 100; // Meter work that can grow through repeated comparisons.
29
+ // Collecting a selection set's fields is cached and is not charged.
30
+
31
+ var MAX_COMPLEXITY_BUDGET = 500000;
37
32
  /**
38
33
  * Overlapping fields can be merged
39
34
  *
@@ -42,35 +37,56 @@ function reasonMessage(reason) {
42
37
  * without ambiguity.
43
38
  */
44
39
 
40
+ function OverlappingFieldsCanBeMergedRule(validationContext) {
41
+ var ruleContext = {
42
+ // A memoization for when two fragments are compared for conflicts.
43
+ // Comparisons may be made many times, so memoizing this can dramatically
44
+ // improve the performance of this validator.
45
+ comparedFragmentPairs: new PairSet(),
46
+ // A cache for the "field map" and list of fragment names found in any given
47
+ // selection set.
48
+ cachedCollectFieldsResults: new Map(),
49
+ stringifiedValues: new Map(),
50
+ remainingComplexityBudget: MAX_COMPLEXITY_BUDGET
51
+ };
52
+ var reportedConflicts = 0;
53
+ return {
54
+ SelectionSet: function SelectionSet(selectionSet, _key, parent) {
55
+ // Inline fragments are flattened into their enclosing response level.
56
+ // Their fields' own selection sets are still visited independently.
57
+ if (parent && 'kind' in parent && parent.kind === _kinds.Kind.INLINE_FRAGMENT) {
58
+ return;
59
+ }
45
60
 
46
- function OverlappingFieldsCanBeMergedRule(context) {
47
- // A memoization for when two fragments are compared "between" each other for
48
- // conflicts. Two fragments may be compared many times, so memoizing this can
49
- // dramatically improve the performance of this validator.
50
- var comparedFragmentPairs = new PairSet(); // A cache for the "field map" and list of fragment names found in any given
51
- // selection set. Selection sets may be asked for this information multiple
52
- // times, so this improves the performance of this validator.
61
+ if (selectionSet.selections.length === 0 || selectionSet.selections.length === 1 && selectionSet.selections[0].kind !== _kinds.Kind.INLINE_FRAGMENT) {
62
+ return;
63
+ }
53
64
 
54
- var cachedFieldsAndFragmentNames = new Map();
55
- return {
56
- SelectionSet: function SelectionSet(selectionSet) {
57
- var conflicts = findConflictsWithinSelectionSet(context, cachedFieldsAndFragmentNames, comparedFragmentPairs, context.getParentType(), selectionSet);
65
+ var conflicts = findConflictsWithinSelectionSet(validationContext, ruleContext, validationContext.getParentType(), selectionSet);
58
66
 
59
67
  for (var _i2 = 0; _i2 < conflicts.length; _i2++) {
60
- var _ref3 = conflicts[_i2];
61
- var _ref2$ = _ref3[0];
62
- var responseName = _ref2$[0];
63
- var reason = _ref2$[1];
64
- var fields1 = _ref3[1];
65
- var fields2 = _ref3[2];
66
- var reasonMsg = reasonMessage(reason);
67
- 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)));
68
+ var _ref2 = conflicts[_i2];
69
+ var responseName = _ref2.responseName;
70
+ var reason = _ref2.reason;
71
+ var fields1 = _ref2.fields1;
72
+ var fields2 = _ref2.fields2;
73
+
74
+ if (reportedConflicts >= MAX_REPORTED_CONFLICTS) {
75
+ throw _validationAbortedError.validationAbortedError;
76
+ }
77
+
78
+ reportedConflicts++;
79
+ validationContext.reportError(new _GraphQLError.GraphQLError("Fields \"".concat(maybeAbbreviate(responseName), "\" conflict because ").concat(reason, ". Use different aliases on the fields to fetch both if this was intentional."), fields1.concat(fields2)));
68
80
  }
69
81
  }
70
82
  };
71
83
  }
72
84
 
73
85
  /**
86
+ * Find all conflicts found "within" a selection set, including those found
87
+ * via spreading in fragments. Called when visiting each SelectionSet in the
88
+ * GraphQL Document.
89
+ *
74
90
  * Algorithm:
75
91
  *
76
92
  * Conflicts occur when two fields exist in a query which will produce the same
@@ -86,7 +102,7 @@ function OverlappingFieldsCanBeMergedRule(context) {
86
102
  * A) Each selection set represented in the document first compares "within" its
87
103
  * collected set of fields, finding any conflicts between every pair of
88
104
  * overlapping fields.
89
- * Note: This is the *only time* that a the fields "within" a set are compared
105
+ * Note: This is the *only time* that the fields "within" a set are compared
90
106
  * to each other. After this only fields "between" sets are compared.
91
107
  *
92
108
  * B) Also, if any fragment is referenced in a selection set, then a
@@ -98,14 +114,14 @@ function OverlappingFieldsCanBeMergedRule(context) {
98
114
  *
99
115
  * D) When comparing "between" a set of fields and a referenced fragment, first
100
116
  * a comparison is made between each field in the original set of fields and
101
- * each field in the the referenced set of fields.
117
+ * each field in the referenced set of fields.
102
118
  *
103
119
  * E) Also, if any fragment is referenced in the referenced selection set,
104
120
  * then a comparison is made "between" the original set of fields and the
105
121
  * referenced fragment (recursively referring to step D).
106
122
  *
107
123
  * F) When comparing "between" two fragments, first a comparison is made between
108
- * each field in the first referenced set of fields and each field in the the
124
+ * each field in the first referenced set of fields and each field in the
109
125
  * second referenced set of fields.
110
126
  *
111
127
  * G) Also, any fragments referenced by the first must be compared to the
@@ -122,33 +138,25 @@ function OverlappingFieldsCanBeMergedRule(context) {
122
138
  *
123
139
  * J) Also, if two fragments are referenced in both selection sets, then a
124
140
  * comparison is made "between" the two fragments.
125
- *
126
141
  */
127
- // Find all conflicts found "within" a selection set, including those found
128
- // via spreading in fragments. Called when visiting each SelectionSet in the
129
- // GraphQL Document.
130
- function findConflictsWithinSelectionSet(context, cachedFieldsAndFragmentNames, comparedFragmentPairs, parentType, selectionSet) {
142
+ function findConflictsWithinSelectionSet(validationContext, ruleContext, parentType, selectionSet) {
131
143
  var conflicts = [];
132
-
133
- var _getFieldsAndFragment = getFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, parentType, selectionSet),
134
- fieldMap = _getFieldsAndFragment[0],
135
- fragmentNames = _getFieldsAndFragment[1]; // (A) Find find all conflicts "within" the fields of this selection set.
144
+ var collectFieldsResult = collectFields(validationContext, ruleContext, parentType, selectionSet); // (A) Find find all conflicts "within" the fields of this selection set.
136
145
  // Note: this is the *only place* `collectConflictsWithin` is called.
137
146
 
147
+ collectConflictsWithin(validationContext, conflicts, ruleContext, collectFieldsResult); // Compare fragment uses only with fields or another spread.
138
148
 
139
- collectConflictsWithin(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, fieldMap);
140
-
141
- if (fragmentNames.length !== 0) {
149
+ if (collectFieldsResult.fragmentNames.length !== 0 && (collectFieldsResult.responseNames.length !== 0 || collectFieldsResult.fragmentNames.length > 1)) {
142
150
  // (B) Then collect conflicts between these fields and those represented by
143
151
  // each spread fragment name found.
144
- for (var i = 0; i < fragmentNames.length; i++) {
145
- collectConflictsBetweenFieldsAndFragment(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, false, fieldMap, fragmentNames[i]); // (C) Then compare this fragment with all other fragments found in this
152
+ for (var i = 0; i < collectFieldsResult.fragmentNames.length; i++) {
153
+ collectConflictsBetweenFieldsAndFragment(validationContext, conflicts, ruleContext, false, collectFieldsResult, collectFieldsResult.fragmentNames[i]); // (C) Then compare this fragment with all other fragments found in this
146
154
  // selection set to collect conflicts between fragments spread together.
147
155
  // This compares each item in the list of fragment names to every other
148
156
  // item in that same list (except for itself).
149
157
 
150
- for (var j = i + 1; j < fragmentNames.length; j++) {
151
- collectConflictsBetweenFragments(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, false, fragmentNames[i], fragmentNames[j]);
158
+ for (var j = i + 1; j < collectFieldsResult.fragmentNames.length; j++) {
159
+ collectConflictsBetweenFragments(validationContext, conflicts, ruleContext, false, collectFieldsResult.fragmentNames[i], collectFieldsResult.fragmentNames[j]);
152
160
  }
153
161
  }
154
162
  }
@@ -158,43 +166,57 @@ function findConflictsWithinSelectionSet(context, cachedFieldsAndFragmentNames,
158
166
  // including via spreading in any nested fragments.
159
167
 
160
168
 
161
- function collectConflictsBetweenFieldsAndFragment(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fieldMap, fragmentName) {
162
- var fragment = context.getFragment(fragmentName);
169
+ function collectConflictsBetweenFieldsAndFragment(validationContext, conflicts, ruleContext, areMutuallyExclusive, collectFieldsResult, fragmentName) {
170
+ var _collectFieldsResult$;
163
171
 
164
- if (!fragment) {
172
+ // Callers can enumerate more field-map/fragment pairs than document nodes.
173
+ // Count attempts even when an empty collection or the pair cache skips them.
174
+ spendComplexityBudget(ruleContext, 1);
175
+
176
+ if (collectFieldsResult.responseNames.length === 0) {
165
177
  return;
166
178
  }
167
179
 
168
- var _getReferencedFieldsA = getReferencedFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, fragment),
169
- fieldMap2 = _getReferencedFieldsA[0],
170
- fragmentNames2 = _getReferencedFieldsA[1]; // Do not compare a fragment's fieldMap to itself.
180
+ var comparedFragments = (_collectFieldsResult$ = collectFieldsResult.comparedFragments) !== null && _collectFieldsResult$ !== void 0 ? _collectFieldsResult$ : collectFieldsResult.comparedFragments = new Map(); // Memoize so the fields and fragments are not compared for conflicts more
181
+ // than once.
171
182
 
183
+ var compared = comparedFragments.get(fragmentName); // Only a non-exclusive comparison can be reused for overlapping parents.
172
184
 
173
- if (fieldMap === fieldMap2) {
185
+ if (compared === false || areMutuallyExclusive && compared === true) {
186
+ return;
187
+ }
188
+
189
+ comparedFragments.set(fragmentName, areMutuallyExclusive);
190
+ var fragment = validationContext.getFragment(fragmentName);
191
+
192
+ if (!fragment) {
193
+ return;
194
+ }
195
+
196
+ var collectFieldsResult2 = collectFragmentFields(validationContext, ruleContext, fragment); // Do not compare a fragment's collectFieldsResult to itself.
197
+
198
+ if (collectFieldsResult === collectFieldsResult2) {
174
199
  return;
175
200
  } // (D) First collect any conflicts between the provided collection of fields
176
201
  // and the collection of fields represented by the given fragment.
177
202
 
178
203
 
179
- collectConflictsBetween(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fieldMap, fieldMap2); // (E) Then collect any conflicts between the provided collection of fields
204
+ collectConflictsBetween(validationContext, conflicts, ruleContext, areMutuallyExclusive, collectFieldsResult, collectFieldsResult2); // (E) Then collect any conflicts between the provided collection of fields
180
205
  // and any fragment names found in the given fragment.
181
206
 
182
- for (var i = 0; i < fragmentNames2.length; i++) {
183
- var referencedFragmentName = fragmentNames2[i]; // Memoize so two fragments are not compared for conflicts more than once.
184
-
185
- if (comparedFragmentPairs.has(referencedFragmentName, fragmentName, areMutuallyExclusive)) {
186
- continue;
187
- }
188
-
189
- comparedFragmentPairs.add(referencedFragmentName, fragmentName, areMutuallyExclusive);
190
- collectConflictsBetweenFieldsAndFragment(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fieldMap, fragmentNames2[i]);
207
+ for (var i = 0; i < collectFieldsResult2.fragmentNames.length; i++) {
208
+ var referencedFragmentName = collectFieldsResult2.fragmentNames[i];
209
+ collectConflictsBetweenFieldsAndFragment(validationContext, conflicts, ruleContext, areMutuallyExclusive, collectFieldsResult, referencedFragmentName);
191
210
  }
192
211
  } // Collect all conflicts found between two fragments, including via spreading in
193
212
  // any nested fragments.
194
213
 
195
214
 
196
- function collectConflictsBetweenFragments(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fragmentName1, fragmentName2) {
197
- // No need to compare a fragment to itself.
215
+ function collectConflictsBetweenFragments(validationContext, conflicts, ruleContext, areMutuallyExclusive, fragmentName1, fragmentName2) {
216
+ // Count attempted fragment pairs, including identity and cache hits.
217
+ spendComplexityBudget(ruleContext, 2);
218
+ var comparedFragmentPairs = ruleContext.comparedFragmentPairs; // No need to compare a fragment to itself.
219
+
198
220
  if (fragmentName1 === fragmentName2) {
199
221
  return;
200
222
  } // Memoize so two fragments are not compared for conflicts more than once.
@@ -205,75 +227,62 @@ function collectConflictsBetweenFragments(context, conflicts, cachedFieldsAndFra
205
227
  }
206
228
 
207
229
  comparedFragmentPairs.add(fragmentName1, fragmentName2, areMutuallyExclusive);
208
- var fragment1 = context.getFragment(fragmentName1);
209
- var fragment2 = context.getFragment(fragmentName2);
230
+ var fragment1 = validationContext.getFragment(fragmentName1);
231
+ var fragment2 = validationContext.getFragment(fragmentName2);
210
232
 
211
233
  if (!fragment1 || !fragment2) {
212
234
  return;
213
235
  }
214
236
 
215
- var _getReferencedFieldsA2 = getReferencedFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, fragment1),
216
- fieldMap1 = _getReferencedFieldsA2[0],
217
- fragmentNames1 = _getReferencedFieldsA2[1];
218
-
219
- var _getReferencedFieldsA3 = getReferencedFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, fragment2),
220
- fieldMap2 = _getReferencedFieldsA3[0],
221
- fragmentNames2 = _getReferencedFieldsA3[1]; // (F) First, collect all conflicts between these two collections of fields
237
+ var collectFieldsResult1 = collectFragmentFields(validationContext, ruleContext, fragment1);
238
+ var collectFieldsResult2 = collectFragmentFields(validationContext, ruleContext, fragment2); // (F) First, collect all conflicts between these two collections of fields
222
239
  // (not including any nested fragments).
223
240
 
224
-
225
- collectConflictsBetween(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fieldMap1, fieldMap2); // (G) Then collect conflicts between the first fragment and any nested
241
+ collectConflictsBetween(validationContext, conflicts, ruleContext, areMutuallyExclusive, collectFieldsResult1, collectFieldsResult2); // (G) Then collect conflicts between the first fragment and any nested
226
242
  // fragments spread in the second fragment.
227
243
 
228
- for (var j = 0; j < fragmentNames2.length; j++) {
229
- collectConflictsBetweenFragments(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fragmentName1, fragmentNames2[j]);
244
+ for (var j = 0; j < collectFieldsResult2.fragmentNames.length; j++) {
245
+ collectConflictsBetweenFragments(validationContext, conflicts, ruleContext, areMutuallyExclusive, fragmentName1, collectFieldsResult2.fragmentNames[j]);
230
246
  } // (G) Then collect conflicts between the second fragment and any nested
231
247
  // fragments spread in the first fragment.
232
248
 
233
249
 
234
- for (var i = 0; i < fragmentNames1.length; i++) {
235
- collectConflictsBetweenFragments(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fragmentNames1[i], fragmentName2);
250
+ for (var i = 0; i < collectFieldsResult1.fragmentNames.length; i++) {
251
+ collectConflictsBetweenFragments(validationContext, conflicts, ruleContext, areMutuallyExclusive, collectFieldsResult1.fragmentNames[i], fragmentName2);
236
252
  }
237
253
  } // Find all conflicts found between two selection sets, including those found
238
254
  // via spreading in fragments. Called when determining if conflicts exist
239
255
  // between the sub-fields of two overlapping fields.
240
256
 
241
257
 
242
- function findConflictsBetweenSubSelectionSets(context, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, parentType1, selectionSet1, parentType2, selectionSet2) {
258
+ function findConflictsBetweenSubSelectionSets(validationContext, ruleContext, areMutuallyExclusive, parentType1, selectionSet1, parentType2, selectionSet2) {
243
259
  var conflicts = [];
260
+ var collectFieldsResult1 = collectFields(validationContext, ruleContext, parentType1, selectionSet1);
261
+ var collectFieldsResult2 = collectFields(validationContext, ruleContext, parentType2, selectionSet2); // (H) First, collect all conflicts between these two collections of field.
244
262
 
245
- var _getFieldsAndFragment2 = getFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, parentType1, selectionSet1),
246
- fieldMap1 = _getFieldsAndFragment2[0],
247
- fragmentNames1 = _getFieldsAndFragment2[1];
248
-
249
- var _getFieldsAndFragment3 = getFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, parentType2, selectionSet2),
250
- fieldMap2 = _getFieldsAndFragment3[0],
251
- fragmentNames2 = _getFieldsAndFragment3[1]; // (H) First, collect all conflicts between these two collections of field.
252
-
253
-
254
- collectConflictsBetween(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fieldMap1, fieldMap2); // (I) Then collect conflicts between the first collection of fields and
263
+ collectConflictsBetween(validationContext, conflicts, ruleContext, areMutuallyExclusive, collectFieldsResult1, collectFieldsResult2); // (I) Then collect conflicts between the first collection of fields and
255
264
  // those referenced by each fragment name associated with the second.
256
265
 
257
- if (fragmentNames2.length !== 0) {
258
- for (var j = 0; j < fragmentNames2.length; j++) {
259
- collectConflictsBetweenFieldsAndFragment(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fieldMap1, fragmentNames2[j]);
266
+ if (collectFieldsResult2.fragmentNames.length !== 0) {
267
+ for (var j = 0; j < collectFieldsResult2.fragmentNames.length; j++) {
268
+ collectConflictsBetweenFieldsAndFragment(validationContext, conflicts, ruleContext, areMutuallyExclusive, collectFieldsResult1, collectFieldsResult2.fragmentNames[j]);
260
269
  }
261
270
  } // (I) Then collect conflicts between the second collection of fields and
262
271
  // those referenced by each fragment name associated with the first.
263
272
 
264
273
 
265
- if (fragmentNames1.length !== 0) {
266
- for (var i = 0; i < fragmentNames1.length; i++) {
267
- collectConflictsBetweenFieldsAndFragment(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fieldMap2, fragmentNames1[i]);
274
+ if (collectFieldsResult1.fragmentNames.length !== 0) {
275
+ for (var i = 0; i < collectFieldsResult1.fragmentNames.length; i++) {
276
+ collectConflictsBetweenFieldsAndFragment(validationContext, conflicts, ruleContext, areMutuallyExclusive, collectFieldsResult2, collectFieldsResult1.fragmentNames[i]);
268
277
  }
269
278
  } // (J) Also collect conflicts between any fragment names by the first and
270
279
  // fragment names by the second. This compares each item in the first set of
271
280
  // names to each item in the second set of names.
272
281
 
273
282
 
274
- for (var _i3 = 0; _i3 < fragmentNames1.length; _i3++) {
275
- for (var _j = 0; _j < fragmentNames2.length; _j++) {
276
- collectConflictsBetweenFragments(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fragmentNames1[_i3], fragmentNames2[_j]);
283
+ for (var _i3 = 0; _i3 < collectFieldsResult1.fragmentNames.length; _i3++) {
284
+ for (var _j = 0; _j < collectFieldsResult2.fragmentNames.length; _j++) {
285
+ collectConflictsBetweenFragments(validationContext, conflicts, ruleContext, areMutuallyExclusive, collectFieldsResult1.fragmentNames[_i3], collectFieldsResult2.fragmentNames[_j]);
277
286
  }
278
287
  }
279
288
 
@@ -281,24 +290,21 @@ function findConflictsBetweenSubSelectionSets(context, cachedFieldsAndFragmentNa
281
290
  } // Collect all Conflicts "within" one collection of fields.
282
291
 
283
292
 
284
- function collectConflictsWithin(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, fieldMap) {
293
+ function collectConflictsWithin(validationContext, conflicts, ruleContext, collectFieldsResult) {
285
294
  // A field map is a keyed collection, where each key represents a response
286
295
  // name and the value at that key is a list of all fields which provide that
287
296
  // response name. For every response name, if there are multiple fields, they
288
297
  // must be compared to find a potential conflict.
289
- for (var _i5 = 0, _objectEntries2 = (0, _objectEntries3.default)(fieldMap); _i5 < _objectEntries2.length; _i5++) {
290
- var _ref5 = _objectEntries2[_i5];
291
- var responseName = _ref5[0];
292
- var fields = _ref5[1];
293
-
294
- // This compares every field in the list to every other field in this list
298
+ for (var _i5 = 0, _collectFieldsResult$3 = collectFieldsResult.responseNames; _i5 < _collectFieldsResult$3.length; _i5++) {
299
+ var responseName = _collectFieldsResult$3[_i5];
300
+ var fields = collectFieldsResult.groupedFieldSet[responseName]; // This compares every field in the list to every other field in this list
295
301
  // (except to itself). If the list only has one item, nothing needs to
296
302
  // be compared.
303
+
297
304
  if (fields.length > 1) {
298
305
  for (var i = 0; i < fields.length; i++) {
299
306
  for (var j = i + 1; j < fields.length; j++) {
300
- var conflict = findConflict(context, cachedFieldsAndFragmentNames, comparedFragmentPairs, false, // within one collection is never mutually exclusive
301
- responseName, fields[i], fields[j]);
307
+ var conflict = findConflict(validationContext, ruleContext, false, responseName, fields[i], fields[j]);
302
308
 
303
309
  if (conflict) {
304
310
  conflicts.push(conflict);
@@ -314,41 +320,76 @@ function collectConflictsWithin(context, conflicts, cachedFieldsAndFragmentNames
314
320
  // each individual selection set.
315
321
 
316
322
 
317
- function collectConflictsBetween(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, parentFieldsAreMutuallyExclusive, fieldMap1, fieldMap2) {
318
- // A field map is a keyed collection, where each key represents a response
323
+ function collectConflictsBetween(validationContext, conflicts, ruleContext, parentFieldsAreMutuallyExclusive, collectFieldsResult1, collectFieldsResult2) {
324
+ if (collectFieldsResult1.responseNames.length === 0 || collectFieldsResult2.responseNames.length === 0) {
325
+ return;
326
+ } // A field map is a keyed collection, where each key represents a response
319
327
  // name and the value at that key is a list of all fields which provide that
320
328
  // response name. For any response name which appears in both provided field
321
329
  // maps, each field from the first field map must be compared to every field
322
330
  // in the second field map to find potential conflicts.
323
- for (var _i7 = 0, _Object$keys2 = Object.keys(fieldMap1); _i7 < _Object$keys2.length; _i7++) {
324
- var responseName = _Object$keys2[_i7];
325
- var fields2 = fieldMap2[responseName];
326
331
 
327
- if (fields2) {
328
- var fields1 = fieldMap1[responseName];
329
332
 
330
- for (var i = 0; i < fields1.length; i++) {
331
- for (var j = 0; j < fields2.length; j++) {
332
- var conflict = findConflict(context, cachedFieldsAndFragmentNames, comparedFragmentPairs, parentFieldsAreMutuallyExclusive, responseName, fields1[i], fields2[j]);
333
+ for (var _i7 = 0, _getOverlappingFields2 = getOverlappingFields(ruleContext, collectFieldsResult1, collectFieldsResult2); _i7 < _getOverlappingFields2.length; _i7++) {
334
+ var _ref4 = _getOverlappingFields2[_i7];
335
+ var responseName = _ref4.responseName;
336
+ var fields1 = _ref4.fields1;
337
+ var fields2 = _ref4.fields2;
333
338
 
334
- if (conflict) {
335
- conflicts.push(conflict);
336
- }
339
+ for (var i = 0; i < fields1.length; i++) {
340
+ for (var j = 0; j < fields2.length; j++) {
341
+ var conflict = findConflict(validationContext, ruleContext, parentFieldsAreMutuallyExclusive, responseName, fields1[i], fields2[j]);
342
+
343
+ if (conflict) {
344
+ conflicts.push(conflict);
337
345
  }
338
346
  }
339
347
  }
340
348
  }
349
+ }
350
+
351
+ function getOverlappingFields(ruleContext, collectFieldsResult1, collectFieldsResult2) {
352
+ var _collectFieldsResult;
353
+
354
+ // Reuse matching response groups when these collections are compared again.
355
+ var byCollectFieldsResult = (_collectFieldsResult = collectFieldsResult1.overlappingFields) !== null && _collectFieldsResult !== void 0 ? _collectFieldsResult : collectFieldsResult1.overlappingFields = new Map();
356
+ var cached = byCollectFieldsResult.get(collectFieldsResult2);
357
+
358
+ if (cached !== undefined) {
359
+ return cached;
360
+ }
361
+
362
+ var overlappingFields = [];
363
+
364
+ for (var _i9 = 0, _collectFieldsResult3 = collectFieldsResult1.responseNames; _i9 < _collectFieldsResult3.length; _i9++) {
365
+ var responseName = _collectFieldsResult3[_i9];
366
+ // A field map can participate in many distinct comparisons.
367
+ spendComplexityBudget(ruleContext, 1);
368
+ var fields2 = collectFieldsResult2.groupedFieldSet[responseName];
369
+
370
+ if (fields2 !== undefined) {
371
+ var fields1 = collectFieldsResult1.groupedFieldSet[responseName];
372
+ overlappingFields.push({
373
+ responseName: responseName,
374
+ fields1: fields1,
375
+ fields2: fields2
376
+ });
377
+ }
378
+ }
379
+
380
+ byCollectFieldsResult.set(collectFieldsResult2, overlappingFields);
381
+ return overlappingFields;
341
382
  } // Determines if there is a conflict between two particular fields, including
342
383
  // comparing their sub-fields.
343
384
 
344
385
 
345
- function findConflict(context, cachedFieldsAndFragmentNames, comparedFragmentPairs, parentFieldsAreMutuallyExclusive, responseName, field1, field2) {
346
- var parentType1 = field1[0],
347
- node1 = field1[1],
348
- def1 = field1[2];
349
- var parentType2 = field2[0],
350
- node2 = field2[1],
351
- def2 = field2[2]; // If it is known that two fields could not possibly apply at the same
386
+ function findConflict(validationContext, ruleContext, parentFieldsAreMutuallyExclusive, responseName, fieldDetails1, fieldDetails2) {
387
+ var parentType1 = fieldDetails1.parentType,
388
+ node1 = fieldDetails1.node,
389
+ def1 = fieldDetails1.definition;
390
+ var parentType2 = fieldDetails2.parentType,
391
+ node2 = fieldDetails2.node,
392
+ def2 = fieldDetails2.definition; // If it is known that two fields could not possibly apply at the same
352
393
  // time, due to the parent types, then it is safe to permit them to diverge
353
394
  // in aliased field or arguments used as they will not present any ambiguity
354
395
  // by differing.
@@ -357,26 +398,34 @@ function findConflict(context, cachedFieldsAndFragmentNames, comparedFragmentPai
357
398
  // in the current state of the schema, then perhaps in some future version,
358
399
  // thus may not safely diverge.
359
400
 
360
- var areMutuallyExclusive = parentFieldsAreMutuallyExclusive || parentType1 !== parentType2 && (0, _definition.isObjectType)(parentType1) && (0, _definition.isObjectType)(parentType2);
361
-
362
- if (!areMutuallyExclusive) {
363
- var _node1$arguments, _node2$arguments;
364
-
365
- // Two aliases must refer to the same field.
366
- var name1 = node1.name.value;
367
- var name2 = node2.name.value;
401
+ var areMutuallyExclusive = parentFieldsAreMutuallyExclusive || parentType1 !== parentType2 && (0, _definition.isObjectType)(parentType1) && (0, _definition.isObjectType)(parentType2); // Count attempted field pairs, including pairs skipped by type exclusivity.
368
402
 
369
- if (name1 !== name2) {
370
- return [[responseName, "\"".concat(name1, "\" and \"").concat(name2, "\" are different fields")], [node1], [node2]];
371
- } // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
403
+ spendComplexityBudget(ruleContext, 2);
372
404
 
405
+ if (fieldDetails1.isSimpleLeaf && fieldDetails2.isSimpleLeaf && (def1 === null || def1 === void 0 ? void 0 : def1.type) === (def2 === null || def2 === void 0 ? void 0 : def2.type) && node1.name.value === node2.name.value) {
406
+ return;
407
+ }
373
408
 
374
- 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')
375
-
376
- var args2 = (_node2$arguments = node2.arguments) !== null && _node2$arguments !== void 0 ? _node2$arguments : []; // Two field calls must have the same arguments.
409
+ if (!areMutuallyExclusive) {
410
+ var fieldInfo1 = getFieldInfo(fieldDetails1);
411
+ var fieldInfo2 = getFieldInfo(fieldDetails2);
412
+ var reason = fieldInfo1.comparisons.get(fieldInfo2);
413
+
414
+ if (reason === undefined) {
415
+ // Two aliases must refer to the same field with the same arguments.
416
+ var name1 = node1.name.value;
417
+ var name2 = node2.name.value;
418
+ reason = name1 !== name2 ? "\"".concat(maybeAbbreviate(name1), "\" and \"").concat(maybeAbbreviate(name2), "\" are different fields") : sameArguments(ruleContext, fieldInfo1.arguments, fieldInfo2.arguments) ? null : 'they have differing arguments';
419
+ fieldInfo1.comparisons.set(fieldInfo2, reason);
420
+ }
377
421
 
378
- if (!sameArguments(args1, args2)) {
379
- return [[responseName, 'they have differing arguments'], [node1], [node2]];
422
+ if (reason !== null) {
423
+ return {
424
+ responseName: responseName,
425
+ reason: reason,
426
+ fields1: [node1],
427
+ fields2: [node2]
428
+ };
380
429
  }
381
430
  } // The return type for each field.
382
431
 
@@ -384,8 +433,21 @@ function findConflict(context, cachedFieldsAndFragmentNames, comparedFragmentPai
384
433
  var type1 = def1 === null || def1 === void 0 ? void 0 : def1.type;
385
434
  var type2 = def2 === null || def2 === void 0 ? void 0 : def2.type;
386
435
 
387
- if (type1 && type2 && doTypesConflict(type1, type2)) {
388
- return [[responseName, "they return conflicting types \"".concat((0, _inspect.default)(type1), "\" and \"").concat((0, _inspect.default)(type2), "\"")], [node1], [node2]];
436
+ if (type1 && type2 && doTypesConflict(ruleContext, type1, type2)) {
437
+ var _fieldInfo$typeString, _fieldInfo2$typeStrin;
438
+
439
+ var _fieldInfo = getFieldInfo(fieldDetails1);
440
+
441
+ var _fieldInfo2 = getFieldInfo(fieldDetails2);
442
+
443
+ var type1String = (_fieldInfo$typeString = _fieldInfo.typeString) !== null && _fieldInfo$typeString !== void 0 ? _fieldInfo$typeString : _fieldInfo.typeString = (0, _inspect.default)(type1);
444
+ var type2String = (_fieldInfo2$typeStrin = _fieldInfo2.typeString) !== null && _fieldInfo2$typeStrin !== void 0 ? _fieldInfo2$typeStrin : _fieldInfo2.typeString = (0, _inspect.default)(type2);
445
+ return {
446
+ responseName: responseName,
447
+ reason: "they return conflicting types \"".concat(type1String, "\" and \"").concat(type2String, "\""),
448
+ fields1: [node1],
449
+ fields2: [node2]
450
+ };
389
451
  } // Collect and compare sub-fields. Use the same "visited fragment names" list
390
452
  // for both collections so fields in a fragment reference are never
391
453
  // compared to themselves.
@@ -395,39 +457,119 @@ function findConflict(context, cachedFieldsAndFragmentNames, comparedFragmentPai
395
457
  var selectionSet2 = node2.selectionSet;
396
458
 
397
459
  if (selectionSet1 && selectionSet2) {
398
- var conflicts = findConflictsBetweenSubSelectionSets(context, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, (0, _definition.getNamedType)(type1), selectionSet1, (0, _definition.getNamedType)(type2), selectionSet2);
460
+ var conflicts = findConflictsBetweenSubSelectionSets(validationContext, ruleContext, areMutuallyExclusive, getFieldInfo(fieldDetails1).namedType, selectionSet1, getFieldInfo(fieldDetails2).namedType, selectionSet2);
399
461
  return subfieldConflicts(conflicts, responseName, node1, node2);
400
462
  }
463
+ } // Prepare argument lookups only when a field overlaps.
464
+
465
+
466
+ function getFieldInfo(fieldDetails) {
467
+ if (fieldDetails.info === undefined) {
468
+ var _fieldDetails$definit;
469
+
470
+ fieldDetails.info = {
471
+ comparisons: new Map(),
472
+ namedType: (0, _definition.getNamedType)((_fieldDetails$definit = fieldDetails.definition) === null || _fieldDetails$definit === void 0 ? void 0 : _fieldDetails$definit.type),
473
+ arguments: getArgumentInfo(fieldDetails.node.arguments)
474
+ };
475
+ }
476
+
477
+ return fieldDetails.info;
401
478
  }
402
479
 
403
- function sameArguments(arguments1, arguments2) {
404
- if (arguments1.length !== arguments2.length) {
480
+ function getArgumentInfo(args) {
481
+ if (args === undefined || args.length === 0) {
482
+ return;
483
+ }
484
+
485
+ var values = new Map();
486
+
487
+ for (var _i11 = 0; _i11 < args.length; _i11++) {
488
+ var _ref6 = args[_i11];
489
+ var name = _ref6.name;
490
+ var value = _ref6.value;
491
+ values.set(name.value, value);
492
+ }
493
+
494
+ return {
495
+ nodes: args,
496
+ values: values
497
+ };
498
+ }
499
+
500
+ function sameArguments(ruleContext, argumentInfo1, argumentInfo2) {
501
+ if (argumentInfo1 === undefined) {
502
+ return argumentInfo2 === undefined;
503
+ }
504
+
505
+ if (argumentInfo2 === undefined || argumentInfo1.nodes.length !== argumentInfo2.nodes.length) {
405
506
  return false;
406
507
  }
407
508
 
408
- return arguments1.every(function (argument1) {
409
- var argument2 = (0, _find.default)(arguments2, function (argument) {
410
- return argument.name.value === argument1.name.value;
411
- });
509
+ var values2 = argumentInfo2.values;
510
+ return argumentInfo1.nodes.every(function (arg1) {
511
+ // A prepared argument list can participate in many field-pair comparisons.
512
+ spendComplexityBudget(ruleContext, 1);
513
+ var value2 = values2.get(arg1.name.value);
412
514
 
413
- if (!argument2) {
515
+ if (value2 === undefined) {
414
516
  return false;
415
517
  }
416
518
 
417
- return sameValue(argument1.value, argument2.value);
519
+ return stringifyValue(ruleContext, arg1.value) === stringifyValue(ruleContext, value2);
418
520
  });
419
521
  }
420
522
 
421
- function sameValue(value1, value2) {
422
- return (0, _printer.print)(value1) === (0, _printer.print)(value2);
523
+ function stringifyValue(ruleContext, value) {
524
+ var cached = ruleContext.stringifiedValues.get(value);
525
+
526
+ if (cached !== undefined) {
527
+ return cached;
528
+ }
529
+
530
+ var result = JSON.stringify(value, function (_key, node) {
531
+ if (!(0, _ast.isNode)(node)) {
532
+ return node;
533
+ }
534
+
535
+ switch (node.kind) {
536
+ case _kinds.Kind.VARIABLE:
537
+ return [node.kind, node.name.value];
538
+
539
+ case _kinds.Kind.OBJECT:
540
+ return [node.kind, node.fields.slice().sort(function (first, second) {
541
+ return first.name.value < second.name.value ? -1 : first.name.value > second.name.value ? 1 : 0;
542
+ })];
543
+
544
+ case _kinds.Kind.OBJECT_FIELD:
545
+ return [node.name.value, node.value];
546
+
547
+ case _kinds.Kind.LIST:
548
+ return [node.kind, node.values];
549
+
550
+ case _kinds.Kind.STRING:
551
+ return [node.kind, node.value, node.block === true];
552
+
553
+ default:
554
+ return [node.kind, 'value' in node ? node.value : null];
555
+ }
556
+ });
557
+ ruleContext.stringifiedValues.set(value, result);
558
+ return result;
423
559
  } // Two types conflict if both types could not apply to a value simultaneously.
424
560
  // Composite types are ignored as their individual field types will be compared
425
561
  // later recursively. However List and Non-Null types must match.
426
562
 
427
563
 
428
- function doTypesConflict(type1, type2) {
564
+ function doTypesConflict(ruleContext, type1, type2) {
565
+ if (type1 === type2) {
566
+ return false;
567
+ }
568
+
569
+ spendComplexityBudget(ruleContext, 1);
570
+
429
571
  if ((0, _definition.isListType)(type1)) {
430
- return (0, _definition.isListType)(type2) ? doTypesConflict(type1.ofType, type2.ofType) : true;
572
+ return (0, _definition.isListType)(type2) ? doTypesConflict(ruleContext, type1.ofType, type2.ofType) : true;
431
573
  }
432
574
 
433
575
  if ((0, _definition.isListType)(type2)) {
@@ -435,7 +577,7 @@ function doTypesConflict(type1, type2) {
435
577
  }
436
578
 
437
579
  if ((0, _definition.isNonNullType)(type1)) {
438
- return (0, _definition.isNonNullType)(type2) ? doTypesConflict(type1.ofType, type2.ofType) : true;
580
+ return (0, _definition.isNonNullType)(type2) ? doTypesConflict(ruleContext, type1.ofType, type2.ofType) : true;
439
581
  }
440
582
 
441
583
  if ((0, _definition.isNonNullType)(type2)) {
@@ -452,43 +594,51 @@ function doTypesConflict(type1, type2) {
452
594
  // referenced via fragment spreads.
453
595
 
454
596
 
455
- function getFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, parentType, selectionSet) {
456
- var cached = cachedFieldsAndFragmentNames.get(selectionSet);
597
+ function collectFields(validationContext, ruleContext, parentType, selectionSet) {
598
+ var cachedCollectFieldsResults = ruleContext.cachedCollectFieldsResults; // A programmatically built document can share a selection-set object between
599
+ // different parent types; their field definitions must not share a cache entry.
457
600
 
458
- if (!cached) {
459
- var nodeAndDefs = Object.create(null);
460
- var fragmentNames = Object.create(null);
601
+ var bySelectionSet = cachedCollectFieldsResults.get(parentType);
461
602
 
462
- _collectFieldsAndFragmentNames(context, parentType, selectionSet, nodeAndDefs, fragmentNames);
463
-
464
- cached = [nodeAndDefs, Object.keys(fragmentNames)];
465
- cachedFieldsAndFragmentNames.set(selectionSet, cached);
603
+ if (bySelectionSet === undefined) {
604
+ bySelectionSet = new Map();
605
+ cachedCollectFieldsResults.set(parentType, bySelectionSet);
466
606
  }
467
607
 
468
- return cached;
469
- } // Given a reference to a fragment, return the represented collection of fields
470
- // as well as a list of nested fragment names referenced via fragment spreads.
471
-
472
-
473
- function getReferencedFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, fragment) {
474
- // Short-circuit building a type from the node if possible.
475
- var cached = cachedFieldsAndFragmentNames.get(fragment.selectionSet);
608
+ var cached = bySelectionSet.get(selectionSet);
476
609
 
477
610
  if (cached) {
478
611
  return cached;
479
612
  }
480
613
 
481
- var fragmentType = (0, _typeFromAST.typeFromAST)(context.getSchema(), fragment.typeCondition);
482
- return getFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, fragmentType, fragment.selectionSet);
614
+ var fieldDetails = Object.create(null);
615
+ var fragmentNames = Object.create(null);
616
+ collectFieldsImpl(validationContext, parentType, selectionSet, fieldDetails, fragmentNames);
617
+ var result = {
618
+ groupedFieldSet: fieldDetails,
619
+ responseNames: Object.keys(fieldDetails),
620
+ fragmentNames: Object.keys(fragmentNames)
621
+ };
622
+ bySelectionSet.set(selectionSet, result);
623
+ return result;
624
+ } // Given a reference to a fragment, return the represented collection of fields
625
+ // as well as a list of nested fragment names referenced via fragment spreads.
626
+
627
+
628
+ function collectFragmentFields(validationContext, ruleContext, fragment) {
629
+ var fragmentType = (0, _typeFromAST.typeFromAST)(validationContext.getSchema(), fragment.typeCondition);
630
+ return collectFields(validationContext, ruleContext, fragmentType, fragment.selectionSet);
483
631
  }
484
632
 
485
- function _collectFieldsAndFragmentNames(context, parentType, selectionSet, nodeAndDefs, fragmentNames) {
486
- for (var _i9 = 0, _selectionSet$selecti2 = selectionSet.selections; _i9 < _selectionSet$selecti2.length; _i9++) {
487
- var selection = _selectionSet$selecti2[_i9];
633
+ function collectFieldsImpl(validationContext, parentType, selectionSet, fieldDetails, fragmentNames) {
634
+ for (var _i13 = 0, _selectionSet$selecti2 = selectionSet.selections; _i13 < _selectionSet$selecti2.length; _i13++) {
635
+ var selection = _selectionSet$selecti2[_i13];
488
636
 
489
637
  switch (selection.kind) {
490
638
  case _kinds.Kind.FIELD:
491
639
  {
640
+ var _selection$arguments$, _selection$arguments;
641
+
492
642
  var fieldName = selection.name.value;
493
643
  var fieldDef = void 0;
494
644
 
@@ -498,11 +648,16 @@ function _collectFieldsAndFragmentNames(context, parentType, selectionSet, nodeA
498
648
 
499
649
  var responseName = selection.alias ? selection.alias.value : fieldName;
500
650
 
501
- if (!nodeAndDefs[responseName]) {
502
- nodeAndDefs[responseName] = [];
651
+ if (!fieldDetails[responseName]) {
652
+ fieldDetails[responseName] = [];
503
653
  }
504
654
 
505
- nodeAndDefs[responseName].push([parentType, selection, fieldDef]);
655
+ fieldDetails[responseName].push({
656
+ parentType: parentType,
657
+ node: selection,
658
+ definition: fieldDef,
659
+ isSimpleLeaf: ((_selection$arguments$ = (_selection$arguments = selection.arguments) === null || _selection$arguments === void 0 ? void 0 : _selection$arguments.length) !== null && _selection$arguments$ !== void 0 ? _selection$arguments$ : 0) === 0 && !selection.selectionSet
660
+ });
506
661
  break;
507
662
  }
508
663
 
@@ -513,10 +668,8 @@ function _collectFieldsAndFragmentNames(context, parentType, selectionSet, nodeA
513
668
  case _kinds.Kind.INLINE_FRAGMENT:
514
669
  {
515
670
  var typeCondition = selection.typeCondition;
516
- var inlineFragmentType = typeCondition ? (0, _typeFromAST.typeFromAST)(context.getSchema(), typeCondition) : parentType;
517
-
518
- _collectFieldsAndFragmentNames(context, inlineFragmentType, selection.selectionSet, nodeAndDefs, fragmentNames);
519
-
671
+ var inlineFragmentType = typeCondition ? (0, _typeFromAST.typeFromAST)(validationContext.getSchema(), typeCondition) : parentType;
672
+ collectFieldsImpl(validationContext, inlineFragmentType, selection.selectionSet, fieldDetails, fragmentNames);
520
673
  break;
521
674
  }
522
675
  }
@@ -527,16 +680,57 @@ function _collectFieldsAndFragmentNames(context, parentType, selectionSet, nodeA
527
680
 
528
681
  function subfieldConflicts(conflicts, responseName, node1, node2) {
529
682
  if (conflicts.length > 0) {
530
- return [[responseName, conflicts.map(function (_ref6) {
531
- var reason = _ref6[0];
532
- return reason;
533
- })], conflicts.reduce(function (allFields, _ref7) {
534
- var fields1 = _ref7[1];
535
- return allFields.concat(fields1);
536
- }, [node1]), conflicts.reduce(function (allFields, _ref8) {
537
- var fields2 = _ref8[2];
538
- return allFields.concat(fields2);
539
- }, [node2])];
683
+ var message = '';
684
+
685
+ for (var _i15 = 0; _i15 < conflicts.length; _i15++) {
686
+ var _ref8 = conflicts[_i15];
687
+ var name = _ref8.responseName;
688
+ var reason = _ref8.reason;
689
+ message += (message === '' ? '' : ' and ') + "subfields \"".concat(maybeAbbreviate(name), "\" conflict because ").concat(reason);
690
+
691
+ if (message.length > MAX_CONFLICT_DETAIL_LENGTH) {
692
+ break;
693
+ }
694
+ }
695
+
696
+ return {
697
+ responseName: responseName,
698
+ reason: maybeAbbreviate(message),
699
+ fields1: collectConflictNodes(node1, conflicts, 'fields1'),
700
+ fields2: collectConflictNodes(node2, conflicts, 'fields2')
701
+ };
702
+ }
703
+ }
704
+
705
+ function collectConflictNodes(node, conflicts, side) {
706
+ var nodes = [node];
707
+
708
+ for (var _i17 = 0; _i17 < conflicts.length; _i17++) {
709
+ var conflict = conflicts[_i17];
710
+ var remaining = MAX_CONFLICT_NODES - nodes.length; // Locations start with the enclosing field. Take from the end so that
711
+ // truncation favors the deeper fields involved in the conflict.
712
+
713
+ var retainedNodes = (side === 'fields1' ? conflict.fields1 : conflict.fields2).slice(-remaining);
714
+ nodes.push.apply(nodes, retainedNodes);
715
+
716
+ if (nodes.length === MAX_CONFLICT_NODES) {
717
+ break;
718
+ }
719
+ }
720
+
721
+ return nodes;
722
+ }
723
+
724
+ function maybeAbbreviate(text) {
725
+ return text.length > MAX_CONFLICT_DETAIL_LENGTH ? text.slice(0, MAX_CONFLICT_DETAIL_LENGTH) + '...' : text;
726
+ }
727
+
728
+ function spendComplexityBudget(ruleContext, cost) {
729
+ ruleContext.remainingComplexityBudget -= cost;
730
+ /* istanbul ignore if -- exercised by private complexity calibration */
731
+
732
+ if (ruleContext.remainingComplexityBudget < 0) {
733
+ throw _validationAbortedError.validationAbortedError;
540
734
  }
541
735
  }
542
736
  /**