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