graphql 16.14.2 → 16.15.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 (240) hide show
  1. package/error/GraphQLError.d.ts +5 -1
  2. package/error/GraphQLError.js +12 -7
  3. package/error/GraphQLError.mjs +13 -6
  4. package/error/locatedError.d.ts +6 -4
  5. package/error/locatedError.js +6 -4
  6. package/error/locatedError.mjs +6 -4
  7. package/execution/collectFields.d.ts +1 -0
  8. package/execution/collectFields.js +9 -0
  9. package/execution/collectFields.mjs +9 -0
  10. package/execution/execute.d.ts +9 -10
  11. package/execution/execute.js +44 -29
  12. package/execution/execute.mjs +44 -29
  13. package/execution/mapAsyncIterator.js +12 -11
  14. package/execution/mapAsyncIterator.mjs +12 -11
  15. package/execution/subscribe.d.ts +5 -2
  16. package/execution/subscribe.js +5 -2
  17. package/execution/subscribe.mjs +5 -2
  18. package/execution/values.d.ts +43 -21
  19. package/execution/values.js +52 -24
  20. package/execution/values.mjs +51 -23
  21. package/graphql.d.ts +8 -14
  22. package/graphql.js +16 -15
  23. package/graphql.mjs +11 -15
  24. package/jsutils/SetMap.d.ts +14 -0
  25. package/jsutils/SetMap.js +48 -0
  26. package/jsutils/SetMap.mjs +39 -0
  27. package/jsutils/isIterableObject.d.ts +2 -0
  28. package/jsutils/isIterableObject.js +2 -0
  29. package/jsutils/isIterableObject.mjs +2 -0
  30. package/jsutils/keyMap.d.ts +2 -0
  31. package/jsutils/keyMap.js +2 -0
  32. package/jsutils/keyMap.mjs +2 -0
  33. package/jsutils/keyValMap.d.ts +3 -3
  34. package/jsutils/keyValMap.js +3 -3
  35. package/jsutils/keyValMap.mjs +3 -3
  36. package/language/ast.d.ts +2 -6
  37. package/language/ast.js +2 -6
  38. package/language/ast.mjs +2 -6
  39. package/language/lexer.d.ts +6 -0
  40. package/language/lexer.js +22 -0
  41. package/language/lexer.mjs +22 -0
  42. package/language/location.d.ts +8 -3
  43. package/language/location.js +57 -17
  44. package/language/location.mjs +56 -16
  45. package/language/parser.d.ts +27 -13
  46. package/language/parser.js +31 -9
  47. package/language/parser.mjs +31 -9
  48. package/language/predicates.d.ts +10 -3
  49. package/language/predicates.js +10 -3
  50. package/language/predicates.mjs +10 -3
  51. package/language/printLocation.d.ts +2 -6
  52. package/language/printLocation.js +2 -6
  53. package/language/printLocation.mjs +2 -6
  54. package/language/printer.d.ts +22 -3
  55. package/language/printer.js +394 -308
  56. package/language/printer.mjs +394 -308
  57. package/language/reducer.d.ts +32 -0
  58. package/language/reducer.js +95 -0
  59. package/language/reducer.mjs +86 -0
  60. package/language/source.d.ts +4 -5
  61. package/language/source.js +4 -5
  62. package/language/source.mjs +4 -5
  63. package/language/visitor.d.ts +16 -10
  64. package/language/visitor.js +15 -7
  65. package/language/visitor.mjs +15 -7
  66. package/package.json +1 -4
  67. package/type/definition.d.ts +170 -102
  68. package/type/definition.js +169 -96
  69. package/type/definition.mjs +168 -95
  70. package/type/directives.d.ts +12 -6
  71. package/type/directives.js +12 -6
  72. package/type/directives.mjs +12 -6
  73. package/type/scalars.js +8 -11
  74. package/type/scalars.mjs +1 -1
  75. package/type/schema.d.ts +36 -11
  76. package/type/schema.js +35 -10
  77. package/type/schema.mjs +35 -10
  78. package/type/validate.d.ts +1 -3
  79. package/type/validate.js +1 -3
  80. package/type/validate.mjs +1 -3
  81. package/utilities/TypeInfo.d.ts +15 -11
  82. package/utilities/TypeInfo.js +15 -11
  83. package/utilities/TypeInfo.mjs +15 -11
  84. package/utilities/assertValidName.d.ts +1 -1
  85. package/utilities/assertValidName.js +1 -1
  86. package/utilities/assertValidName.mjs +1 -1
  87. package/utilities/astFromValue.d.ts +3 -1
  88. package/utilities/astFromValue.js +3 -1
  89. package/utilities/astFromValue.mjs +3 -1
  90. package/utilities/buildASTSchema.d.ts +8 -8
  91. package/utilities/buildASTSchema.js +9 -20
  92. package/utilities/buildASTSchema.mjs +9 -20
  93. package/utilities/buildClientSchema.d.ts +6 -2
  94. package/utilities/buildClientSchema.js +6 -2
  95. package/utilities/buildClientSchema.mjs +6 -2
  96. package/utilities/coerceInputValue.d.ts +9 -3
  97. package/utilities/coerceInputValue.js +60 -13
  98. package/utilities/coerceInputValue.mjs +58 -13
  99. package/utilities/concatAST.d.ts +4 -1
  100. package/utilities/concatAST.js +4 -1
  101. package/utilities/concatAST.mjs +4 -1
  102. package/utilities/extendSchema.d.ts +7 -4
  103. package/utilities/extendSchema.js +7 -4
  104. package/utilities/extendSchema.mjs +7 -4
  105. package/utilities/findBreakingChanges.d.ts +2 -2
  106. package/utilities/findBreakingChanges.js +2 -2
  107. package/utilities/findBreakingChanges.mjs +2 -2
  108. package/utilities/getIntrospectionQuery.d.ts +9 -9
  109. package/utilities/getIntrospectionQuery.js +8 -8
  110. package/utilities/getIntrospectionQuery.mjs +8 -8
  111. package/utilities/getOperationAST.d.ts +2 -2
  112. package/utilities/getOperationAST.js +2 -2
  113. package/utilities/getOperationAST.mjs +2 -2
  114. package/utilities/getOperationRootType.d.ts +3 -1
  115. package/utilities/getOperationRootType.js +3 -1
  116. package/utilities/getOperationRootType.mjs +3 -1
  117. package/utilities/introspectionFromSchema.d.ts +12 -4
  118. package/utilities/introspectionFromSchema.js +12 -4
  119. package/utilities/introspectionFromSchema.mjs +12 -4
  120. package/utilities/lexicographicSortSchema.d.ts +17 -13
  121. package/utilities/lexicographicSortSchema.js +17 -13
  122. package/utilities/lexicographicSortSchema.mjs +17 -13
  123. package/utilities/printSchema.d.ts +7 -4
  124. package/utilities/printSchema.js +7 -4
  125. package/utilities/printSchema.mjs +7 -4
  126. package/utilities/resolveSchemaCoordinate.d.ts +9 -8
  127. package/utilities/resolveSchemaCoordinate.js +9 -8
  128. package/utilities/resolveSchemaCoordinate.mjs +9 -8
  129. package/utilities/separateOperations.d.ts +2 -2
  130. package/utilities/separateOperations.js +2 -2
  131. package/utilities/separateOperations.mjs +2 -2
  132. package/utilities/stripIgnoredCharacters.d.ts +19 -5
  133. package/utilities/stripIgnoredCharacters.js +19 -5
  134. package/utilities/stripIgnoredCharacters.mjs +19 -5
  135. package/utilities/typeComparators.d.ts +1 -5
  136. package/utilities/typeComparators.js +1 -5
  137. package/utilities/typeComparators.mjs +1 -5
  138. package/utilities/valueFromAST.js +4 -4
  139. package/utilities/valueFromAST.mjs +4 -4
  140. package/utilities/valueFromASTUntyped.d.ts +1 -3
  141. package/utilities/valueFromASTUntyped.js +1 -3
  142. package/utilities/valueFromASTUntyped.mjs +1 -3
  143. package/validation/ValidationContext.d.ts +22 -3
  144. package/validation/ValidationContext.js +84 -27
  145. package/validation/ValidationContext.mjs +82 -27
  146. package/validation/rules/ExecutableDefinitionsRule.d.ts +6 -4
  147. package/validation/rules/ExecutableDefinitionsRule.js +6 -4
  148. package/validation/rules/ExecutableDefinitionsRule.mjs +6 -4
  149. package/validation/rules/FieldsOnCorrectTypeRule.d.ts +4 -4
  150. package/validation/rules/FieldsOnCorrectTypeRule.js +10 -6
  151. package/validation/rules/FieldsOnCorrectTypeRule.mjs +12 -6
  152. package/validation/rules/FragmentsOnCompositeTypesRule.d.ts +6 -4
  153. package/validation/rules/FragmentsOnCompositeTypesRule.js +6 -4
  154. package/validation/rules/FragmentsOnCompositeTypesRule.mjs +6 -4
  155. package/validation/rules/KnownArgumentNamesRule.d.ts +4 -4
  156. package/validation/rules/KnownArgumentNamesRule.js +26 -17
  157. package/validation/rules/KnownArgumentNamesRule.mjs +21 -11
  158. package/validation/rules/KnownDirectivesRule.d.ts +1 -3
  159. package/validation/rules/KnownDirectivesRule.js +1 -3
  160. package/validation/rules/KnownDirectivesRule.mjs +1 -3
  161. package/validation/rules/KnownFragmentNamesRule.d.ts +4 -4
  162. package/validation/rules/KnownFragmentNamesRule.js +4 -4
  163. package/validation/rules/KnownFragmentNamesRule.mjs +4 -4
  164. package/validation/rules/KnownTypeNamesRule.d.ts +1 -3
  165. package/validation/rules/KnownTypeNamesRule.js +7 -7
  166. package/validation/rules/KnownTypeNamesRule.mjs +7 -7
  167. package/validation/rules/LoneAnonymousOperationRule.d.ts +6 -4
  168. package/validation/rules/LoneAnonymousOperationRule.js +6 -4
  169. package/validation/rules/LoneAnonymousOperationRule.mjs +6 -4
  170. package/validation/rules/MaxIntrospectionDepthRule.d.ts +6 -4
  171. package/validation/rules/MaxIntrospectionDepthRule.js +44 -19
  172. package/validation/rules/MaxIntrospectionDepthRule.mjs +44 -19
  173. package/validation/rules/NoFragmentCyclesRule.d.ts +1 -3
  174. package/validation/rules/NoFragmentCyclesRule.js +1 -3
  175. package/validation/rules/NoFragmentCyclesRule.mjs +1 -3
  176. package/validation/rules/NoUndefinedVariablesRule.d.ts +4 -4
  177. package/validation/rules/NoUndefinedVariablesRule.js +4 -4
  178. package/validation/rules/NoUndefinedVariablesRule.mjs +4 -4
  179. package/validation/rules/NoUnusedFragmentsRule.d.ts +4 -4
  180. package/validation/rules/NoUnusedFragmentsRule.js +10 -14
  181. package/validation/rules/NoUnusedFragmentsRule.mjs +10 -14
  182. package/validation/rules/NoUnusedVariablesRule.d.ts +4 -4
  183. package/validation/rules/NoUnusedVariablesRule.js +4 -4
  184. package/validation/rules/NoUnusedVariablesRule.mjs +4 -4
  185. package/validation/rules/OverlappingFieldsCanBeMergedRule.d.ts +8 -6
  186. package/validation/rules/OverlappingFieldsCanBeMergedRule.js +552 -354
  187. package/validation/rules/OverlappingFieldsCanBeMergedRule.mjs +545 -352
  188. package/validation/rules/PossibleFragmentSpreadsRule.d.ts +6 -4
  189. package/validation/rules/PossibleFragmentSpreadsRule.js +6 -4
  190. package/validation/rules/PossibleFragmentSpreadsRule.mjs +6 -4
  191. package/validation/rules/ProvidedRequiredArgumentsRule.d.ts +6 -4
  192. package/validation/rules/ProvidedRequiredArgumentsRule.js +6 -4
  193. package/validation/rules/ProvidedRequiredArgumentsRule.mjs +6 -4
  194. package/validation/rules/ScalarLeafsRule.d.ts +1 -3
  195. package/validation/rules/ScalarLeafsRule.js +1 -3
  196. package/validation/rules/ScalarLeafsRule.mjs +1 -3
  197. package/validation/rules/SingleFieldSubscriptionsRule.d.ts +6 -4
  198. package/validation/rules/SingleFieldSubscriptionsRule.js +50 -9
  199. package/validation/rules/SingleFieldSubscriptionsRule.mjs +50 -9
  200. package/validation/rules/UniqueArgumentNamesRule.d.ts +4 -4
  201. package/validation/rules/UniqueArgumentNamesRule.js +4 -4
  202. package/validation/rules/UniqueArgumentNamesRule.mjs +4 -4
  203. package/validation/rules/UniqueDirectivesPerLocationRule.d.ts +6 -4
  204. package/validation/rules/UniqueDirectivesPerLocationRule.js +6 -4
  205. package/validation/rules/UniqueDirectivesPerLocationRule.mjs +6 -4
  206. package/validation/rules/UniqueFragmentNamesRule.d.ts +4 -4
  207. package/validation/rules/UniqueFragmentNamesRule.js +4 -4
  208. package/validation/rules/UniqueFragmentNamesRule.mjs +4 -4
  209. package/validation/rules/UniqueInputFieldNamesRule.d.ts +6 -4
  210. package/validation/rules/UniqueInputFieldNamesRule.js +6 -4
  211. package/validation/rules/UniqueInputFieldNamesRule.mjs +6 -4
  212. package/validation/rules/UniqueOperationNamesRule.d.ts +4 -4
  213. package/validation/rules/UniqueOperationNamesRule.js +4 -4
  214. package/validation/rules/UniqueOperationNamesRule.mjs +4 -4
  215. package/validation/rules/UniqueVariableNamesRule.d.ts +4 -4
  216. package/validation/rules/UniqueVariableNamesRule.js +4 -4
  217. package/validation/rules/UniqueVariableNamesRule.mjs +4 -4
  218. package/validation/rules/ValuesOfCorrectTypeRule.d.ts +4 -4
  219. package/validation/rules/ValuesOfCorrectTypeRule.js +29 -23
  220. package/validation/rules/ValuesOfCorrectTypeRule.mjs +24 -14
  221. package/validation/rules/VariablesAreInputTypesRule.d.ts +6 -4
  222. package/validation/rules/VariablesAreInputTypesRule.js +6 -4
  223. package/validation/rules/VariablesAreInputTypesRule.mjs +6 -4
  224. package/validation/rules/VariablesInAllowedPositionRule.d.ts +6 -4
  225. package/validation/rules/VariablesInAllowedPositionRule.js +6 -4
  226. package/validation/rules/VariablesInAllowedPositionRule.mjs +6 -4
  227. package/validation/rules/custom/NoDeprecatedCustomRule.d.ts +4 -4
  228. package/validation/rules/custom/NoDeprecatedCustomRule.js +4 -4
  229. package/validation/rules/custom/NoDeprecatedCustomRule.mjs +4 -4
  230. package/validation/rules/custom/NoSchemaIntrospectionCustomRule.d.ts +7 -5
  231. package/validation/rules/custom/NoSchemaIntrospectionCustomRule.js +7 -5
  232. package/validation/rules/custom/NoSchemaIntrospectionCustomRule.mjs +7 -5
  233. package/validation/validate.d.ts +2 -0
  234. package/validation/validate.js +14 -2
  235. package/validation/validate.mjs +13 -2
  236. package/validation/validationAbortedError.d.ts +3 -0
  237. package/validation/validationAbortedError.js +14 -0
  238. package/validation/validationAbortedError.mjs +4 -0
  239. package/version.js +3 -3
  240. package/version.mjs +3 -3
