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