@@ -9,30 +9,23 @@ var _inspect = require('../../jsutils/inspect.js');
9
9
 
10
10
  var _GraphQLError = require('../../error/GraphQLError.js');
11
11
 
12
- var _kinds = require('../../language/kinds.js');
12
+ var _ast = require('../../language/ast.js');
13
13
 
14
- var _printer = require('../../language/printer.js');
14
+ var _kinds = require('../../language/kinds.js');
15
15
 
16
16
  var _definition = require('../../type/definition.js');
17
17
 
18
- var _sortValueNode = require('../../utilities/sortValueNode.js');
19
-
20
18
  var _typeFromAST = require('../../utilities/typeFromAST.js');
21
19
 
20
+ var _validationAbortedError = require('../validationAbortedError.js');
21
+
22
22
  /** @category Validation Rules */
23
- function reasonMessage(reason) {
24
- if (Array.isArray(reason)) {
25
- return reason
26
- .map(
27
- ([responseName, subReason]) =>
28
- `subfields "${responseName}" conflict because ` +
29
- reasonMessage(subReason),
30
- )
31
- .join(' and ');
32
- }
33
-
34
- return reason;
35
- }
23
+ // Keep diagnostic detail bounded independently of document size.
24
+ const MAX_CONFLICT_DETAIL_LENGTH = 1000;
25
+ const MAX_CONFLICT_NODES = 20; // Meter work that can grow through repeated comparisons.
26
+ // Collecting a selection set's fields is cached and is not charged.
27
+
28
+ const MAX_COMPLEXITY_BUDGET = 500000;
36
29
  /**
37
30
  * Overlapping fields can be merged
38
31
  *
@@ -41,7 +34,7 @@ function reasonMessage(reason) {
41
34
  * without ambiguity.
42
35
  *
43
36
  * See https://spec.graphql.org/draft/#sec-Field-Selection-Merging
44
- * @param context - The validation context used while checking the document.
37
+ * @param validationContext - The validation context used while checking the document.
45
38
  * @returns A visitor that reports validation errors for this rule.
46
39
  * @example
47
40
  * ```ts
@@ -62,45 +55,70 @@ function reasonMessage(reason) {
62
55
  * const invalidDocument = parse(`
63
56
  * { dog { value: barkVolume value: name } }
64
57
  * `);
65
- * const invalidErrors = validate(schema, invalidDocument, [OverlappingFieldsCanBeMergedRule]);
58
+ * const invalidErrors = validate(schema, invalidDocument, [
59
+ * OverlappingFieldsCanBeMergedRule,
60
+ * ]);
66
61
  *
67
62
  * invalidErrors.length; // => 1
68
63
  *
69
64
  * const validDocument = parse(`
70
65
  * { dog { barkVolume name } }
71
66
  * `);
72
- * const validErrors = validate(schema, validDocument, [OverlappingFieldsCanBeMergedRule]);
73
- *
74
- * validErrors; // => []
67
+ * const validErrors = validate(schema, validDocument, [
68
+ * OverlappingFieldsCanBeMergedRule,
69
+ * ]); // => []
75
70
  * ```
76
71
  */
77
72
 
78
- function OverlappingFieldsCanBeMergedRule(context) {
79
- // A memoization for when fields and a fragment or two fragments are compared
80
- // "between" each other for conflicts. Comparisons made be made many times,
81
- // so memoizing this can dramatically improve the performance of this validator.
82
- const comparedFieldsAndFragmentPairs = new OrderedPairSet();
83
- const comparedFragmentPairs = new PairSet(); // A cache for the "field map" and list of fragment names found in any given
84
- // selection set. Selection sets may be asked for this information multiple
85
- // times, so this improves the performance of this validator.
86
-
87
- const cachedFieldsAndFragmentNames = new Map();
73
+ function OverlappingFieldsCanBeMergedRule(validationContext) {
74
+ const ruleContext = {
75
+ // A memoization for when two fragments are compared for conflicts.
76
+ // Comparisons may be made many times, so memoizing this can dramatically
77
+ // improve the performance of this validator.
78
+ comparedFragmentPairs: new PairSet(),
79
+ // A cache for the "field map" and list of fragment names found in any given
80
+ // selection set.
81
+ cachedCollectFieldsResults: new Map(),
82
+ stringifiedValues: new Map(),
83
+ remainingComplexityBudget: MAX_COMPLEXITY_BUDGET,
84
+ };
88
85
  return {
89
- SelectionSet(selectionSet) {
86
+ SelectionSet(selectionSet, _key, parent) {
87
+ var _selectionSet$selecti;
88
+
89
+ // Inline fragments are flattened into their enclosing response level.
90
+ // Their fields' own selection sets are still visited independently.
91
+ if (
92
+ parent &&
93
+ 'kind' in parent &&
94
+ parent.kind === _kinds.Kind.INLINE_FRAGMENT
95
+ ) {
96
+ return;
97
+ }
98
+
99
+ if (
100
+ selectionSet.selections.length <= 1 &&
101
+ ((_selectionSet$selecti = selectionSet.selections[0]) === null ||
102
+ _selectionSet$selecti === void 0
103
+ ? void 0
104
+ : _selectionSet$selecti.kind) !== _kinds.Kind.INLINE_FRAGMENT
105
+ ) {
106
+ return;
107
+ }
108
+
90
109
  const conflicts = findConflictsWithinSelectionSet(
91
- context,
92
- cachedFieldsAndFragmentNames,
93
- comparedFieldsAndFragmentPairs,
94
- comparedFragmentPairs,
95
- context.getParentType(),
110
+ validationContext,
111
+ ruleContext,
112
+ validationContext.getParentType(),
96
113
  selectionSet,
97
114
  );
98
115
 
99
- for (const [[responseName, reason], fields1, fields2] of conflicts) {
100
- const reasonMsg = reasonMessage(reason);
101
- context.reportError(
116
+ for (const { responseName, reason, fields1, fields2 } of conflicts) {
117
+ validationContext.reportError(
102
118
  new _GraphQLError.GraphQLError(
103
- `Fields "${responseName}" conflict because ${reasonMsg}. Use different aliases on the fields to fetch both if this was intentional.`,
119
+ `Fields "${maybeAbbreviate(
120
+ responseName,
121
+ )}" conflict because ${reason}. Use different aliases on the fields to fetch both if this was intentional.`,
104
122
  {
105
123
  nodes: fields1.concat(fields2),
106
124
  },
@@ -171,59 +189,55 @@ function OverlappingFieldsCanBeMergedRule(context) {
171
189
  * @internal
172
190
  */
173
191
  function findConflictsWithinSelectionSet(
174
- context,
175
- cachedFieldsAndFragmentNames,
176
- comparedFieldsAndFragmentPairs,
177
- comparedFragmentPairs,
192
+ validationContext,
193
+ ruleContext,
178
194
  parentType,
179
195
  selectionSet,
180
196
  ) {
181
197
  const conflicts = [];
182
- const [fieldMap, fragmentNames] = getFieldsAndFragmentNames(
183
- context,
184
- cachedFieldsAndFragmentNames,
198
+ const collectFieldsResult = collectFields(
199
+ validationContext,
200
+ ruleContext,
185
201
  parentType,
186
202
  selectionSet,
187
203
  ); // (A) Find find all conflicts "within" the fields of this selection set.
188
204
  // Note: this is the *only place* `collectConflictsWithin` is called.
189
205
 
190
206
  collectConflictsWithin(
191
- context,
207
+ validationContext,
192
208
  conflicts,
193
- cachedFieldsAndFragmentNames,
194
- comparedFieldsAndFragmentPairs,
195
- comparedFragmentPairs,
196
- fieldMap,
197
- );
209
+ ruleContext,
210
+ collectFieldsResult.groupedFieldSet,
211
+ ); // Compare fragment uses only with fields or another spread.
198
212
 
199
- if (fragmentNames.length !== 0) {
213
+ if (
214
+ collectFieldsResult.fragmentNames.length !== 0 &&
215
+ (collectFieldsResult.groupedFieldSet.size !== 0 ||
216
+ collectFieldsResult.fragmentNames.length > 1)
217
+ ) {
200
218
  // (B) Then collect conflicts between these fields and those represented by
201
219
  // each spread fragment name found.
202
- for (let i = 0; i < fragmentNames.length; i++) {
220
+ for (let i = 0; i < collectFieldsResult.fragmentNames.length; i++) {
203
221
  collectConflictsBetweenFieldsAndFragment(
204
- context,
222
+ validationContext,
205
223
  conflicts,
206
- cachedFieldsAndFragmentNames,
207
- comparedFieldsAndFragmentPairs,
208
- comparedFragmentPairs,
224
+ ruleContext,
209
225
  false,
210
- fieldMap,
211
- fragmentNames[i],
226
+ collectFieldsResult,
227
+ collectFieldsResult.fragmentNames[i],
212
228
  ); // (C) Then compare this fragment with all other fragments found in this
213
229
  // selection set to collect conflicts between fragments spread together.
214
230
  // This compares each item in the list of fragment names to every other
215
231
  // item in that same list (except for itself).
216
232
 
217
- for (let j = i + 1; j < fragmentNames.length; j++) {
233
+ for (let j = i + 1; j < collectFieldsResult.fragmentNames.length; j++) {
218
234
  collectConflictsBetweenFragments(
219
- context,
235
+ validationContext,
220
236
  conflicts,
221
- cachedFieldsAndFragmentNames,
222
- comparedFieldsAndFragmentPairs,
223
- comparedFragmentPairs,
237
+ ruleContext,
224
238
  false,
225
- fragmentNames[i],
226
- fragmentNames[j],
239
+ collectFieldsResult.fragmentNames[i],
240
+ collectFieldsResult.fragmentNames[j],
227
241
  );
228
242
  }
229
243
  }
@@ -234,71 +248,71 @@ function findConflictsWithinSelectionSet(
234
248
  // including via spreading in any nested fragments.
235
249
 
236
250
  function collectConflictsBetweenFieldsAndFragment(
237
- context,
251
+ validationContext,
238
252
  conflicts,
239
- cachedFieldsAndFragmentNames,
240
- comparedFieldsAndFragmentPairs,
241
- comparedFragmentPairs,
253
+ ruleContext,
242
254
  areMutuallyExclusive,
243
- fieldMap,
255
+ collectFieldsResult,
244
256
  fragmentName,
245
257
  ) {
246
- // Memoize so the fields and fragments are not compared for conflicts more
258
+ var _collectFieldsResult$;
259
+
260
+ // Callers can enumerate more field-map/fragment pairs than document nodes.
261
+ // Count attempts even when an empty collection or the pair cache skips them.
262
+ spendComplexityBudget(ruleContext, 1);
263
+
264
+ if (collectFieldsResult.groupedFieldSet.size === 0) {
265
+ return;
266
+ }
267
+
268
+ const comparedFragments =
269
+ (_collectFieldsResult$ = collectFieldsResult.comparedFragments) !== null &&
270
+ _collectFieldsResult$ !== void 0
271
+ ? _collectFieldsResult$
272
+ : (collectFieldsResult.comparedFragments = new Map()); // Memoize so the fields and fragments are not compared for conflicts more
247
273
  // than once.
248
- if (
249
- comparedFieldsAndFragmentPairs.has(
250
- fieldMap,
251
- fragmentName,
252
- areMutuallyExclusive,
253
- )
254
- ) {
274
+
275
+ const compared = comparedFragments.get(fragmentName); // Only a non-exclusive comparison can be reused for overlapping parents.
276
+
277
+ if (compared === false || (areMutuallyExclusive && compared === true)) {
255
278
  return;
256
279
  }
257
280
 
258
- comparedFieldsAndFragmentPairs.add(
259
- fieldMap,
260
- fragmentName,
261
- areMutuallyExclusive,
262
- );
263
- const fragment = context.getFragment(fragmentName);
281
+ comparedFragments.set(fragmentName, areMutuallyExclusive);
282
+ const fragment = validationContext.getFragment(fragmentName);
264
283
 
265
284
  if (!fragment) {
266
285
  return;
267
286
  }
268
287
 
269
- const [fieldMap2, referencedFragmentNames] =
270
- getReferencedFieldsAndFragmentNames(
271
- context,
272
- cachedFieldsAndFragmentNames,
273
- fragment,
274
- ); // Do not compare a fragment's fieldMap to itself.
288
+ const collectFieldsResult2 = collectFragmentFields(
289
+ validationContext,
290
+ ruleContext,
291
+ fragment,
292
+ ); // Do not compare a fragment's collectFieldsResult to itself.
275
293
 
276
- if (fieldMap === fieldMap2) {
294
+ if (collectFieldsResult === collectFieldsResult2) {
277
295
  return;
278
296
  } // (D) First collect any conflicts between the provided collection of fields
279
297
  // and the collection of fields represented by the given fragment.
280
298
 
281
299
  collectConflictsBetween(
282
- context,
300
+ validationContext,
283
301
  conflicts,
284
- cachedFieldsAndFragmentNames,
285
- comparedFieldsAndFragmentPairs,
286
- comparedFragmentPairs,
302
+ ruleContext,
287
303
  areMutuallyExclusive,
288
- fieldMap,
289
- fieldMap2,
304
+ collectFieldsResult,
305
+ collectFieldsResult2,
290
306
  ); // (E) Then collect any conflicts between the provided collection of fields
291
307
  // and any fragment names found in the given fragment.
292
308
 
293
- for (const referencedFragmentName of referencedFragmentNames) {
309
+ for (const referencedFragmentName of collectFieldsResult2.fragmentNames) {
294
310
  collectConflictsBetweenFieldsAndFragment(
295
- context,
311
+ validationContext,
296
312
  conflicts,
297
- cachedFieldsAndFragmentNames,
298
- comparedFieldsAndFragmentPairs,
299
- comparedFragmentPairs,
313
+ ruleContext,
300
314
  areMutuallyExclusive,
301
- fieldMap,
315
+ collectFieldsResult,
302
316
  referencedFragmentName,
303
317
  );
304
318
  }
@@ -306,16 +320,17 @@ function collectConflictsBetweenFieldsAndFragment(
306
320
  // any nested fragments.
307
321
 
308
322
  function collectConflictsBetweenFragments(
309
- context,
323
+ validationContext,
310
324
  conflicts,
311
- cachedFieldsAndFragmentNames,
312
- comparedFieldsAndFragmentPairs,
313
- comparedFragmentPairs,
325
+ ruleContext,
314
326
  areMutuallyExclusive,
315
327
  fragmentName1,
316
328
  fragmentName2,
317
329
  ) {
318
- // No need to compare a fragment to itself.
330
+ // Count attempted fragment pairs, including identity and cache hits.
331
+ spendComplexityBudget(ruleContext, 2);
332
+ const { comparedFragmentPairs } = ruleContext; // No need to compare a fragment to itself.
333
+
319
334
  if (fragmentName1 === fragmentName2) {
320
335
  return;
321
336
  } // Memoize so two fragments are not compared for conflicts more than once.
@@ -331,46 +346,40 @@ function collectConflictsBetweenFragments(
331
346
  }
332
347
 
333
348
  comparedFragmentPairs.add(fragmentName1, fragmentName2, areMutuallyExclusive);
334
- const fragment1 = context.getFragment(fragmentName1);
335
- const fragment2 = context.getFragment(fragmentName2);
349
+ const fragment1 = validationContext.getFragment(fragmentName1);
350
+ const fragment2 = validationContext.getFragment(fragmentName2);
336
351
 
337
352
  if (!fragment1 || !fragment2) {
338
353
  return;
339
354
  }
340
355
 
341
- const [fieldMap1, referencedFragmentNames1] =
342
- getReferencedFieldsAndFragmentNames(
343
- context,
344
- cachedFieldsAndFragmentNames,
345
- fragment1,
346
- );
347
- const [fieldMap2, referencedFragmentNames2] =
348
- getReferencedFieldsAndFragmentNames(
349
- context,
350
- cachedFieldsAndFragmentNames,
351
- fragment2,
352
- ); // (F) First, collect all conflicts between these two collections of fields
356
+ const collectFieldsResult1 = collectFragmentFields(
357
+ validationContext,
358
+ ruleContext,
359
+ fragment1,
360
+ );
361
+ const collectFieldsResult2 = collectFragmentFields(
362
+ validationContext,
363
+ ruleContext,
364
+ fragment2,
365
+ ); // (F) First, collect all conflicts between these two collections of fields
353
366
  // (not including any nested fragments).
354
367
 
355
368
  collectConflictsBetween(
356
- context,
369
+ validationContext,
357
370
  conflicts,
358
- cachedFieldsAndFragmentNames,
359
- comparedFieldsAndFragmentPairs,
360
- comparedFragmentPairs,
371
+ ruleContext,
361
372
  areMutuallyExclusive,
362
- fieldMap1,
363
- fieldMap2,
373
+ collectFieldsResult1,
374
+ collectFieldsResult2,
364
375
  ); // (G) Then collect conflicts between the first fragment and any nested
365
376
  // fragments spread in the second fragment.
366
377
 
367
- for (const referencedFragmentName2 of referencedFragmentNames2) {
378
+ for (const referencedFragmentName2 of collectFieldsResult2.fragmentNames) {
368
379
  collectConflictsBetweenFragments(
369
- context,
380
+ validationContext,
370
381
  conflicts,
371
- cachedFieldsAndFragmentNames,
372
- comparedFieldsAndFragmentPairs,
373
- comparedFragmentPairs,
382
+ ruleContext,
374
383
  areMutuallyExclusive,
375
384
  fragmentName1,
376
385
  referencedFragmentName2,
@@ -378,13 +387,11 @@ function collectConflictsBetweenFragments(
378
387
  } // (G) Then collect conflicts between the second fragment and any nested
379
388
  // fragments spread in the first fragment.
380
389
 
381
- for (const referencedFragmentName1 of referencedFragmentNames1) {
390
+ for (const referencedFragmentName1 of collectFieldsResult1.fragmentNames) {
382
391
  collectConflictsBetweenFragments(
383
- context,
392
+ validationContext,
384
393
  conflicts,
385
- cachedFieldsAndFragmentNames,
386
- comparedFieldsAndFragmentPairs,
387
- comparedFragmentPairs,
394
+ ruleContext,
388
395
  areMutuallyExclusive,
389
396
  referencedFragmentName1,
390
397
  fragmentName2,
@@ -395,10 +402,8 @@ function collectConflictsBetweenFragments(
395
402
  // between the sub-fields of two overlapping fields.
396
403
 
397
404
  function findConflictsBetweenSubSelectionSets(
398
- context,
399
- cachedFieldsAndFragmentNames,
400
- comparedFieldsAndFragmentPairs,
401
- comparedFragmentPairs,
405
+ validationContext,
406
+ ruleContext,
402
407
  areMutuallyExclusive,
403
408
  parentType1,
404
409
  selectionSet1,
@@ -406,68 +411,60 @@ function findConflictsBetweenSubSelectionSets(
406
411
  selectionSet2,
407
412
  ) {
408
413
  const conflicts = [];
409
- const [fieldMap1, fragmentNames1] = getFieldsAndFragmentNames(
410
- context,
411
- cachedFieldsAndFragmentNames,
414
+ const collectFieldsResult1 = collectFields(
415
+ validationContext,
416
+ ruleContext,
412
417
  parentType1,
413
418
  selectionSet1,
414
419
  );
415
- const [fieldMap2, fragmentNames2] = getFieldsAndFragmentNames(
416
- context,
417
- cachedFieldsAndFragmentNames,
420
+ const collectFieldsResult2 = collectFields(
421
+ validationContext,
422
+ ruleContext,
418
423
  parentType2,
419
424
  selectionSet2,
420
425
  ); // (H) First, collect all conflicts between these two collections of field.
421
426
 
422
427
  collectConflictsBetween(
423
- context,
428
+ validationContext,
424
429
  conflicts,
425
- cachedFieldsAndFragmentNames,
426
- comparedFieldsAndFragmentPairs,
427
- comparedFragmentPairs,
430
+ ruleContext,
428
431
  areMutuallyExclusive,
429
- fieldMap1,
430
- fieldMap2,
432
+ collectFieldsResult1,
433
+ collectFieldsResult2,
431
434
  ); // (I) Then collect conflicts between the first collection of fields and
432
435
  // those referenced by each fragment name associated with the second.
433
436
 
434
- for (const fragmentName2 of fragmentNames2) {
437
+ for (const fragmentName2 of collectFieldsResult2.fragmentNames) {
435
438
  collectConflictsBetweenFieldsAndFragment(
436
- context,
439
+ validationContext,
437
440
  conflicts,
438
- cachedFieldsAndFragmentNames,
439
- comparedFieldsAndFragmentPairs,
440
- comparedFragmentPairs,
441
+ ruleContext,
441
442
  areMutuallyExclusive,
442
- fieldMap1,
443
+ collectFieldsResult1,
443
444
  fragmentName2,
444
445
  );
445
446
  } // (I) Then collect conflicts between the second collection of fields and
446
447
  // those referenced by each fragment name associated with the first.
447
448
 
448
- for (const fragmentName1 of fragmentNames1) {
449
+ for (const fragmentName1 of collectFieldsResult1.fragmentNames) {
449
450
  collectConflictsBetweenFieldsAndFragment(
450
- context,
451
+ validationContext,
451
452
  conflicts,
452
- cachedFieldsAndFragmentNames,
453
- comparedFieldsAndFragmentPairs,
454
- comparedFragmentPairs,
453
+ ruleContext,
455
454
  areMutuallyExclusive,
456
- fieldMap2,
455
+ collectFieldsResult2,
457
456
  fragmentName1,
458
457
  );
459
458
  } // (J) Also collect conflicts between any fragment names by the first and
460
459
  // fragment names by the second. This compares each item in the first set of
461
460
  // names to each item in the second set of names.
462
461
 
463
- for (const fragmentName1 of fragmentNames1) {
464
- for (const fragmentName2 of fragmentNames2) {
462
+ for (const fragmentName1 of collectFieldsResult1.fragmentNames) {
463
+ for (const fragmentName2 of collectFieldsResult2.fragmentNames) {
465
464
  collectConflictsBetweenFragments(
466
- context,
465
+ validationContext,
467
466
  conflicts,
468
- cachedFieldsAndFragmentNames,
469
- comparedFieldsAndFragmentPairs,
470
- comparedFragmentPairs,
467
+ ruleContext,
471
468
  areMutuallyExclusive,
472
469
  fragmentName1,
473
470
  fragmentName2,
@@ -479,18 +476,16 @@ function findConflictsBetweenSubSelectionSets(
479
476
  } // Collect all Conflicts "within" one collection of fields.
480
477
 
481
478
  function collectConflictsWithin(
482
- context,
479
+ validationContext,
483
480
  conflicts,
484
- cachedFieldsAndFragmentNames,
485
- comparedFieldsAndFragmentPairs,
486
- comparedFragmentPairs,
487
- fieldMap,
481
+ ruleContext,
482
+ groupedFieldSet,
488
483
  ) {
489
484
  // A field map is a keyed collection, where each key represents a response
490
485
  // name and the value at that key is a list of all fields which provide that
491
486
  // response name. For every response name, if there are multiple fields, they
492
487
  // must be compared to find a potential conflict.
493
- for (const [responseName, fields] of Object.entries(fieldMap)) {
488
+ for (const [responseName, fields] of groupedFieldSet) {
494
489
  // This compares every field in the list to every other field in this list
495
490
  // (except to itself). If the list only has one item, nothing needs to
496
491
  // be compared.
@@ -498,11 +493,9 @@ function collectConflictsWithin(
498
493
  for (let i = 0; i < fields.length; i++) {
499
494
  for (let j = i + 1; j < fields.length; j++) {
500
495
  const conflict = findConflict(
501
- context,
502
- cachedFieldsAndFragmentNames,
503
- comparedFieldsAndFragmentPairs,
504
- comparedFragmentPairs,
505
- false, // within one collection is never mutually exclusive
496
+ validationContext,
497
+ ruleContext,
498
+ false,
506
499
  responseName,
507
500
  fields[i],
508
501
  fields[j],
@@ -522,59 +515,106 @@ function collectConflictsWithin(
522
515
  // each individual selection set.
523
516
 
524
517
  function collectConflictsBetween(
525
- context,
518
+ validationContext,
526
519
  conflicts,
527
- cachedFieldsAndFragmentNames,
528
- comparedFieldsAndFragmentPairs,
529
- comparedFragmentPairs,
520
+ ruleContext,
530
521
  parentFieldsAreMutuallyExclusive,
531
- fieldMap1,
532
- fieldMap2,
522
+ collectFieldsResult1,
523
+ collectFieldsResult2,
533
524
  ) {
534
- // A field map is a keyed collection, where each key represents a response
525
+ if (
526
+ collectFieldsResult1.groupedFieldSet.size === 0 ||
527
+ collectFieldsResult2.groupedFieldSet.size === 0
528
+ ) {
529
+ return;
530
+ } // A field map is a keyed collection, where each key represents a response
535
531
  // name and the value at that key is a list of all fields which provide that
536
532
  // response name. For any response name which appears in both provided field
537
533
  // maps, each field from the first field map must be compared to every field
538
534
  // in the second field map to find potential conflicts.
539
- for (const [responseName, fields1] of Object.entries(fieldMap1)) {
540
- const fields2 = fieldMap2[responseName];
541
535
 
542
- if (fields2) {
543
- for (const field1 of fields1) {
544
- for (const field2 of fields2) {
545
- const conflict = findConflict(
546
- context,
547
- cachedFieldsAndFragmentNames,
548
- comparedFieldsAndFragmentPairs,
549
- comparedFragmentPairs,
550
- parentFieldsAreMutuallyExclusive,
551
- responseName,
552
- field1,
553
- field2,
554
- );
536
+ for (const { responseName, fields1, fields2 } of getOverlappingFields(
537
+ ruleContext,
538
+ collectFieldsResult1,
539
+ collectFieldsResult2,
540
+ )) {
541
+ for (const field1 of fields1) {
542
+ for (const field2 of fields2) {
543
+ const conflict = findConflict(
544
+ validationContext,
545
+ ruleContext,
546
+ parentFieldsAreMutuallyExclusive,
547
+ responseName,
548
+ field1,
549
+ field2,
550
+ );
555
551
 
556
- if (conflict) {
557
- conflicts.push(conflict);
558
- }
552
+ if (conflict) {
553
+ conflicts.push(conflict);
559
554
  }
560
555
  }
561
556
  }
562
557
  }
558
+ }
559
+
560
+ function getOverlappingFields(
561
+ ruleContext,
562
+ collectFieldsResult1,
563
+ collectFieldsResult2,
564
+ ) {
565
+ var _collectFieldsResult;
566
+
567
+ // Reuse matching response groups when these collections are compared again.
568
+ const byCollectFieldsResult =
569
+ (_collectFieldsResult = collectFieldsResult1.overlappingFields) !== null &&
570
+ _collectFieldsResult !== void 0
571
+ ? _collectFieldsResult
572
+ : (collectFieldsResult1.overlappingFields = new Map());
573
+ const cached = byCollectFieldsResult.get(collectFieldsResult2);
574
+
575
+ if (cached !== undefined) {
576
+ return cached;
577
+ }
578
+
579
+ const overlappingFields = [];
580
+
581
+ for (const [responseName, fields1] of collectFieldsResult1.groupedFieldSet) {
582
+ // A field map can participate in many distinct comparisons.
583
+ spendComplexityBudget(ruleContext, 1);
584
+ const fields2 = collectFieldsResult2.groupedFieldSet.get(responseName);
585
+
586
+ if (fields2 !== undefined) {
587
+ overlappingFields.push({
588
+ responseName,
589
+ fields1,
590
+ fields2,
591
+ });
592
+ }
593
+ }
594
+
595
+ byCollectFieldsResult.set(collectFieldsResult2, overlappingFields);
596
+ return overlappingFields;
563
597
  } // Determines if there is a conflict between two particular fields, including
564
598
  // comparing their sub-fields.
565
599
 
566
600
  function findConflict(
567
- context,
568
- cachedFieldsAndFragmentNames,
569
- comparedFieldsAndFragmentPairs,
570
- comparedFragmentPairs,
601
+ validationContext,
602
+ ruleContext,
571
603
  parentFieldsAreMutuallyExclusive,
572
604
  responseName,
573
- field1,
574
- field2,
605
+ fieldDetails1,
606
+ fieldDetails2,
575
607
  ) {
576
- const [parentType1, node1, def1] = field1;
577
- const [parentType2, node2, def2] = field2; // If it is known that two fields could not possibly apply at the same
608
+ const {
609
+ parentType: parentType1,
610
+ node: node1,
611
+ definition: def1,
612
+ } = fieldDetails1;
613
+ const {
614
+ parentType: parentType2,
615
+ node: node2,
616
+ definition: def2,
617
+ } = fieldDetails2; // If it is known that two fields could not possibly apply at the same
578
618
  // time, due to the parent types, then it is safe to permit them to diverge
579
619
  // in aliased field or arguments used as they will not present any ambiguity
580
620
  // by differing.
@@ -587,44 +627,78 @@ function findConflict(
587
627
  parentFieldsAreMutuallyExclusive ||
588
628
  (parentType1 !== parentType2 &&
589
629
  (0, _definition.isObjectType)(parentType1) &&
590
- (0, _definition.isObjectType)(parentType2));
630
+ (0, _definition.isObjectType)(parentType2)); // Count attempted field pairs, including pairs skipped by type exclusivity.
631
+
632
+ spendComplexityBudget(ruleContext, 2);
633
+
634
+ if (
635
+ fieldDetails1.isSimpleLeaf &&
636
+ fieldDetails2.isSimpleLeaf &&
637
+ (def1 === null || def1 === void 0 ? void 0 : def1.type) ===
638
+ (def2 === null || def2 === void 0 ? void 0 : def2.type) &&
639
+ node1.name.value === node2.name.value
640
+ ) {
641
+ return;
642
+ }
591
643
 
592
644
  if (!areMutuallyExclusive) {
593
- // Two aliases must refer to the same field.
594
- const name1 = node1.name.value;
595
- const name2 = node2.name.value;
596
-
597
- if (name1 !== name2) {
598
- return [
599
- [responseName, `"${name1}" and "${name2}" are different fields`],
600
- [node1],
601
- [node2],
602
- ];
603
- } // Two field calls must have the same arguments.
604
-
605
- if (!sameArguments(node1, node2)) {
606
- return [
607
- [responseName, 'they have differing arguments'],
608
- [node1],
609
- [node2],
610
- ];
645
+ const fieldInfo1 = getFieldInfo(fieldDetails1);
646
+ const fieldInfo2 = getFieldInfo(fieldDetails2);
647
+ let reason = fieldInfo1.comparisons.get(fieldInfo2);
648
+
649
+ if (reason === undefined) {
650
+ // Two aliases must refer to the same field with the same arguments.
651
+ const name1 = node1.name.value;
652
+ const name2 = node2.name.value;
653
+ reason =
654
+ name1 !== name2
655
+ ? `"${maybeAbbreviate(name1)}" and "${maybeAbbreviate(
656
+ name2,
657
+ )}" are different fields`
658
+ : sameArguments(
659
+ ruleContext,
660
+ fieldInfo1.arguments,
661
+ fieldInfo2.arguments,
662
+ )
663
+ ? null
664
+ : 'they have differing arguments';
665
+ fieldInfo1.comparisons.set(fieldInfo2, reason);
666
+ }
667
+
668
+ if (reason !== null) {
669
+ return {
670
+ responseName,
671
+ reason,
672
+ fields1: [node1],
673
+ fields2: [node2],
674
+ };
611
675
  }
612
676
  } // The return type for each field.
613
677
 
614
678
  const type1 = def1 === null || def1 === void 0 ? void 0 : def1.type;
615
679
  const type2 = def2 === null || def2 === void 0 ? void 0 : def2.type;
616
680
 
617
- if (type1 && type2 && doTypesConflict(type1, type2)) {
618
- return [
619
- [
620
- responseName,
621
- `they return conflicting types "${(0, _inspect.inspect)(
622
- type1,
623
- )}" and "${(0, _inspect.inspect)(type2)}"`,
624
- ],
625
- [node1],
626
- [node2],
627
- ];
681
+ if (type1 && type2 && doTypesConflict(ruleContext, type1, type2)) {
682
+ var _fieldInfo1$typeStrin, _fieldInfo2$typeStrin;
683
+
684
+ const fieldInfo1 = getFieldInfo(fieldDetails1);
685
+ const fieldInfo2 = getFieldInfo(fieldDetails2);
686
+ const type1String =
687
+ (_fieldInfo1$typeStrin = fieldInfo1.typeString) !== null &&
688
+ _fieldInfo1$typeStrin !== void 0
689
+ ? _fieldInfo1$typeStrin
690
+ : (fieldInfo1.typeString = (0, _inspect.inspect)(type1));
691
+ const type2String =
692
+ (_fieldInfo2$typeStrin = fieldInfo2.typeString) !== null &&
693
+ _fieldInfo2$typeStrin !== void 0
694
+ ? _fieldInfo2$typeStrin
695
+ : (fieldInfo2.typeString = (0, _inspect.inspect)(type2));
696
+ return {
697
+ responseName,
698
+ reason: `they return conflicting types "${type1String}" and "${type2String}"`,
699
+ fields1: [node1],
700
+ fields2: [node2],
701
+ };
628
702
  } // Collect and compare sub-fields. Use the same "visited fragment names" list
629
703
  // for both collections so fields in a fragment reference are never
630
704
  // compared to themselves.
@@ -634,62 +708,144 @@ function findConflict(
634
708
 
635
709
  if (selectionSet1 && selectionSet2) {
636
710
  const conflicts = findConflictsBetweenSubSelectionSets(
637
- context,
638
- cachedFieldsAndFragmentNames,
639
- comparedFieldsAndFragmentPairs,
640
- comparedFragmentPairs,
711
+ validationContext,
712
+ ruleContext,
641
713
  areMutuallyExclusive,
642
- (0, _definition.getNamedType)(type1),
714
+ getFieldInfo(fieldDetails1).namedType,
643
715
  selectionSet1,
644
- (0, _definition.getNamedType)(type2),
716
+ getFieldInfo(fieldDetails2).namedType,
645
717
  selectionSet2,
646
718
  );
647
719
  return subfieldConflicts(conflicts, responseName, node1, node2);
648
720
  }
721
+ } // Prepare argument lookups only when a field overlaps.
722
+
723
+ function getFieldInfo(fieldDetails) {
724
+ if (fieldDetails.info === undefined) {
725
+ var _fieldDetails$definit;
726
+
727
+ fieldDetails.info = {
728
+ comparisons: new Map(),
729
+ namedType: (0, _definition.getNamedType)(
730
+ (_fieldDetails$definit = fieldDetails.definition) === null ||
731
+ _fieldDetails$definit === void 0
732
+ ? void 0
733
+ : _fieldDetails$definit.type,
734
+ ),
735
+ arguments: getArgumentInfo(fieldDetails.node.arguments),
736
+ };
737
+ }
738
+
739
+ return fieldDetails.info;
649
740
  }
650
741
 
651
- function sameArguments(node1, node2) {
652
- const args1 = node1.arguments;
653
- const args2 = node2.arguments;
742
+ function getArgumentInfo(args) {
743
+ if (args === undefined || args.length === 0) {
744
+ return;
745
+ }
654
746
 
655
- if (args1 === undefined || args1.length === 0) {
656
- return args2 === undefined || args2.length === 0;
747
+ const values = new Map();
748
+
749
+ for (const { name, value } of args) {
750
+ values.set(name.value, value);
657
751
  }
658
752
 
659
- if (args2 === undefined || args2.length === 0) {
660
- return false;
753
+ return {
754
+ nodes: args,
755
+ values,
756
+ };
757
+ }
758
+
759
+ function sameArguments(ruleContext, argumentInfo1, argumentInfo2) {
760
+ if (argumentInfo1 === undefined) {
761
+ return argumentInfo2 === undefined;
661
762
  }
662
- /* c8 ignore next */
663
763
 
664
- if (args1.length !== args2.length) {
665
- /* c8 ignore next */
764
+ if (
765
+ argumentInfo1.nodes.length !==
766
+ (argumentInfo2 === null || argumentInfo2 === void 0
767
+ ? void 0
768
+ : argumentInfo2.nodes.length)
769
+ ) {
666
770
  return false;
667
- /* c8 ignore next */
668
771
  }
669
772
 
670
- const values2 = new Map(args2.map(({ name, value }) => [name.value, value]));
671
- return args1.every((arg1) => {
672
- const value1 = arg1.value;
773
+ const values2 = argumentInfo2.values;
774
+ return argumentInfo1.nodes.every((arg1) => {
775
+ // A prepared argument list can participate in many field-pair comparisons.
776
+ spendComplexityBudget(ruleContext, 1);
673
777
  const value2 = values2.get(arg1.name.value);
674
778
 
675
779
  if (value2 === undefined) {
676
780
  return false;
677
781
  }
678
782
 
679
- return stringifyValue(value1) === stringifyValue(value2);
783
+ return (
784
+ stringifyValue(ruleContext, arg1.value) ===
785
+ stringifyValue(ruleContext, value2)
786
+ );
680
787
  });
681
788
  }
682
789
 
683
- function stringifyValue(value) {
684
- return (0, _printer.print)((0, _sortValueNode.sortValueNode)(value));
790
+ function stringifyValue(ruleContext, value) {
791
+ const cached = ruleContext.stringifiedValues.get(value);
792
+
793
+ if (cached !== undefined) {
794
+ return cached;
795
+ }
796
+
797
+ const result = JSON.stringify(value, (_key, node) => {
798
+ if (!(0, _ast.isNode)(node)) {
799
+ return node;
800
+ }
801
+
802
+ switch (node.kind) {
803
+ case _kinds.Kind.VARIABLE:
804
+ return [node.kind, node.name.value];
805
+
806
+ case _kinds.Kind.OBJECT:
807
+ return [
808
+ node.kind,
809
+ node.fields
810
+ .slice()
811
+ .sort((first, second) =>
812
+ first.name.value < second.name.value
813
+ ? -1
814
+ : first.name.value > second.name.value
815
+ ? 1
816
+ : 0,
817
+ ),
818
+ ];
819
+
820
+ case _kinds.Kind.OBJECT_FIELD:
821
+ return [node.name.value, node.value];
822
+
823
+ case _kinds.Kind.LIST:
824
+ return [node.kind, node.values];
825
+
826
+ case _kinds.Kind.STRING:
827
+ return [node.kind, node.value, node.block === true];
828
+
829
+ default:
830
+ return [node.kind, 'value' in node ? node.value : null];
831
+ }
832
+ });
833
+ ruleContext.stringifiedValues.set(value, result);
834
+ return result;
685
835
  } // Two types conflict if both types could not apply to a value simultaneously.
686
836
  // Composite types are ignored as their individual field types will be compared
687
837
  // later recursively. However List and Non-Null types must match.
688
838
 
689
- function doTypesConflict(type1, type2) {
839
+ function doTypesConflict(ruleContext, type1, type2) {
840
+ if (type1 === type2) {
841
+ return false;
842
+ }
843
+
844
+ spendComplexityBudget(ruleContext, 1);
845
+
690
846
  if ((0, _definition.isListType)(type1)) {
691
847
  return (0, _definition.isListType)(type2)
692
- ? doTypesConflict(type1.ofType, type2.ofType)
848
+ ? doTypesConflict(ruleContext, type1.ofType, type2.ofType)
693
849
  : true;
694
850
  }
695
851
 
@@ -699,7 +855,7 @@ function doTypesConflict(type1, type2) {
699
855
 
700
856
  if ((0, _definition.isNonNullType)(type1)) {
701
857
  return (0, _definition.isNonNullType)(type2)
702
- ? doTypesConflict(type1.ofType, type2.ofType)
858
+ ? doTypesConflict(ruleContext, type1.ofType, type2.ofType)
703
859
  : true;
704
860
  }
705
861
 
@@ -719,69 +875,71 @@ function doTypesConflict(type1, type2) {
719
875
  // name to field nodes and definitions) as well as a list of fragment names
720
876
  // referenced via fragment spreads.
721
877
 
722
- function getFieldsAndFragmentNames(
723
- context,
724
- cachedFieldsAndFragmentNames,
878
+ function collectFields(
879
+ validationContext,
880
+ ruleContext,
725
881
  parentType,
726
882
  selectionSet,
727
883
  ) {
728
- const cached = cachedFieldsAndFragmentNames.get(selectionSet);
884
+ const { cachedCollectFieldsResults } = ruleContext; // A programmatically built document can share a selection-set object between
885
+ // different parent types; their field definitions must not share a cache entry.
886
+
887
+ let bySelectionSet = cachedCollectFieldsResults.get(parentType);
888
+
889
+ if (bySelectionSet === undefined) {
890
+ bySelectionSet = new Map();
891
+ cachedCollectFieldsResults.set(parentType, bySelectionSet);
892
+ }
893
+
894
+ const cached = bySelectionSet.get(selectionSet);
729
895
 
730
896
  if (cached) {
731
897
  return cached;
732
898
  }
733
899
 
734
- const nodeAndDefs = Object.create(null);
735
- const fragmentNames = Object.create(null);
736
-
737
- _collectFieldsAndFragmentNames(
738
- context,
900
+ const fieldDetails = new Map();
901
+ const fragmentNames = new Set();
902
+ collectFieldsImpl(
903
+ validationContext,
739
904
  parentType,
740
905
  selectionSet,
741
- nodeAndDefs,
906
+ fieldDetails,
742
907
  fragmentNames,
743
908
  );
744
-
745
- const result = [nodeAndDefs, Object.keys(fragmentNames)];
746
- cachedFieldsAndFragmentNames.set(selectionSet, result);
909
+ const result = {
910
+ groupedFieldSet: fieldDetails,
911
+ fragmentNames: [...fragmentNames],
912
+ };
913
+ bySelectionSet.set(selectionSet, result);
747
914
  return result;
748
915
  } // Given a reference to a fragment, return the represented collection of fields
749
916
  // as well as a list of nested fragment names referenced via fragment spreads.
750
917
 
751
- function getReferencedFieldsAndFragmentNames(
752
- context,
753
- cachedFieldsAndFragmentNames,
754
- fragment,
755
- ) {
756
- // Short-circuit building a type from the node if possible.
757
- const cached = cachedFieldsAndFragmentNames.get(fragment.selectionSet);
758
-
759
- if (cached) {
760
- return cached;
761
- }
762
-
918
+ function collectFragmentFields(validationContext, ruleContext, fragment) {
763
919
  const fragmentType = (0, _typeFromAST.typeFromAST)(
764
- context.getSchema(),
920
+ validationContext.getSchema(),
765
921
  fragment.typeCondition,
766
922
  );
767
- return getFieldsAndFragmentNames(
768
- context,
769
- cachedFieldsAndFragmentNames,
923
+ return collectFields(
924
+ validationContext,
925
+ ruleContext,
770
926
  fragmentType,
771
927
  fragment.selectionSet,
772
928
  );
773
929
  }
774
930
 
775
- function _collectFieldsAndFragmentNames(
776
- context,
931
+ function collectFieldsImpl(
932
+ validationContext,
777
933
  parentType,
778
934
  selectionSet,
779
- nodeAndDefs,
935
+ fieldDetails,
780
936
  fragmentNames,
781
937
  ) {
782
938
  for (const selection of selectionSet.selections) {
783
939
  switch (selection.kind) {
784
940
  case _kinds.Kind.FIELD: {
941
+ var _selection$arguments$, _selection$arguments;
942
+
785
943
  const fieldName = selection.name.value;
786
944
  let fieldDef;
787
945
 
@@ -795,33 +953,49 @@ function _collectFieldsAndFragmentNames(
795
953
  const responseName = selection.alias
796
954
  ? selection.alias.value
797
955
  : fieldName;
956
+ let fieldDetailsList = fieldDetails.get(responseName);
798
957
 
799
- if (!nodeAndDefs[responseName]) {
800
- nodeAndDefs[responseName] = [];
958
+ if (fieldDetailsList === undefined) {
959
+ fieldDetailsList = [];
960
+ fieldDetails.set(responseName, fieldDetailsList);
801
961
  }
802
962
 
803
- nodeAndDefs[responseName].push([parentType, selection, fieldDef]);
963
+ fieldDetailsList.push({
964
+ parentType,
965
+ node: selection,
966
+ definition: fieldDef,
967
+ isSimpleLeaf:
968
+ ((_selection$arguments$ =
969
+ (_selection$arguments = selection.arguments) === null ||
970
+ _selection$arguments === void 0
971
+ ? void 0
972
+ : _selection$arguments.length) !== null &&
973
+ _selection$arguments$ !== void 0
974
+ ? _selection$arguments$
975
+ : 0) === 0 && !selection.selectionSet,
976
+ });
804
977
  break;
805
978
  }
806
979
 
807
980
  case _kinds.Kind.FRAGMENT_SPREAD:
808
- fragmentNames[selection.name.value] = true;
981
+ fragmentNames.add(selection.name.value);
809
982
  break;
810
983
 
811
984
  case _kinds.Kind.INLINE_FRAGMENT: {
812
985
  const typeCondition = selection.typeCondition;
813
986
  const inlineFragmentType = typeCondition
814
- ? (0, _typeFromAST.typeFromAST)(context.getSchema(), typeCondition)
987
+ ? (0, _typeFromAST.typeFromAST)(
988
+ validationContext.getSchema(),
989
+ typeCondition,
990
+ )
815
991
  : parentType;
816
-
817
- _collectFieldsAndFragmentNames(
818
- context,
992
+ collectFieldsImpl(
993
+ validationContext,
819
994
  inlineFragmentType,
820
995
  selection.selectionSet,
821
- nodeAndDefs,
996
+ fieldDetails,
822
997
  fragmentNames,
823
998
  );
824
-
825
999
  break;
826
1000
  }
827
1001
  }
@@ -831,29 +1005,79 @@ function _collectFieldsAndFragmentNames(
831
1005
 
832
1006
  function subfieldConflicts(conflicts, responseName, node1, node2) {
833
1007
  if (conflicts.length > 0) {
834
- return [
835
- [responseName, conflicts.map(([reason]) => reason)],
836
- [node1, ...conflicts.map(([, fields1]) => fields1).flat()],
837
- [node2, ...conflicts.map(([, , fields2]) => fields2).flat()],
838
- ];
1008
+ let message = '';
1009
+
1010
+ for (const { responseName: name, reason } of conflicts) {
1011
+ message +=
1012
+ (message === '' ? '' : ' and ') +
1013
+ `subfields "${maybeAbbreviate(name)}" conflict because ${reason}`;
1014
+
1015
+ if (message.length > MAX_CONFLICT_DETAIL_LENGTH) {
1016
+ break;
1017
+ }
1018
+ }
1019
+
1020
+ return {
1021
+ responseName,
1022
+ reason: maybeAbbreviate(message),
1023
+ fields1: collectConflictNodes(node1, conflicts, 'fields1'),
1024
+ fields2: collectConflictNodes(node2, conflicts, 'fields2'),
1025
+ };
1026
+ }
1027
+ }
1028
+
1029
+ function collectConflictNodes(node, conflicts, side) {
1030
+ const nodes = [node];
1031
+
1032
+ for (const conflict of conflicts) {
1033
+ const remaining = MAX_CONFLICT_NODES - nodes.length; // Locations start with the enclosing field. Take from the end so that
1034
+ // truncation favors the deeper fields involved in the conflict.
1035
+
1036
+ const retainedNodes = conflict[side].slice(-remaining);
1037
+ nodes.push(...retainedNodes);
1038
+
1039
+ if (nodes.length === MAX_CONFLICT_NODES) {
1040
+ break;
1041
+ }
1042
+ }
1043
+
1044
+ return nodes;
1045
+ }
1046
+
1047
+ function maybeAbbreviate(text) {
1048
+ return text.length > MAX_CONFLICT_DETAIL_LENGTH
1049
+ ? text.slice(0, MAX_CONFLICT_DETAIL_LENGTH) + '...'
1050
+ : text;
1051
+ }
1052
+
1053
+ function spendComplexityBudget(ruleContext, cost) {
1054
+ ruleContext.remainingComplexityBudget -= cost;
1055
+ /* c8 ignore next 3 */
1056
+
1057
+ if (ruleContext.remainingComplexityBudget < 0) {
1058
+ throw _validationAbortedError.validationAbortedError;
839
1059
  }
840
1060
  }
841
1061
  /**
842
- * A way to keep track of pairs of things where the ordering of the pair
843
- * matters.
1062
+ * A way to keep track of pairs of similar things when the ordering of the pair
1063
+ * does not matter.
844
1064
  *
845
- * Provides a third argument for has/set to allow flagging the pair as
1065
+ * Provides a third argument for has/add to allow flagging the pair as
846
1066
  * weakly or strongly present within the collection.
847
1067
  *
848
1068
  * @internal
849
1069
  */
850
1070
 
851
- class OrderedPairSet {
1071
+ class PairSet {
852
1072
  constructor() {
853
1073
  this._data = new Map();
854
1074
  }
855
1075
 
856
1076
  has(a, b, weaklyPresent) {
1077
+ return this._has(a, b, weaklyPresent) || this._has(b, a, weaklyPresent);
1078
+ }
1079
+
1080
+ _has(a, b, weaklyPresent) {
857
1081
  var _this$_data$get;
858
1082
 
859
1083
  const result =
@@ -879,29 +1103,3 @@ class OrderedPairSet {
879
1103
  }
880
1104
  }
881
1105
  }
882
- /**
883
- * A way to keep track of pairs of similar things when the ordering of the pair
884
- * does not matter.
885
- *
886
- * @internal
887
- */
888
-
889
- class PairSet {
890
- constructor() {
891
- this._orderedPairSet = new OrderedPairSet();
892
- }
893
-
894
- has(a, b, weaklyPresent) {
895
- return a < b
896
- ? this._orderedPairSet.has(a, b, weaklyPresent)
897
- : this._orderedPairSet.has(b, a, weaklyPresent);
898
- }
899
-
900
- add(a, b, weaklyPresent) {
901
- if (a < b) {
902
- this._orderedPairSet.add(a, b, weaklyPresent);
903
- } else {
904
- this._orderedPairSet.add(b, a, weaklyPresent);
905
- }
906
- }
907
- }