@apollo/federation-internals 2.4.3 → 2.4.4

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 (123) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/package.json +1 -1
  3. package/dist/Subgraph.d.ts +0 -1
  4. package/dist/Subgraph.d.ts.map +0 -1
  5. package/dist/Subgraph.js +0 -2
  6. package/dist/Subgraph.js.map +0 -1
  7. package/dist/argumentCompositionStrategies.d.ts +0 -34
  8. package/dist/argumentCompositionStrategies.d.ts.map +0 -1
  9. package/dist/argumentCompositionStrategies.js +0 -35
  10. package/dist/argumentCompositionStrategies.js.map +0 -1
  11. package/dist/buildSchema.d.ts +0 -10
  12. package/dist/buildSchema.d.ts.map +0 -1
  13. package/dist/buildSchema.js +0 -362
  14. package/dist/buildSchema.js.map +0 -1
  15. package/dist/coreSpec.d.ts +0 -127
  16. package/dist/coreSpec.d.ts.map +0 -1
  17. package/dist/coreSpec.js +0 -590
  18. package/dist/coreSpec.js.map +0 -1
  19. package/dist/debug.d.ts +0 -15
  20. package/dist/debug.d.ts.map +0 -1
  21. package/dist/debug.js +0 -122
  22. package/dist/debug.js.map +0 -1
  23. package/dist/definitions.d.ts +0 -663
  24. package/dist/definitions.d.ts.map +0 -1
  25. package/dist/definitions.js +0 -2841
  26. package/dist/definitions.js.map +0 -1
  27. package/dist/directiveAndTypeSpecification.d.ts +0 -67
  28. package/dist/directiveAndTypeSpecification.d.ts.map +0 -1
  29. package/dist/directiveAndTypeSpecification.js +0 -271
  30. package/dist/directiveAndTypeSpecification.js.map +0 -1
  31. package/dist/error.d.ts +0 -128
  32. package/dist/error.d.ts.map +0 -1
  33. package/dist/error.js +0 -315
  34. package/dist/error.js.map +0 -1
  35. package/dist/extractSubgraphsFromSupergraph.d.ts +0 -8
  36. package/dist/extractSubgraphsFromSupergraph.d.ts.map +0 -1
  37. package/dist/extractSubgraphsFromSupergraph.js +0 -576
  38. package/dist/extractSubgraphsFromSupergraph.js.map +0 -1
  39. package/dist/federation.d.ts +0 -175
  40. package/dist/federation.d.ts.map +0 -1
  41. package/dist/federation.js +0 -1414
  42. package/dist/federation.js.map +0 -1
  43. package/dist/federationSpec.d.ts +0 -25
  44. package/dist/federationSpec.d.ts.map +0 -1
  45. package/dist/federationSpec.js +0 -125
  46. package/dist/federationSpec.js.map +0 -1
  47. package/dist/genErrorCodeDoc.d.ts +0 -2
  48. package/dist/genErrorCodeDoc.d.ts.map +0 -1
  49. package/dist/genErrorCodeDoc.js +0 -61
  50. package/dist/genErrorCodeDoc.js.map +0 -1
  51. package/dist/graphQLJSSchemaToAST.d.ts +0 -8
  52. package/dist/graphQLJSSchemaToAST.d.ts.map +0 -1
  53. package/dist/graphQLJSSchemaToAST.js +0 -96
  54. package/dist/graphQLJSSchemaToAST.js.map +0 -1
  55. package/dist/inaccessibleSpec.d.ts +0 -18
  56. package/dist/inaccessibleSpec.d.ts.map +0 -1
  57. package/dist/inaccessibleSpec.js +0 -655
  58. package/dist/inaccessibleSpec.js.map +0 -1
  59. package/dist/index.d.ts +0 -24
  60. package/dist/index.d.ts.map +0 -1
  61. package/dist/index.js +0 -42
  62. package/dist/index.js.map +0 -1
  63. package/dist/introspection.d.ts +0 -6
  64. package/dist/introspection.d.ts.map +0 -1
  65. package/dist/introspection.js +0 -96
  66. package/dist/introspection.js.map +0 -1
  67. package/dist/joinSpec.d.ts +0 -51
  68. package/dist/joinSpec.d.ts.map +0 -1
  69. package/dist/joinSpec.js +0 -160
  70. package/dist/joinSpec.js.map +0 -1
  71. package/dist/knownCoreFeatures.d.ts +0 -5
  72. package/dist/knownCoreFeatures.d.ts.map +0 -1
  73. package/dist/knownCoreFeatures.js +0 -20
  74. package/dist/knownCoreFeatures.js.map +0 -1
  75. package/dist/operations.d.ts +0 -415
  76. package/dist/operations.d.ts.map +0 -1
  77. package/dist/operations.js +0 -2040
  78. package/dist/operations.js.map +0 -1
  79. package/dist/precompute.d.ts +0 -3
  80. package/dist/precompute.d.ts.map +0 -1
  81. package/dist/precompute.js +0 -54
  82. package/dist/precompute.js.map +0 -1
  83. package/dist/print.d.ts +0 -28
  84. package/dist/print.d.ts.map +0 -1
  85. package/dist/print.js +0 -299
  86. package/dist/print.js.map +0 -1
  87. package/dist/schemaUpgrader.d.ts +0 -121
  88. package/dist/schemaUpgrader.d.ts.map +0 -1
  89. package/dist/schemaUpgrader.js +0 -570
  90. package/dist/schemaUpgrader.js.map +0 -1
  91. package/dist/suggestions.d.ts +0 -3
  92. package/dist/suggestions.d.ts.map +0 -1
  93. package/dist/suggestions.js +0 -44
  94. package/dist/suggestions.js.map +0 -1
  95. package/dist/supergraphs.d.ts +0 -10
  96. package/dist/supergraphs.d.ts.map +0 -1
  97. package/dist/supergraphs.js +0 -76
  98. package/dist/supergraphs.js.map +0 -1
  99. package/dist/tagSpec.d.ts +0 -19
  100. package/dist/tagSpec.d.ts.map +0 -1
  101. package/dist/tagSpec.js +0 -66
  102. package/dist/tagSpec.js.map +0 -1
  103. package/dist/types.d.ts +0 -9
  104. package/dist/types.d.ts.map +0 -1
  105. package/dist/types.js +0 -64
  106. package/dist/types.js.map +0 -1
  107. package/dist/utils.d.ts +0 -64
  108. package/dist/utils.d.ts.map +0 -1
  109. package/dist/utils.js +0 -326
  110. package/dist/utils.js.map +0 -1
  111. package/dist/validate.d.ts +0 -4
  112. package/dist/validate.d.ts.map +0 -1
  113. package/dist/validate.js +0 -239
  114. package/dist/validate.js.map +0 -1
  115. package/dist/validation/KnownTypeNamesInFederationRule.d.ts +0 -4
  116. package/dist/validation/KnownTypeNamesInFederationRule.d.ts.map +0 -1
  117. package/dist/validation/KnownTypeNamesInFederationRule.js +0 -41
  118. package/dist/validation/KnownTypeNamesInFederationRule.js.map +0 -1
  119. package/dist/values.d.ts +0 -23
  120. package/dist/values.d.ts.map +0 -1
  121. package/dist/values.js +0 -580
  122. package/dist/values.js.map +0 -1
  123. package/tsconfig.tsbuildinfo +0 -1
@@ -1,2040 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.operationToDocument = exports.parseSelectionSet = exports.parseOperation = exports.operationFromDocument = exports.FragmentSelection = exports.FieldSelection = exports.selectionOfElement = exports.selectionSetOfElement = exports.selectionSetOf = exports.allFieldDefinitionsInSelectionSet = exports.MutableSelectionSet = exports.SelectionSetUpdates = exports.SelectionSet = exports.NamedFragments = exports.NamedFragmentDefinition = exports.Operation = exports.concatOperationPaths = exports.conditionalDirectivesInOperationPath = exports.sameOperationPaths = exports.operationPathToStringPath = exports.FragmentElement = exports.Field = void 0;
4
- const graphql_1 = require("graphql");
5
- const definitions_1 = require("./definitions");
6
- const federation_1 = require("./federation");
7
- const error_1 = require("./error");
8
- const types_1 = require("./types");
9
- const utils_1 = require("./utils");
10
- const values_1 = require("./values");
11
- const uuid_1 = require("uuid");
12
- function validate(condition, message, sourceAST) {
13
- if (!condition) {
14
- throw error_1.ERRORS.INVALID_GRAPHQL.err(message(), { nodes: sourceAST });
15
- }
16
- }
17
- function haveSameDirectives(op1, op2) {
18
- return (0, definitions_1.sameDirectiveApplications)(op1.appliedDirectives, op2.appliedDirectives);
19
- }
20
- class AbstractOperationElement extends definitions_1.DirectiveTargetElement {
21
- constructor(schema, directives) {
22
- super(schema, directives);
23
- }
24
- collectVariables(collector) {
25
- this.collectVariablesInElement(collector);
26
- this.collectVariablesInAppliedDirectives(collector);
27
- }
28
- addAttachement(key, value) {
29
- if (!this.attachements) {
30
- this.attachements = new Map();
31
- }
32
- this.attachements.set(key, value);
33
- }
34
- getAttachement(key) {
35
- var _a;
36
- return (_a = this.attachements) === null || _a === void 0 ? void 0 : _a.get(key);
37
- }
38
- copyAttachementsTo(elt) {
39
- if (this.attachements) {
40
- for (const [k, v] of this.attachements.entries()) {
41
- elt.addAttachement(k, v);
42
- }
43
- }
44
- }
45
- }
46
- class Field extends AbstractOperationElement {
47
- constructor(definition, args, directives, alias) {
48
- super(definition.schema(), directives);
49
- this.definition = definition;
50
- this.args = args;
51
- this.alias = alias;
52
- this.kind = 'Field';
53
- }
54
- collectVariablesInElement(collector) {
55
- if (this.args) {
56
- collector.collectInArguments(this.args);
57
- }
58
- }
59
- get name() {
60
- return this.definition.name;
61
- }
62
- argumentValue(name) {
63
- return this.args ? this.args[name] : undefined;
64
- }
65
- responseName() {
66
- return this.alias ? this.alias : this.name;
67
- }
68
- key() {
69
- return this.responseName();
70
- }
71
- asPathElement() {
72
- return this.responseName();
73
- }
74
- get parentType() {
75
- return this.definition.parent;
76
- }
77
- isLeafField() {
78
- return (0, definitions_1.isLeafType)((0, definitions_1.baseType)(this.definition.type));
79
- }
80
- withUpdatedDefinition(newDefinition) {
81
- const newField = new Field(newDefinition, this.args, this.appliedDirectives, this.alias);
82
- this.copyAttachementsTo(newField);
83
- return newField;
84
- }
85
- withUpdatedAlias(newAlias) {
86
- const newField = new Field(this.definition, this.args, this.appliedDirectives, newAlias);
87
- this.copyAttachementsTo(newField);
88
- return newField;
89
- }
90
- withUpdatedDirectives(newDirectives) {
91
- const newField = new Field(this.definition, this.args, newDirectives, this.alias);
92
- this.copyAttachementsTo(newField);
93
- return newField;
94
- }
95
- argumentsToNodes() {
96
- if (!this.args) {
97
- return undefined;
98
- }
99
- const entries = Object.entries(this.args);
100
- if (entries.length === 0) {
101
- return undefined;
102
- }
103
- return entries.map(([n, v]) => {
104
- return {
105
- kind: graphql_1.Kind.ARGUMENT,
106
- name: { kind: graphql_1.Kind.NAME, value: n },
107
- value: (0, values_1.valueToAST)(v, this.definition.argument(n).type),
108
- };
109
- });
110
- }
111
- appliesTo(type) {
112
- const definition = type.field(this.name);
113
- return !!definition && this.selects(definition);
114
- }
115
- selects(definition, assumeValid = false, variableDefinitions) {
116
- (0, utils_1.assert)(assumeValid || variableDefinitions, 'Must provide variable definitions if validation is needed');
117
- if (definition === this.definition) {
118
- return true;
119
- }
120
- if (this.name !== definition.name) {
121
- return false;
122
- }
123
- for (const argDef of definition.arguments()) {
124
- const appliedValue = this.argumentValue(argDef.name);
125
- if (appliedValue === undefined) {
126
- if (argDef.defaultValue === undefined && !(0, definitions_1.isNullableType)(argDef.type)) {
127
- return false;
128
- }
129
- }
130
- else {
131
- if (!assumeValid && !(0, values_1.isValidValue)(appliedValue, argDef, variableDefinitions)) {
132
- return false;
133
- }
134
- }
135
- }
136
- if (!assumeValid && this.args) {
137
- for (const [name, value] of Object.entries(this.args)) {
138
- if (value !== null && definition.argument(name) === undefined) {
139
- return false;
140
- }
141
- }
142
- }
143
- return true;
144
- }
145
- validate(variableDefinitions) {
146
- validate(this.name === this.definition.name, () => `Field name "${this.name}" cannot select field "${this.definition.coordinate}: name mismatch"`);
147
- for (const argDef of this.definition.arguments()) {
148
- const appliedValue = this.argumentValue(argDef.name);
149
- if (appliedValue === undefined) {
150
- validate(argDef.defaultValue !== undefined || (0, definitions_1.isNullableType)(argDef.type), () => `Missing mandatory value for argument "${argDef.name}" of field "${this.definition.coordinate}" in selection "${this}"`);
151
- }
152
- else {
153
- validate((0, values_1.isValidValue)(appliedValue, argDef, variableDefinitions), () => `Invalid value ${(0, values_1.valueToString)(appliedValue)} for argument "${argDef.coordinate}" of type ${argDef.type}`);
154
- }
155
- }
156
- if (this.args) {
157
- for (const [name, value] of Object.entries(this.args)) {
158
- validate(value === null || this.definition.argument(name) !== undefined, () => `Unknown argument "${name}" in field application of "${this.name}"`);
159
- }
160
- }
161
- }
162
- rebaseOn(parentType) {
163
- const fieldParent = this.definition.parent;
164
- if (parentType === fieldParent) {
165
- return this;
166
- }
167
- if (this.name === definitions_1.typenameFieldName) {
168
- return this.withUpdatedDefinition(parentType.typenameField());
169
- }
170
- validate(this.canRebaseOn(parentType), () => `Cannot add selection of field "${this.definition.coordinate}" to selection set of parent type "${parentType}"`);
171
- const fieldDef = parentType.field(this.name);
172
- validate(fieldDef, () => `Cannot add selection of field "${this.definition.coordinate}" to selection set of parent type "${parentType}" (that does not declare that field)`);
173
- return this.withUpdatedDefinition(fieldDef);
174
- }
175
- canRebaseOn(parentType) {
176
- const fieldParentType = this.definition.parent;
177
- return parentType.name === fieldParentType.name
178
- || (0, definitions_1.isInterfaceType)(fieldParentType)
179
- || (0, federation_1.isInterfaceObjectType)(fieldParentType);
180
- }
181
- typeIfAddedTo(parentType) {
182
- var _a, _b;
183
- const fieldParentType = this.definition.parent;
184
- if (parentType == fieldParentType) {
185
- return this.definition.type;
186
- }
187
- if (this.name === definitions_1.typenameFieldName) {
188
- return (_a = parentType.typenameField()) === null || _a === void 0 ? void 0 : _a.type;
189
- }
190
- return this.canRebaseOn(parentType)
191
- ? (_b = parentType.field(this.name)) === null || _b === void 0 ? void 0 : _b.type
192
- : undefined;
193
- }
194
- hasDefer() {
195
- return false;
196
- }
197
- deferDirectiveArgs() {
198
- return undefined;
199
- }
200
- withoutDefer() {
201
- return this;
202
- }
203
- equals(that) {
204
- if (this === that) {
205
- return true;
206
- }
207
- return that.kind === 'Field'
208
- && this.name === that.name
209
- && this.alias === that.alias
210
- && (this.args ? that.args && (0, values_1.argumentsEquals)(this.args, that.args) : !that.args)
211
- && haveSameDirectives(this, that);
212
- }
213
- toString() {
214
- const alias = this.alias ? this.alias + ': ' : '';
215
- const entries = this.args ? Object.entries(this.args) : [];
216
- const args = entries.length === 0
217
- ? ''
218
- : '(' + entries.map(([n, v]) => { var _a; return `${n}: ${(0, values_1.valueToString)(v, (_a = this.definition.argument(n)) === null || _a === void 0 ? void 0 : _a.type)}`; }).join(', ') + ')';
219
- return alias + this.name + args + this.appliedDirectivesToString();
220
- }
221
- }
222
- exports.Field = Field;
223
- function keyForDirective(directive, directivesNeverEqualToThemselves = ['defer']) {
224
- if (directivesNeverEqualToThemselves.includes(directive.name)) {
225
- return (0, uuid_1.v1)();
226
- }
227
- const entries = Object.entries(directive.arguments()).filter(([_, v]) => v !== undefined);
228
- entries.sort(([n1], [n2]) => n1.localeCompare(n2));
229
- const args = entries.length == 0 ? '' : '(' + entries.map(([n, v]) => `${n}: ${(0, values_1.valueToString)(v, directive.argumentType(n))}`).join(', ') + ')';
230
- return `@${directive.name}${args}`;
231
- }
232
- class FragmentElement extends AbstractOperationElement {
233
- constructor(sourceType, typeCondition, directives) {
234
- super(sourceType.schema(), directives);
235
- this.sourceType = sourceType;
236
- this.kind = 'FragmentElement';
237
- this.typeCondition = typeCondition !== undefined && typeof typeCondition === 'string'
238
- ? this.schema().type(typeCondition)
239
- : typeCondition;
240
- }
241
- collectVariablesInElement(_) {
242
- }
243
- get parentType() {
244
- return this.sourceType;
245
- }
246
- key() {
247
- if (!this.computedKey) {
248
- const keyForDirectives = this.appliedDirectives.map((d) => keyForDirective(d)).join(' ');
249
- this.computedKey = '...' + (this.typeCondition ? ' on ' + this.typeCondition.name : '') + keyForDirectives;
250
- }
251
- return this.computedKey;
252
- }
253
- castedType() {
254
- return this.typeCondition ? this.typeCondition : this.sourceType;
255
- }
256
- asPathElement() {
257
- const condition = this.typeCondition;
258
- return condition ? `... on ${condition}` : undefined;
259
- }
260
- withUpdatedSourceType(newSourceType) {
261
- return this.withUpdatedTypes(newSourceType, this.typeCondition);
262
- }
263
- withUpdatedCondition(newCondition) {
264
- return this.withUpdatedTypes(this.sourceType, newCondition);
265
- }
266
- withUpdatedTypes(newSourceType, newCondition) {
267
- const newFragment = new FragmentElement(newSourceType, newCondition === null || newCondition === void 0 ? void 0 : newCondition.name, this.appliedDirectives);
268
- this.copyAttachementsTo(newFragment);
269
- return newFragment;
270
- }
271
- withUpdatedDirectives(newDirectives) {
272
- const newFragment = new FragmentElement(this.sourceType, this.typeCondition, newDirectives);
273
- this.copyAttachementsTo(newFragment);
274
- return newFragment;
275
- }
276
- rebaseOn(parentType) {
277
- const fragmentParent = this.parentType;
278
- const typeCondition = this.typeCondition;
279
- if (parentType === fragmentParent) {
280
- return this;
281
- }
282
- const { canRebase, rebasedCondition } = this.canRebaseOn(parentType);
283
- validate(canRebase, () => `Cannot add fragment of condition "${typeCondition}" (runtimes: [${(0, definitions_1.possibleRuntimeTypes)(typeCondition)}]) to parent type "${parentType}" (runtimes: ${(0, definitions_1.possibleRuntimeTypes)(parentType)})`);
284
- return this.withUpdatedTypes(parentType, rebasedCondition);
285
- }
286
- canRebaseOn(parentType) {
287
- if (!this.typeCondition) {
288
- return { canRebase: true, rebasedCondition: undefined };
289
- }
290
- const rebasedCondition = parentType.schema().type(this.typeCondition.name);
291
- if (!rebasedCondition || !(0, definitions_1.isCompositeType)(rebasedCondition) || !(0, definitions_1.runtimeTypesIntersects)(parentType, rebasedCondition)) {
292
- return { canRebase: false };
293
- }
294
- return { canRebase: true, rebasedCondition };
295
- }
296
- castedTypeIfAddedTo(parentType) {
297
- if (parentType == this.parentType) {
298
- return this.castedType();
299
- }
300
- const { canRebase, rebasedCondition } = this.canRebaseOn(parentType);
301
- return canRebase ? (rebasedCondition ? rebasedCondition : parentType) : undefined;
302
- }
303
- hasDefer() {
304
- return this.hasAppliedDirective('defer');
305
- }
306
- hasStream() {
307
- return this.hasAppliedDirective('stream');
308
- }
309
- deferDirectiveArgs() {
310
- var _a;
311
- return (_a = this.appliedDirectivesOf(this.schema().deferDirective())[0]) === null || _a === void 0 ? void 0 : _a.arguments();
312
- }
313
- withoutDefer() {
314
- const deferName = this.schema().deferDirective().name;
315
- const updatedDirectives = this.appliedDirectives.filter((d) => d.name !== deferName);
316
- if (!this.typeCondition && updatedDirectives.length === 0) {
317
- return undefined;
318
- }
319
- if (updatedDirectives.length === this.appliedDirectives.length) {
320
- return this;
321
- }
322
- const updated = new FragmentElement(this.sourceType, this.typeCondition, updatedDirectives);
323
- this.copyAttachementsTo(updated);
324
- return updated;
325
- }
326
- withNormalizedDefer(normalizer) {
327
- const deferArgs = this.deferDirectiveArgs();
328
- if (!deferArgs) {
329
- return this;
330
- }
331
- let newDeferArgs = undefined;
332
- let conditionVariable = undefined;
333
- if (deferArgs.if !== undefined) {
334
- if (typeof deferArgs.if === 'boolean') {
335
- if (deferArgs.if) {
336
- newDeferArgs = {
337
- ...deferArgs,
338
- if: undefined,
339
- };
340
- }
341
- else {
342
- return this.withoutDefer();
343
- }
344
- }
345
- else {
346
- conditionVariable = deferArgs.if;
347
- }
348
- }
349
- let label = deferArgs.label;
350
- if (!label) {
351
- label = normalizer.newLabel();
352
- if (newDeferArgs) {
353
- newDeferArgs.label = label;
354
- }
355
- else {
356
- newDeferArgs = {
357
- ...deferArgs,
358
- label,
359
- };
360
- }
361
- }
362
- if (conditionVariable) {
363
- normalizer.registerCondition(label, conditionVariable);
364
- }
365
- if (!newDeferArgs) {
366
- return this;
367
- }
368
- const deferDirective = this.schema().deferDirective();
369
- const updatedDirectives = this.appliedDirectives
370
- .filter((d) => d.name !== deferDirective.name)
371
- .concat(new definitions_1.Directive(deferDirective.name, newDeferArgs));
372
- const updated = new FragmentElement(this.sourceType, this.typeCondition, updatedDirectives);
373
- this.copyAttachementsTo(updated);
374
- return updated;
375
- }
376
- equals(that) {
377
- var _a, _b;
378
- if (this === that) {
379
- return true;
380
- }
381
- return that.kind === 'FragmentElement'
382
- && ((_a = this.typeCondition) === null || _a === void 0 ? void 0 : _a.name) === ((_b = that.typeCondition) === null || _b === void 0 ? void 0 : _b.name)
383
- && haveSameDirectives(this, that);
384
- }
385
- toString() {
386
- return '...' + (this.typeCondition ? ' on ' + this.typeCondition : '') + this.appliedDirectivesToString();
387
- }
388
- }
389
- exports.FragmentElement = FragmentElement;
390
- function operationPathToStringPath(path) {
391
- return path
392
- .filter((p) => !(p.kind === 'FragmentElement' && !p.typeCondition))
393
- .map((p) => { var _a; return p.kind === 'Field' ? p.responseName() : `... on ${(_a = p.typeCondition) === null || _a === void 0 ? void 0 : _a.coordinate}`; });
394
- }
395
- exports.operationPathToStringPath = operationPathToStringPath;
396
- function sameOperationPaths(p1, p2) {
397
- if (p1 === p2) {
398
- return true;
399
- }
400
- if (p1.length !== p2.length) {
401
- return false;
402
- }
403
- for (let i = 0; i < p1.length; i++) {
404
- if (!p1[i].equals(p2[i])) {
405
- return false;
406
- }
407
- }
408
- return true;
409
- }
410
- exports.sameOperationPaths = sameOperationPaths;
411
- function conditionalDirectivesInOperationPath(path) {
412
- return path.map((e) => e.appliedDirectives).flat().filter((d) => (0, definitions_1.isConditionalDirective)(d));
413
- }
414
- exports.conditionalDirectivesInOperationPath = conditionalDirectivesInOperationPath;
415
- function concatOperationPaths(head, tail) {
416
- if (head.length === 0) {
417
- return tail;
418
- }
419
- if (tail.length === 0) {
420
- return head;
421
- }
422
- const lastOfHead = head[head.length - 1];
423
- const conditionals = conditionalDirectivesInOperationPath(head);
424
- let firstOfTail = tail[0];
425
- while (firstOfTail && isUselessFollowupElement(lastOfHead, firstOfTail, conditionals)) {
426
- tail = tail.slice(1);
427
- firstOfTail = tail[0];
428
- }
429
- return head.concat(tail);
430
- }
431
- exports.concatOperationPaths = concatOperationPaths;
432
- function isUselessFollowupElement(first, followup, conditionals) {
433
- const typeOfFirst = first.kind === 'Field'
434
- ? (0, definitions_1.baseType)(first.definition.type)
435
- : first.typeCondition;
436
- return !!typeOfFirst
437
- && followup.kind === 'FragmentElement'
438
- && !!followup.typeCondition
439
- && (followup.appliedDirectives.length === 0 || (0, definitions_1.isDirectiveApplicationsSubset)(conditionals, followup.appliedDirectives))
440
- && (0, types_1.isSubtype)(followup.typeCondition, typeOfFirst);
441
- }
442
- class Operation {
443
- constructor(schema, rootKind, selectionSet, variableDefinitions, name) {
444
- this.schema = schema;
445
- this.rootKind = rootKind;
446
- this.selectionSet = selectionSet;
447
- this.variableDefinitions = variableDefinitions;
448
- this.name = name;
449
- }
450
- optimize(fragments, minUsagesToOptimize = 2) {
451
- var _a;
452
- (0, utils_1.assert)(minUsagesToOptimize >= 1, `Expected 'minUsagesToOptimize' to be at least 1, but got ${minUsagesToOptimize}`);
453
- if (!fragments || fragments.isEmpty()) {
454
- return this;
455
- }
456
- let optimizedSelection = this.selectionSet.optimize(fragments);
457
- if (optimizedSelection === this.selectionSet) {
458
- return this;
459
- }
460
- const usages = new Map();
461
- optimizedSelection.collectUsedFragmentNames(usages);
462
- for (const fragment of fragments.names()) {
463
- if (!usages.has(fragment)) {
464
- usages.set(fragment, 0);
465
- }
466
- }
467
- const toDeoptimize = (0, utils_1.mapEntries)(usages).filter(([_, count]) => count < minUsagesToOptimize).map(([name]) => name);
468
- if (toDeoptimize.length > 0) {
469
- const newFragments = (_a = optimizedSelection.fragments) === null || _a === void 0 ? void 0 : _a.without(toDeoptimize);
470
- optimizedSelection = optimizedSelection.expandFragments(toDeoptimize, newFragments);
471
- optimizedSelection = optimizedSelection.trimUnsatisfiableBranches(optimizedSelection.parentType);
472
- }
473
- return new Operation(this.schema, this.rootKind, optimizedSelection, this.variableDefinitions, this.name);
474
- }
475
- expandAllFragments() {
476
- const expandedSelections = this.selectionSet.expandAllFragments();
477
- if (expandedSelections === this.selectionSet) {
478
- return this;
479
- }
480
- return new Operation(this.schema, this.rootKind, expandedSelections, this.variableDefinitions, this.name);
481
- }
482
- trimUnsatisfiableBranches() {
483
- const trimmedSelections = this.selectionSet.trimUnsatisfiableBranches(this.selectionSet.parentType);
484
- if (trimmedSelections === this.selectionSet) {
485
- return this;
486
- }
487
- return new Operation(this.schema, this.rootKind, trimmedSelections, this.variableDefinitions, this.name);
488
- }
489
- withoutDefer(labelsToRemove) {
490
- (0, utils_1.assert)(!this.selectionSet.fragments || this.selectionSet.fragments.isEmpty(), 'Removing @defer currently only work on "expanded" selections (no named fragments)');
491
- const updated = this.selectionSet.withoutDefer(labelsToRemove);
492
- return updated == this.selectionSet
493
- ? this
494
- : new Operation(this.schema, this.rootKind, updated, this.variableDefinitions, this.name);
495
- }
496
- withNormalizedDefer() {
497
- (0, utils_1.assert)(!this.selectionSet.fragments || this.selectionSet.fragments.isEmpty(), 'Assigning @defer lables currently only work on "expanded" selections (no named fragments)');
498
- const normalizer = new DeferNormalizer();
499
- const { hasDefers, hasNonLabelledOrConditionalDefers } = normalizer.init(this.selectionSet);
500
- let updatedOperation = this;
501
- if (hasNonLabelledOrConditionalDefers) {
502
- const updated = this.selectionSet.withNormalizedDefer(normalizer);
503
- updatedOperation = new Operation(this.schema, this.rootKind, updated, this.variableDefinitions, this.name);
504
- }
505
- return {
506
- operation: updatedOperation,
507
- hasDefers,
508
- assignedDeferLabels: normalizer.assignedLabels,
509
- deferConditions: normalizer.deferConditions,
510
- };
511
- }
512
- collectDefaultedVariableValues() {
513
- const defaultedVariableValues = {};
514
- for (const { variable, defaultValue } of this.variableDefinitions.definitions()) {
515
- if (defaultValue !== undefined) {
516
- defaultedVariableValues[variable.name] = defaultValue;
517
- }
518
- }
519
- return defaultedVariableValues;
520
- }
521
- toString(expandFragments = false, prettyPrint = true) {
522
- return this.selectionSet.toOperationString(this.rootKind, this.variableDefinitions, this.name, expandFragments, prettyPrint);
523
- }
524
- }
525
- exports.Operation = Operation;
526
- class NamedFragmentDefinition extends definitions_1.DirectiveTargetElement {
527
- constructor(schema, name, typeCondition, directives) {
528
- super(schema, directives);
529
- this.name = name;
530
- this.typeCondition = typeCondition;
531
- this.selectionSetsAtTypesCache = new Map();
532
- }
533
- setSelectionSet(selectionSet) {
534
- (0, utils_1.assert)(!this._selectionSet, 'Attempting to set the selection set of a fragment definition already built');
535
- (0, utils_1.assert)(selectionSet.parentType === this.typeCondition, `Fragment selection set parent is ${selectionSet.parentType} differs from the fragment condition type ${this.typeCondition}`);
536
- this._selectionSet = selectionSet;
537
- return this;
538
- }
539
- get selectionSet() {
540
- (0, utils_1.assert)(this._selectionSet, () => `Trying to access fragment definition ${this.name} before it is fully built`);
541
- return this._selectionSet;
542
- }
543
- withUpdatedSelectionSet(newSelectionSet) {
544
- return new NamedFragmentDefinition(this.schema(), this.name, this.typeCondition).setSelectionSet(newSelectionSet);
545
- }
546
- collectUsedFragmentNames(collector) {
547
- this.selectionSet.collectUsedFragmentNames(collector);
548
- }
549
- toFragmentDefinitionNode() {
550
- return {
551
- kind: graphql_1.Kind.FRAGMENT_DEFINITION,
552
- name: {
553
- kind: graphql_1.Kind.NAME,
554
- value: this.name
555
- },
556
- typeCondition: {
557
- kind: graphql_1.Kind.NAMED_TYPE,
558
- name: {
559
- kind: graphql_1.Kind.NAME,
560
- value: this.typeCondition.name
561
- }
562
- },
563
- selectionSet: this.selectionSet.toSelectionSetNode()
564
- };
565
- }
566
- canApplyAtType(type) {
567
- return (0, types_1.sameType)(type, this.typeCondition) || (0, definitions_1.runtimeTypesIntersects)(type, this.typeCondition);
568
- }
569
- selectionSetAtType(type) {
570
- if ((0, types_1.sameType)(type, this.typeCondition) || (0, definitions_1.isObjectType)(this.typeCondition)) {
571
- return this.selectionSet;
572
- }
573
- if (!(0, definitions_1.isObjectType)(type)) {
574
- return this.selectionSet;
575
- }
576
- let selectionSet = this.selectionSetsAtTypesCache.get(type.name);
577
- if (!selectionSet) {
578
- selectionSet = this.selectionSet.trimUnsatisfiableBranches(type, { recursive: false });
579
- this.selectionSetsAtTypesCache.set(type.name, selectionSet);
580
- }
581
- return selectionSet;
582
- }
583
- toString(indent) {
584
- return (indent !== null && indent !== void 0 ? indent : '') + `fragment ${this.name} on ${this.typeCondition}${this.appliedDirectivesToString()} ${this.selectionSet.toString(false, true, indent)}`;
585
- }
586
- }
587
- exports.NamedFragmentDefinition = NamedFragmentDefinition;
588
- class NamedFragments {
589
- constructor() {
590
- this.fragments = new utils_1.MapWithCachedArrays();
591
- }
592
- isEmpty() {
593
- return this.fragments.size === 0;
594
- }
595
- names() {
596
- return this.fragments.keys();
597
- }
598
- add(fragment) {
599
- if (this.fragments.has(fragment.name)) {
600
- throw error_1.ERRORS.INVALID_GRAPHQL.err(`Duplicate fragment name '${fragment}'`);
601
- }
602
- this.fragments.set(fragment.name, fragment);
603
- }
604
- addIfNotExist(fragment) {
605
- if (!this.fragments.has(fragment.name)) {
606
- this.fragments.set(fragment.name, fragment);
607
- }
608
- }
609
- maybeApplyingAtType(type) {
610
- return this.fragments.values().filter(f => f.canApplyAtType(type));
611
- }
612
- without(names) {
613
- if (!names.some(n => this.fragments.has(n))) {
614
- return this;
615
- }
616
- const newFragments = new NamedFragments();
617
- for (const fragment of this.fragments.values()) {
618
- if (!names.includes(fragment.name)) {
619
- const updatedSelectionSet = fragment.selectionSet.expandFragments(names, newFragments);
620
- const newFragment = updatedSelectionSet === fragment.selectionSet
621
- ? fragment
622
- : fragment.withUpdatedSelectionSet(updatedSelectionSet);
623
- newFragments.add(newFragment);
624
- }
625
- }
626
- return newFragments.isEmpty() ? undefined : newFragments;
627
- }
628
- get(name) {
629
- return this.fragments.get(name);
630
- }
631
- has(name) {
632
- return this.fragments.has(name);
633
- }
634
- definitions() {
635
- return this.fragments.values();
636
- }
637
- map(mapper) {
638
- const mapped = new NamedFragments();
639
- for (const def of this.fragments.values()) {
640
- mapped.fragments.set(def.name, mapper(def));
641
- }
642
- return mapped;
643
- }
644
- mapToExpandedSelectionSets(mapper, recreateFct = (f, s) => f.withUpdatedSelectionSet(s)) {
645
- const fragmentsMap = new Map();
646
- const removedFragments = new Set();
647
- for (const fragment of this.definitions()) {
648
- const mappedSelectionSet = mapper(fragment.selectionSet.expandAllFragments().trimUnsatisfiableBranches(fragment.typeCondition));
649
- if (!mappedSelectionSet) {
650
- removedFragments.add(fragment.name);
651
- continue;
652
- }
653
- const otherFragmentsUsages = new Map();
654
- fragment.collectUsedFragmentNames(otherFragmentsUsages);
655
- fragmentsMap.set(fragment.name, {
656
- original: fragment,
657
- mappedSelectionSet,
658
- dependsOn: Array.from(otherFragmentsUsages.keys()),
659
- });
660
- }
661
- const mappedFragments = new NamedFragments();
662
- while (fragmentsMap.size > 0) {
663
- for (const [name, info] of fragmentsMap) {
664
- if (info.dependsOn.every((n) => mappedFragments.has(n) || removedFragments.has(n))) {
665
- const reoptimizedSelectionSet = info.mappedSelectionSet.optimize(mappedFragments);
666
- mappedFragments.add(recreateFct(info.original, reoptimizedSelectionSet));
667
- fragmentsMap.delete(name);
668
- }
669
- }
670
- }
671
- return mappedFragments.isEmpty() ? undefined : mappedFragments;
672
- }
673
- rebaseOn(schema) {
674
- return this.mapToExpandedSelectionSets((s) => {
675
- const rebasedType = schema.type(s.parentType.name);
676
- try {
677
- return rebasedType && (0, definitions_1.isCompositeType)(rebasedType) ? s.rebaseOn(rebasedType) : undefined;
678
- }
679
- catch (e) {
680
- return undefined;
681
- }
682
- }, (orig, newSelection) => new NamedFragmentDefinition(schema, orig.name, newSelection.parentType).setSelectionSet(newSelection));
683
- }
684
- validate(variableDefinitions) {
685
- for (const fragment of this.fragments.values()) {
686
- fragment.selectionSet.validate(variableDefinitions);
687
- }
688
- }
689
- toFragmentDefinitionNodes() {
690
- return this.definitions().map(f => f.toFragmentDefinitionNode());
691
- }
692
- toString(indent) {
693
- return this.definitions().map(f => f.toString(indent)).join('\n\n');
694
- }
695
- }
696
- exports.NamedFragments = NamedFragments;
697
- class DeferNormalizer {
698
- constructor() {
699
- this.index = 0;
700
- this.assignedLabels = new Set();
701
- this.deferConditions = new utils_1.SetMultiMap();
702
- this.usedLabels = new Set();
703
- }
704
- init(selectionSet) {
705
- let hasNonLabelledOrConditionalDefers = false;
706
- let hasDefers = false;
707
- const stack = selectionSet.selections().concat();
708
- while (stack.length > 0) {
709
- const selection = stack.pop();
710
- if (selection.kind === 'FragmentSelection') {
711
- const deferArgs = selection.element.deferDirectiveArgs();
712
- if (deferArgs) {
713
- hasDefers = true;
714
- if (!deferArgs.label || deferArgs.if !== undefined) {
715
- hasNonLabelledOrConditionalDefers = true;
716
- }
717
- if (deferArgs.label) {
718
- this.usedLabels.add(deferArgs.label);
719
- }
720
- }
721
- }
722
- if (selection.selectionSet) {
723
- selection.selectionSet.selections().forEach((s) => stack.push(s));
724
- }
725
- }
726
- return { hasDefers, hasNonLabelledOrConditionalDefers };
727
- }
728
- nextLabel() {
729
- return `qp__${this.index++}`;
730
- }
731
- newLabel() {
732
- let candidate = this.nextLabel();
733
- while (this.usedLabels.has(candidate)) {
734
- candidate = this.nextLabel();
735
- }
736
- this.assignedLabels.add(candidate);
737
- return candidate;
738
- }
739
- registerCondition(label, condition) {
740
- this.deferConditions.add(condition.name, label);
741
- }
742
- }
743
- class SelectionSet {
744
- constructor(parentType, keyedSelections = new Map(), fragments) {
745
- this.parentType = parentType;
746
- this.fragments = fragments;
747
- this._keyedSelections = keyedSelections;
748
- this._selections = (0, utils_1.mapValues)(keyedSelections);
749
- }
750
- selectionsInReverseOrder() {
751
- const length = this._selections.length;
752
- const reversed = new Array(length);
753
- for (let i = 0; i < length; i++) {
754
- reversed[i] = this._selections[length - i - 1];
755
- }
756
- return reversed;
757
- }
758
- selections() {
759
- return this._selections;
760
- }
761
- hasTopLevelTypenameField() {
762
- return this._keyedSelections.has(definitions_1.typenameFieldName);
763
- }
764
- fieldsInSet() {
765
- const fields = new Array();
766
- for (const selection of this.selections()) {
767
- if (selection.kind === 'FieldSelection') {
768
- fields.push({ path: [], field: selection });
769
- }
770
- else {
771
- const condition = selection.element.typeCondition;
772
- const header = condition ? [`... on ${condition}`] : [];
773
- for (const { path, field } of selection.selectionSet.fieldsInSet()) {
774
- fields.push({ path: header.concat(path), field });
775
- }
776
- }
777
- }
778
- return fields;
779
- }
780
- usedVariables() {
781
- const collector = new definitions_1.VariableCollector();
782
- this.collectVariables(collector);
783
- return collector.variables();
784
- }
785
- collectVariables(collector) {
786
- for (const selection of this.selections()) {
787
- selection.collectVariables(collector);
788
- }
789
- }
790
- collectUsedFragmentNames(collector) {
791
- for (const selection of this.selections()) {
792
- selection.collectUsedFragmentNames(collector);
793
- }
794
- }
795
- optimize(fragments) {
796
- if (!fragments || fragments.isEmpty()) {
797
- return this;
798
- }
799
- const wrapped = new InlineFragmentSelection(new FragmentElement(this.parentType, this.parentType), this);
800
- const optimized = wrapped.optimize(fragments);
801
- return optimized instanceof FragmentSpreadSelection
802
- ? selectionSetOf(this.parentType, optimized, fragments)
803
- : optimized.selectionSet;
804
- }
805
- optimizeSelections(fragments) {
806
- (0, utils_1.assert)(!this.fragments || this.fragments.isEmpty(), `Should not be called on selection that already has named fragments, but got ${this.fragments}`);
807
- return this.lazyMap((selection) => selection.optimize(fragments), { fragments });
808
- }
809
- expandAllFragments() {
810
- return this.lazyMap((selection) => selection.expandAllFragments(), { fragments: null });
811
- }
812
- expandFragments(names, updatedFragments) {
813
- if (names.length === 0) {
814
- return this;
815
- }
816
- return this.lazyMap((selection) => selection.expandFragments(names, updatedFragments), { fragments: updatedFragments !== null && updatedFragments !== void 0 ? updatedFragments : null });
817
- }
818
- trimUnsatisfiableBranches(parentType, options) {
819
- return this.lazyMap((selection) => selection.trimUnsatisfiableBranches(parentType, options), { parentType });
820
- }
821
- lazyMap(mapper, options) {
822
- var _a;
823
- const selections = this.selections();
824
- const updatedFragments = options === null || options === void 0 ? void 0 : options.fragments;
825
- const newFragments = updatedFragments === undefined ? this.fragments : (updatedFragments !== null && updatedFragments !== void 0 ? updatedFragments : undefined);
826
- let updatedSelections = undefined;
827
- for (let i = 0; i < selections.length; i++) {
828
- const selection = selections[i];
829
- const updated = mapper(selection);
830
- if (updated !== selection && !updatedSelections) {
831
- updatedSelections = new SelectionSetUpdates();
832
- for (let j = 0; j < i; j++) {
833
- updatedSelections.add(selections[j]);
834
- }
835
- }
836
- if (!!updated && updatedSelections) {
837
- updatedSelections.add(updated);
838
- }
839
- }
840
- if (!updatedSelections) {
841
- return this.withUpdatedFragments(newFragments);
842
- }
843
- return updatedSelections.toSelectionSet((_a = options === null || options === void 0 ? void 0 : options.parentType) !== null && _a !== void 0 ? _a : this.parentType, newFragments);
844
- }
845
- withUpdatedFragments(newFragments) {
846
- return this.fragments === newFragments ? this : new SelectionSet(this.parentType, this._keyedSelections, newFragments);
847
- }
848
- withoutDefer(labelsToRemove) {
849
- (0, utils_1.assert)(!this.fragments, 'Not yet supported');
850
- return this.lazyMap((selection) => selection.withoutDefer(labelsToRemove));
851
- }
852
- withNormalizedDefer(normalizer) {
853
- (0, utils_1.assert)(!this.fragments, 'Not yet supported');
854
- return this.lazyMap((selection) => selection.withNormalizedDefer(normalizer));
855
- }
856
- hasDefer() {
857
- return this.selections().some((s) => s.hasDefer());
858
- }
859
- filter(predicate) {
860
- return this.lazyMap((selection) => selection.filter(predicate));
861
- }
862
- withoutEmptyBranches() {
863
- const updated = this.filter((selection) => { var _a; return ((_a = selection.selectionSet) === null || _a === void 0 ? void 0 : _a.isEmpty()) !== true; });
864
- return updated.isEmpty() ? undefined : updated;
865
- }
866
- rebaseOn(parentType) {
867
- if (this.parentType === parentType) {
868
- return this;
869
- }
870
- const newSelections = new Map();
871
- for (const selection of this.selections()) {
872
- newSelections.set(selection.key(), selection.rebaseOn(parentType));
873
- }
874
- return new SelectionSet(parentType, newSelections, this.fragments);
875
- }
876
- equals(that) {
877
- if (this === that) {
878
- return true;
879
- }
880
- if (this._selections.length !== that._selections.length) {
881
- return false;
882
- }
883
- for (const [key, thisSelection] of this._keyedSelections) {
884
- const thatSelection = that._keyedSelections.get(key);
885
- if (!thatSelection || !thisSelection.equals(thatSelection)) {
886
- return false;
887
- }
888
- }
889
- return true;
890
- }
891
- triviallyNestedSelectionsForKey(parentType, key) {
892
- const found = [];
893
- for (const selection of this.selections()) {
894
- if (selection.isUnecessaryInlineFragment(parentType)) {
895
- const selectionForKey = selection.selectionSet._keyedSelections.get(key);
896
- if (selectionForKey) {
897
- found.push(selectionForKey);
898
- }
899
- for (const nestedSelection of selection.selectionSet.triviallyNestedSelectionsForKey(parentType, key)) {
900
- found.push(nestedSelection);
901
- }
902
- }
903
- }
904
- return found;
905
- }
906
- mergeSameKeySelections(selections) {
907
- if (selections.length === 0) {
908
- return undefined;
909
- }
910
- const first = selections[0];
911
- if (!first.selectionSet || (first instanceof FragmentSpreadSelection) || selections.length === 1) {
912
- return first;
913
- }
914
- const mergedSubselections = new SelectionSetUpdates();
915
- for (const selection of selections) {
916
- mergedSubselections.add(selection.selectionSet);
917
- }
918
- return first.withUpdatedSelectionSet(mergedSubselections.toSelectionSet(first.selectionSet.parentType));
919
- }
920
- contains(that) {
921
- for (const [key, thatSelection] of that._keyedSelections) {
922
- const thisSelection = this._keyedSelections.get(key);
923
- const otherSelections = this.triviallyNestedSelectionsForKey(this.parentType, key);
924
- const mergedSelection = this.mergeSameKeySelections([thisSelection].concat(otherSelections).filter(utils_1.isDefined));
925
- if (!(mergedSelection && mergedSelection.contains(thatSelection))
926
- && !(thatSelection.isUnecessaryInlineFragment(this.parentType) && this.contains(thatSelection.selectionSet))) {
927
- return false;
928
- }
929
- }
930
- return true;
931
- }
932
- diffWithNamedFragmentIfContained(candidate, parentType) {
933
- const that = candidate.selectionSetAtType(parentType);
934
- if (this.contains(that)) {
935
- let updatedThis = this.expandFragments([candidate.name], this.fragments);
936
- if (updatedThis !== this) {
937
- updatedThis = updatedThis.trimUnsatisfiableBranches(parentType);
938
- }
939
- const diff = updatedThis.minus(that);
940
- return { contains: true, diff: diff.isEmpty() ? undefined : diff };
941
- }
942
- return { contains: false };
943
- }
944
- minus(that) {
945
- const updated = new SelectionSetUpdates();
946
- for (const [key, thisSelection] of this._keyedSelections) {
947
- const thatSelection = that._keyedSelections.get(key);
948
- const otherSelections = that.triviallyNestedSelectionsForKey(this.parentType, key);
949
- const allSelections = thatSelection ? [thatSelection].concat(otherSelections) : otherSelections;
950
- if (allSelections.length === 0) {
951
- updated.add(thisSelection);
952
- }
953
- else {
954
- const selectionDiff = allSelections.reduce((prev, val) => prev === null || prev === void 0 ? void 0 : prev.minus(val), thisSelection);
955
- if (selectionDiff) {
956
- updated.add(selectionDiff);
957
- }
958
- }
959
- }
960
- return updated.toSelectionSet(this.parentType, this.fragments);
961
- }
962
- canRebaseOn(parentTypeToTest) {
963
- return this.selections().every((selection) => selection.canAddTo(parentTypeToTest));
964
- }
965
- validate(variableDefinitions) {
966
- validate(!this.isEmpty(), () => `Invalid empty selection set`);
967
- for (const selection of this.selections()) {
968
- selection.validate(variableDefinitions);
969
- }
970
- }
971
- isEmpty() {
972
- return this._selections.length === 0;
973
- }
974
- toSelectionSetNode() {
975
- if (this.isEmpty()) {
976
- return {
977
- kind: graphql_1.Kind.SELECTION_SET,
978
- selections: [{
979
- kind: graphql_1.Kind.FIELD,
980
- name: {
981
- kind: graphql_1.Kind.NAME,
982
- value: '...',
983
- },
984
- }]
985
- };
986
- }
987
- return {
988
- kind: graphql_1.Kind.SELECTION_SET,
989
- selections: Array.from(this.selectionsInPrintOrder(), s => s.toSelectionNode())
990
- };
991
- }
992
- selectionsInPrintOrder() {
993
- const isNonAliasedTypenameSelection = (s) => s.kind === 'FieldSelection' && !s.element.alias && s.element.name === definitions_1.typenameFieldName;
994
- const typenameSelection = this._selections.find((s) => isNonAliasedTypenameSelection(s));
995
- if (typenameSelection) {
996
- return [typenameSelection].concat(this.selections().filter(s => !isNonAliasedTypenameSelection(s)));
997
- }
998
- else {
999
- return this._selections;
1000
- }
1001
- }
1002
- toOperationPaths() {
1003
- return this.toOperationPathsInternal([]);
1004
- }
1005
- toOperationPathsInternal(parentPaths) {
1006
- return this.selections().flatMap((selection) => {
1007
- const updatedPaths = parentPaths.map(path => path.concat(selection.element));
1008
- return selection.selectionSet
1009
- ? selection.selectionSet.toOperationPathsInternal(updatedPaths)
1010
- : updatedPaths;
1011
- });
1012
- }
1013
- forEachElement(callback) {
1014
- var _a;
1015
- const stack = this.selectionsInReverseOrder().concat();
1016
- while (stack.length > 0) {
1017
- const selection = stack.pop();
1018
- callback(selection.element);
1019
- (_a = selection.selectionSet) === null || _a === void 0 ? void 0 : _a.selectionsInReverseOrder().forEach((s) => stack.push(s));
1020
- }
1021
- }
1022
- some(predicate) {
1023
- for (const selection of this.selections()) {
1024
- if (predicate(selection.element) || (selection.selectionSet && selection.selectionSet.some(predicate))) {
1025
- return true;
1026
- }
1027
- }
1028
- return false;
1029
- }
1030
- toOperationString(rootKind, variableDefinitions, operationName, expandFragments = false, prettyPrint = true) {
1031
- const indent = prettyPrint ? '' : undefined;
1032
- const fragmentsDefinitions = !expandFragments && this.fragments && !this.fragments.isEmpty()
1033
- ? this.fragments.toString(indent) + "\n\n"
1034
- : "";
1035
- if (rootKind == "query" && !operationName && variableDefinitions.isEmpty()) {
1036
- return fragmentsDefinitions + this.toString(expandFragments, true, indent);
1037
- }
1038
- const nameAndVariables = operationName
1039
- ? " " + (operationName + (variableDefinitions.isEmpty() ? "" : variableDefinitions.toString()))
1040
- : (variableDefinitions.isEmpty() ? "" : " " + variableDefinitions.toString());
1041
- return fragmentsDefinitions + rootKind + nameAndVariables + " " + this.toString(expandFragments, true, indent);
1042
- }
1043
- toString(expandFragments = true, includeExternalBrackets = true, indent) {
1044
- if (this.isEmpty()) {
1045
- return '{}';
1046
- }
1047
- if (indent === undefined) {
1048
- const selectionsToString = this.selections().map(s => s.toString(expandFragments)).join(' ');
1049
- return includeExternalBrackets ? '{ ' + selectionsToString + ' }' : selectionsToString;
1050
- }
1051
- else {
1052
- const selectionIndent = includeExternalBrackets ? indent + " " : indent;
1053
- const selectionsToString = this.selections().map(s => s.toString(expandFragments, selectionIndent)).join('\n');
1054
- return includeExternalBrackets
1055
- ? '{\n' + selectionsToString + '\n' + indent + '}'
1056
- : selectionsToString;
1057
- }
1058
- }
1059
- }
1060
- exports.SelectionSet = SelectionSet;
1061
- class SelectionSetUpdates {
1062
- constructor() {
1063
- this.keyedUpdates = new utils_1.MultiMap;
1064
- }
1065
- isEmpty() {
1066
- return this.keyedUpdates.size === 0;
1067
- }
1068
- add(selections) {
1069
- addToKeyedUpdates(this.keyedUpdates, selections);
1070
- return this;
1071
- }
1072
- addAtPath(path, selections) {
1073
- if (path.length === 0) {
1074
- if (selections) {
1075
- addToKeyedUpdates(this.keyedUpdates, selections);
1076
- }
1077
- }
1078
- else {
1079
- if (path.length === 1 && !selections) {
1080
- const element = path[0];
1081
- if (element.kind === 'Field' && element.isLeafField()) {
1082
- const selection = selectionOfElement(element);
1083
- this.keyedUpdates.add(selection.key(), selection);
1084
- return this;
1085
- }
1086
- }
1087
- this.keyedUpdates.add(path[0].key(), { path, selections });
1088
- }
1089
- return this;
1090
- }
1091
- clone() {
1092
- const cloned = new SelectionSetUpdates();
1093
- for (const [key, values] of this.keyedUpdates.entries()) {
1094
- cloned.keyedUpdates.set(key, Array.from(values));
1095
- }
1096
- return cloned;
1097
- }
1098
- clear() {
1099
- this.keyedUpdates.clear();
1100
- }
1101
- toSelectionSet(parentType, fragments) {
1102
- return makeSelectionSet(parentType, this.keyedUpdates, fragments);
1103
- }
1104
- }
1105
- exports.SelectionSetUpdates = SelectionSetUpdates;
1106
- function addToKeyedUpdates(keyedUpdates, selections) {
1107
- if (selections instanceof AbstractSelection) {
1108
- addOneToKeyedUpdates(keyedUpdates, selections);
1109
- }
1110
- else {
1111
- const toAdd = selections instanceof SelectionSet ? selections.selections() : selections;
1112
- for (const selection of toAdd) {
1113
- addOneToKeyedUpdates(keyedUpdates, selection);
1114
- }
1115
- }
1116
- }
1117
- function addOneToKeyedUpdates(keyedUpdates, selection) {
1118
- if (selection instanceof FragmentSpreadSelection) {
1119
- keyedUpdates.set(selection.key(), [selection]);
1120
- }
1121
- else {
1122
- keyedUpdates.add(selection.key(), selection);
1123
- }
1124
- }
1125
- function maybeRebaseOnSchema(toRebase, schema) {
1126
- if (toRebase.schema() === schema) {
1127
- return toRebase;
1128
- }
1129
- const rebased = schema.type(toRebase.name);
1130
- (0, utils_1.assert)(rebased && (0, definitions_1.isCompositeType)(rebased), () => `Expected ${toRebase} to exists and be composite in the rebased schema, but got ${rebased === null || rebased === void 0 ? void 0 : rebased.kind}`);
1131
- return rebased;
1132
- }
1133
- function isUnecessaryFragment(parentType, fragment) {
1134
- return fragment.element.appliedDirectives.length === 0
1135
- && (!fragment.element.typeCondition || (0, types_1.isSubtype)(maybeRebaseOnSchema(fragment.element.typeCondition, parentType.schema()), parentType));
1136
- }
1137
- function withUnecessaryFragmentsRemoved(parentType, selections) {
1138
- if (selections instanceof AbstractSelection) {
1139
- if (selections.kind !== 'FragmentSelection' || !isUnecessaryFragment(parentType, selections)) {
1140
- return selections;
1141
- }
1142
- return withUnecessaryFragmentsRemoved(parentType, selections.selectionSet);
1143
- }
1144
- const toCheck = selections instanceof SelectionSet ? selections.selections() : selections;
1145
- const filtered = [];
1146
- for (const selection of toCheck) {
1147
- if (selection.kind === 'FragmentSelection' && isUnecessaryFragment(parentType, selection)) {
1148
- const subSelections = withUnecessaryFragmentsRemoved(parentType, selection.selectionSet);
1149
- if (subSelections instanceof AbstractSelection) {
1150
- filtered.push(subSelections);
1151
- }
1152
- else {
1153
- for (const subSelection of subSelections) {
1154
- filtered.push(subSelection);
1155
- }
1156
- }
1157
- }
1158
- else {
1159
- filtered.push(selection);
1160
- }
1161
- }
1162
- return filtered;
1163
- }
1164
- function makeSelection(parentType, updates, fragments) {
1165
- (0, utils_1.assert)(updates.length > 0, 'Should not be called without any updates');
1166
- const first = updates[0];
1167
- if (updates.length === 1 && first instanceof AbstractSelection) {
1168
- return first.rebaseOn(parentType);
1169
- }
1170
- const element = updateElement(first).rebaseOn(parentType);
1171
- const subSelectionParentType = element.kind === 'Field' ? (0, definitions_1.baseType)(element.definition.type) : element.castedType();
1172
- if (!(0, definitions_1.isCompositeType)(subSelectionParentType)) {
1173
- return selectionOfElement(element);
1174
- }
1175
- const subSelectionKeyedUpdates = new utils_1.MultiMap();
1176
- for (const update of updates) {
1177
- if (update instanceof AbstractSelection) {
1178
- if (update.selectionSet) {
1179
- addToKeyedUpdates(subSelectionKeyedUpdates, update.selectionSet);
1180
- }
1181
- }
1182
- else {
1183
- addSubpathToKeyUpdates(subSelectionKeyedUpdates, subSelectionParentType, update);
1184
- }
1185
- }
1186
- return selectionOfElement(element, makeSelectionSet(subSelectionParentType, subSelectionKeyedUpdates, fragments));
1187
- }
1188
- function updateElement(update) {
1189
- return update instanceof AbstractSelection ? update.element : update.path[0];
1190
- }
1191
- function addSubpathToKeyUpdates(keyedUpdates, subSelectionParentType, pathUpdate) {
1192
- if (pathUpdate.path.length === 1) {
1193
- if (!pathUpdate.selections) {
1194
- return;
1195
- }
1196
- addToKeyedUpdates(keyedUpdates, withUnecessaryFragmentsRemoved(subSelectionParentType, pathUpdate.selections));
1197
- }
1198
- else {
1199
- keyedUpdates.add(pathUpdate.path[1].key(), { path: pathUpdate.path.slice(1), selections: pathUpdate.selections });
1200
- }
1201
- }
1202
- function makeSelectionSet(parentType, keyedUpdates, fragments) {
1203
- const selections = new Map();
1204
- for (const [key, updates] of keyedUpdates.entries()) {
1205
- selections.set(key, makeSelection(parentType, updates, fragments));
1206
- }
1207
- return new SelectionSet(parentType, selections, fragments);
1208
- }
1209
- class MutableSelectionSet {
1210
- constructor(parentType, _updates, memoizer) {
1211
- this.parentType = parentType;
1212
- this._updates = _updates;
1213
- this.memoizer = memoizer;
1214
- }
1215
- static empty(parentType) {
1216
- return this.emptyWithMemoized(parentType, () => ({}));
1217
- }
1218
- static emptyWithMemoized(parentType, memoizer) {
1219
- return new MutableSelectionSet(parentType, new SelectionSetUpdates(), memoizer);
1220
- }
1221
- static of(selectionSet) {
1222
- return this.ofWithMemoized(selectionSet, () => ({}));
1223
- }
1224
- static ofWithMemoized(selectionSet, memoizer) {
1225
- const s = new MutableSelectionSet(selectionSet.parentType, new SelectionSetUpdates(), memoizer);
1226
- s._updates.add(selectionSet);
1227
- s.computed = selectionSet;
1228
- return s;
1229
- }
1230
- isEmpty() {
1231
- return this._updates.isEmpty();
1232
- }
1233
- get() {
1234
- if (!this.computed) {
1235
- this.computed = this._updates.toSelectionSet(this.parentType);
1236
- this._updates.clear();
1237
- this._updates.add(this.computed);
1238
- }
1239
- return this.computed;
1240
- }
1241
- updates() {
1242
- this.computed = undefined;
1243
- this._memoized = undefined;
1244
- return this._updates;
1245
- }
1246
- clone() {
1247
- const cloned = new MutableSelectionSet(this.parentType, this._updates.clone(), this.memoizer);
1248
- cloned.computed = this.computed;
1249
- cloned._memoized = this._memoized;
1250
- return cloned;
1251
- }
1252
- rebaseOn(parentType) {
1253
- const rebased = new MutableSelectionSet(parentType, new SelectionSetUpdates(), this.memoizer);
1254
- rebased._updates.add(this.get());
1255
- return rebased;
1256
- }
1257
- memoized() {
1258
- if (!this._memoized) {
1259
- this._memoized = this.memoizer(this.get());
1260
- }
1261
- return this._memoized;
1262
- }
1263
- toString() {
1264
- return this.get().toString();
1265
- }
1266
- }
1267
- exports.MutableSelectionSet = MutableSelectionSet;
1268
- function allFieldDefinitionsInSelectionSet(selection) {
1269
- const stack = Array.from(selection.selections());
1270
- const allFields = [];
1271
- while (stack.length > 0) {
1272
- const selection = stack.pop();
1273
- if (selection.kind === 'FieldSelection') {
1274
- allFields.push(selection.element.definition);
1275
- }
1276
- if (selection.selectionSet) {
1277
- stack.push(...selection.selectionSet.selections());
1278
- }
1279
- }
1280
- return allFields;
1281
- }
1282
- exports.allFieldDefinitionsInSelectionSet = allFieldDefinitionsInSelectionSet;
1283
- function selectionSetOf(parentType, selection, fragments) {
1284
- const map = new Map();
1285
- map.set(selection.key(), selection);
1286
- return new SelectionSet(parentType, map, fragments);
1287
- }
1288
- exports.selectionSetOf = selectionSetOf;
1289
- function selectionSetOfElement(element, subSelection, fragments) {
1290
- return selectionSetOf(element.parentType, selectionOfElement(element, subSelection), fragments);
1291
- }
1292
- exports.selectionSetOfElement = selectionSetOfElement;
1293
- function selectionOfElement(element, subSelection) {
1294
- return element.kind === 'Field' ? new FieldSelection(element, subSelection) : new InlineFragmentSelection(element, subSelection);
1295
- }
1296
- exports.selectionOfElement = selectionOfElement;
1297
- class AbstractSelection {
1298
- constructor(element) {
1299
- this.element = element;
1300
- }
1301
- get parentType() {
1302
- return this.element.parentType;
1303
- }
1304
- collectVariables(collector) {
1305
- var _a;
1306
- this.element.collectVariables(collector);
1307
- (_a = this.selectionSet) === null || _a === void 0 ? void 0 : _a.collectVariables(collector);
1308
- }
1309
- collectUsedFragmentNames(collector) {
1310
- var _a;
1311
- (_a = this.selectionSet) === null || _a === void 0 ? void 0 : _a.collectUsedFragmentNames(collector);
1312
- }
1313
- namedFragments() {
1314
- var _a;
1315
- return (_a = this.selectionSet) === null || _a === void 0 ? void 0 : _a.fragments;
1316
- }
1317
- withUpdatedSelectionSet(selectionSet) {
1318
- return this.withUpdatedComponents(this.element, selectionSet);
1319
- }
1320
- withUpdatedElement(element) {
1321
- return this.withUpdatedComponents(element, this.selectionSet);
1322
- }
1323
- mapToSelectionSet(mapper) {
1324
- if (!this.selectionSet) {
1325
- return this.us();
1326
- }
1327
- const updatedSelectionSet = mapper(this.selectionSet);
1328
- return updatedSelectionSet === this.selectionSet
1329
- ? this.us()
1330
- : this.withUpdatedSelectionSet(updatedSelectionSet);
1331
- }
1332
- minus(that) {
1333
- if (this.selectionSet && that.selectionSet) {
1334
- const updatedSubSelectionSet = this.selectionSet.minus(that.selectionSet);
1335
- if (!updatedSubSelectionSet.isEmpty()) {
1336
- return this.withUpdatedSelectionSet(updatedSubSelectionSet);
1337
- }
1338
- }
1339
- return undefined;
1340
- }
1341
- tryOptimizeSubselectionOnce(_) {
1342
- (0, utils_1.assert)(false, `UNSUPPORTED`);
1343
- }
1344
- tryOptimizeSubselectionWithFragments({ parentType, subSelection, fragments, fragmentFilter, }) {
1345
- let candidates = fragments.maybeApplyingAtType(parentType);
1346
- if (fragmentFilter) {
1347
- candidates = candidates.filter(fragmentFilter);
1348
- }
1349
- let shouldTryAgain;
1350
- do {
1351
- const { spread, optimizedSelection, hasDiff } = this.tryOptimizeSubselectionOnce({ parentType, subSelection, candidates, fragments });
1352
- if (optimizedSelection) {
1353
- subSelection = optimizedSelection;
1354
- }
1355
- else if (spread) {
1356
- return spread;
1357
- }
1358
- shouldTryAgain = !!spread && !!hasDiff;
1359
- if (shouldTryAgain) {
1360
- candidates = candidates.filter((c) => c !== (spread === null || spread === void 0 ? void 0 : spread.namedFragment));
1361
- }
1362
- } while (shouldTryAgain);
1363
- return subSelection;
1364
- }
1365
- }
1366
- class FieldSelection extends AbstractSelection {
1367
- constructor(field, _selectionSet) {
1368
- super(field);
1369
- this._selectionSet = _selectionSet;
1370
- this.kind = 'FieldSelection';
1371
- }
1372
- get selectionSet() {
1373
- return this._selectionSet;
1374
- }
1375
- us() {
1376
- return this;
1377
- }
1378
- withUpdatedComponents(field, selectionSet) {
1379
- return new FieldSelection(field, selectionSet);
1380
- }
1381
- key() {
1382
- return this.element.key();
1383
- }
1384
- optimize(fragments) {
1385
- let optimizedSelection = this.selectionSet ? this.selectionSet.optimizeSelections(fragments) : undefined;
1386
- const fieldBaseType = (0, definitions_1.baseType)(this.element.definition.type);
1387
- if ((0, definitions_1.isCompositeType)(fieldBaseType) && optimizedSelection) {
1388
- const optimized = this.tryOptimizeSubselectionWithFragments({
1389
- parentType: fieldBaseType,
1390
- subSelection: optimizedSelection,
1391
- fragments,
1392
- fragmentFilter: (f) => f.appliedDirectives.length === 0,
1393
- });
1394
- (0, utils_1.assert)(!(optimized instanceof FragmentSpreadSelection), 'tryOptimizeSubselectionOnce should never return only a spread');
1395
- optimizedSelection = optimized;
1396
- }
1397
- return this.selectionSet === optimizedSelection
1398
- ? this
1399
- : new FieldSelection(this.element, optimizedSelection);
1400
- }
1401
- tryOptimizeSubselectionOnce({ parentType, subSelection, candidates, fragments, }) {
1402
- let optimizedSelection = subSelection;
1403
- for (const candidate of candidates) {
1404
- const { contains, diff } = optimizedSelection.diffWithNamedFragmentIfContained(candidate, parentType);
1405
- if (contains) {
1406
- const spread = new FragmentSpreadSelection(parentType, fragments, candidate, []);
1407
- optimizedSelection = diff
1408
- ? new SelectionSetUpdates().add(spread).add(diff).toSelectionSet(parentType, fragments)
1409
- : selectionSetOf(parentType, spread);
1410
- return { spread, optimizedSelection, hasDiff: !!diff };
1411
- }
1412
- }
1413
- return {};
1414
- }
1415
- filter(predicate) {
1416
- if (!this.selectionSet) {
1417
- return predicate(this) ? this : undefined;
1418
- }
1419
- const updatedSelectionSet = this.selectionSet.filter(predicate);
1420
- const thisWithFilteredSelectionSet = this.selectionSet === updatedSelectionSet
1421
- ? this
1422
- : new FieldSelection(this.element, updatedSelectionSet);
1423
- return predicate(thisWithFilteredSelectionSet) ? thisWithFilteredSelectionSet : undefined;
1424
- }
1425
- validate(variableDefinitions) {
1426
- var _a;
1427
- this.element.validate(variableDefinitions);
1428
- validate(this.element.isLeafField() || (this.selectionSet && !this.selectionSet.isEmpty()), () => `Invalid empty selection set for field "${this.element.definition.coordinate}" of non-leaf type ${this.element.definition.type}`, this.element.definition.sourceAST);
1429
- (_a = this.selectionSet) === null || _a === void 0 ? void 0 : _a.validate(variableDefinitions);
1430
- }
1431
- rebaseOn(parentType) {
1432
- if (this.element.parentType === parentType) {
1433
- return this;
1434
- }
1435
- const rebasedElement = this.element.rebaseOn(parentType);
1436
- if (!this.selectionSet) {
1437
- return this.withUpdatedElement(rebasedElement);
1438
- }
1439
- const rebasedBase = (0, definitions_1.baseType)(rebasedElement.definition.type);
1440
- if (rebasedBase === this.selectionSet.parentType) {
1441
- return this.withUpdatedElement(rebasedElement);
1442
- }
1443
- validate((0, definitions_1.isCompositeType)(rebasedBase), () => `Cannot rebase field selection ${this} on ${parentType}: rebased field base return type ${rebasedBase} is not composite`);
1444
- return this.withUpdatedComponents(rebasedElement, this.selectionSet.rebaseOn(rebasedBase));
1445
- }
1446
- canAddTo(parentType) {
1447
- if (this.element.parentType === parentType) {
1448
- return true;
1449
- }
1450
- const type = this.element.typeIfAddedTo(parentType);
1451
- if (!type) {
1452
- return false;
1453
- }
1454
- const base = (0, definitions_1.baseType)(type);
1455
- if (this.selectionSet && this.selectionSet.parentType !== base) {
1456
- (0, utils_1.assert)((0, definitions_1.isCompositeType)(base), () => `${this.element} should have a selection set as it's type is not a composite`);
1457
- return this.selectionSet.selections().every((s) => s.canAddTo(base));
1458
- }
1459
- return true;
1460
- }
1461
- toSelectionNode() {
1462
- var _a;
1463
- const alias = this.element.alias ? { kind: graphql_1.Kind.NAME, value: this.element.alias, } : undefined;
1464
- return {
1465
- kind: graphql_1.Kind.FIELD,
1466
- name: {
1467
- kind: graphql_1.Kind.NAME,
1468
- value: this.element.name,
1469
- },
1470
- alias,
1471
- arguments: this.element.argumentsToNodes(),
1472
- directives: this.element.appliedDirectivesToDirectiveNodes(),
1473
- selectionSet: (_a = this.selectionSet) === null || _a === void 0 ? void 0 : _a.toSelectionSetNode()
1474
- };
1475
- }
1476
- withoutDefer(labelsToRemove) {
1477
- return this.mapToSelectionSet((s) => s.withoutDefer(labelsToRemove));
1478
- }
1479
- withNormalizedDefer(normalizer) {
1480
- return this.mapToSelectionSet((s) => s.withNormalizedDefer(normalizer));
1481
- }
1482
- hasDefer() {
1483
- var _a;
1484
- return !!((_a = this.selectionSet) === null || _a === void 0 ? void 0 : _a.hasDefer());
1485
- }
1486
- expandAllFragments() {
1487
- return this.mapToSelectionSet((s) => s.expandAllFragments());
1488
- }
1489
- trimUnsatisfiableBranches(_, options) {
1490
- var _a, _b;
1491
- if (!this.selectionSet) {
1492
- return this;
1493
- }
1494
- const base = (0, definitions_1.baseType)(this.element.definition.type);
1495
- (0, utils_1.assert)((0, definitions_1.isCompositeType)(base), () => `Field ${this.element} should not have a sub-selection`);
1496
- const trimmed = ((_a = options === null || options === void 0 ? void 0 : options.recursive) !== null && _a !== void 0 ? _a : true) ? this.mapToSelectionSet((s) => s.trimUnsatisfiableBranches(base)) : this;
1497
- if ((_b = trimmed.selectionSet) === null || _b === void 0 ? void 0 : _b.isEmpty()) {
1498
- return trimmed.withUpdatedSelectionSet(selectionSetOfElement(new Field(base.typenameField(), undefined, [new definitions_1.Directive('include', { 'if': false })])));
1499
- }
1500
- else {
1501
- return trimmed;
1502
- }
1503
- }
1504
- expandFragments(names, updatedFragments) {
1505
- return this.mapToSelectionSet((s) => s.expandFragments(names, updatedFragments));
1506
- }
1507
- equals(that) {
1508
- if (this === that) {
1509
- return true;
1510
- }
1511
- if (!(that instanceof FieldSelection) || !this.element.equals(that.element)) {
1512
- return false;
1513
- }
1514
- if (!this.selectionSet) {
1515
- return !that.selectionSet;
1516
- }
1517
- return !!that.selectionSet && this.selectionSet.equals(that.selectionSet);
1518
- }
1519
- contains(that) {
1520
- if (!(that instanceof FieldSelection) || !this.element.equals(that.element)) {
1521
- return false;
1522
- }
1523
- if (!that.selectionSet) {
1524
- return true;
1525
- }
1526
- return !!this.selectionSet && this.selectionSet.contains(that.selectionSet);
1527
- }
1528
- isUnecessaryInlineFragment(_) {
1529
- return false;
1530
- }
1531
- toString(expandFragments = true, indent) {
1532
- return (indent !== null && indent !== void 0 ? indent : '') + this.element + (this.selectionSet ? ' ' + this.selectionSet.toString(expandFragments, true, indent) : '');
1533
- }
1534
- }
1535
- exports.FieldSelection = FieldSelection;
1536
- class FragmentSelection extends AbstractSelection {
1537
- constructor() {
1538
- super(...arguments);
1539
- this.kind = 'FragmentSelection';
1540
- }
1541
- us() {
1542
- return this;
1543
- }
1544
- validateDeferAndStream() {
1545
- if (this.element.hasDefer() || this.element.hasStream()) {
1546
- const schemaDef = this.element.schema().schemaDefinition;
1547
- const parentType = this.parentType;
1548
- validate(schemaDef.rootType('mutation') !== parentType && schemaDef.rootType('subscription') !== parentType, () => { var _a; return `The @defer and @stream directives cannot be used on ${(_a = schemaDef.roots().filter((t) => t.type === parentType).pop()) === null || _a === void 0 ? void 0 : _a.rootKind} root type "${parentType}"`; });
1549
- }
1550
- }
1551
- filter(predicate) {
1552
- const selectionSet = this.selectionSet;
1553
- const updatedSelectionSet = selectionSet.filter(predicate);
1554
- const thisWithFilteredSelectionSet = updatedSelectionSet === selectionSet
1555
- ? this
1556
- : new InlineFragmentSelection(this.element, updatedSelectionSet);
1557
- return predicate(thisWithFilteredSelectionSet) ? thisWithFilteredSelectionSet : undefined;
1558
- }
1559
- hasDefer() {
1560
- return this.element.hasDefer() || this.selectionSet.hasDefer();
1561
- }
1562
- isUnecessaryInlineFragment(parentType) {
1563
- return this.element.appliedDirectives.length === 0
1564
- && !!this.element.typeCondition
1565
- && (this.element.typeCondition.name === parentType.name
1566
- || ((0, definitions_1.isObjectType)(parentType) && (0, definitions_1.possibleRuntimeTypes)(this.element.typeCondition).some((t) => t.name === parentType.name)));
1567
- }
1568
- }
1569
- exports.FragmentSelection = FragmentSelection;
1570
- class InlineFragmentSelection extends FragmentSelection {
1571
- constructor(fragment, _selectionSet) {
1572
- super(fragment);
1573
- this._selectionSet = _selectionSet;
1574
- }
1575
- get selectionSet() {
1576
- return this._selectionSet;
1577
- }
1578
- key() {
1579
- return this.element.key();
1580
- }
1581
- withUpdatedComponents(fragment, selectionSet) {
1582
- return new InlineFragmentSelection(fragment, selectionSet);
1583
- }
1584
- validate(variableDefinitions) {
1585
- this.validateDeferAndStream();
1586
- validate(!this.selectionSet.isEmpty(), () => `Invalid empty selection set for fragment "${this.element}"`);
1587
- this.selectionSet.validate(variableDefinitions);
1588
- }
1589
- rebaseOn(parentType) {
1590
- if (this.parentType === parentType) {
1591
- return this;
1592
- }
1593
- const rebasedFragment = this.element.rebaseOn(parentType);
1594
- const rebasedCastedType = rebasedFragment.castedType();
1595
- if (rebasedCastedType === this.selectionSet.parentType) {
1596
- return this.withUpdatedElement(rebasedFragment);
1597
- }
1598
- return this.withUpdatedComponents(rebasedFragment, this.selectionSet.rebaseOn(rebasedCastedType));
1599
- }
1600
- canAddTo(parentType) {
1601
- if (this.element.parentType === parentType) {
1602
- return true;
1603
- }
1604
- const type = this.element.castedTypeIfAddedTo(parentType);
1605
- if (!type) {
1606
- return false;
1607
- }
1608
- if (this.selectionSet.parentType !== type) {
1609
- return this.selectionSet.selections().every((s) => s.canAddTo(type));
1610
- }
1611
- return true;
1612
- }
1613
- toSelectionNode() {
1614
- const typeCondition = this.element.typeCondition;
1615
- return {
1616
- kind: graphql_1.Kind.INLINE_FRAGMENT,
1617
- typeCondition: typeCondition
1618
- ? {
1619
- kind: graphql_1.Kind.NAMED_TYPE,
1620
- name: {
1621
- kind: graphql_1.Kind.NAME,
1622
- value: typeCondition.name,
1623
- },
1624
- }
1625
- : undefined,
1626
- directives: this.element.appliedDirectivesToDirectiveNodes(),
1627
- selectionSet: this.selectionSet.toSelectionSetNode()
1628
- };
1629
- }
1630
- optimize(fragments) {
1631
- let optimizedSelection = this.selectionSet.optimizeSelections(fragments);
1632
- const typeCondition = this.element.typeCondition;
1633
- if (typeCondition) {
1634
- const optimized = this.tryOptimizeSubselectionWithFragments({
1635
- parentType: typeCondition,
1636
- subSelection: optimizedSelection,
1637
- fragments,
1638
- });
1639
- if (optimized instanceof FragmentSpreadSelection) {
1640
- return optimized;
1641
- }
1642
- optimizedSelection = optimized;
1643
- }
1644
- return this.selectionSet === optimizedSelection
1645
- ? this
1646
- : new InlineFragmentSelection(this.element, optimizedSelection);
1647
- }
1648
- tryOptimizeSubselectionOnce({ parentType, subSelection, candidates, fragments, }) {
1649
- let optimizedSelection = subSelection;
1650
- for (const candidate of candidates) {
1651
- const { contains, diff } = optimizedSelection.diffWithNamedFragmentIfContained(candidate, parentType);
1652
- if (contains) {
1653
- if (!diff && (0, types_1.sameType)(this.element.typeCondition, candidate.typeCondition)) {
1654
- let spreadDirectives = this.element.appliedDirectives;
1655
- if (candidate.appliedDirectives.length > 0) {
1656
- const { isSubset, difference } = diffDirectives(this.element.appliedDirectives, candidate.appliedDirectives);
1657
- if (!isSubset) {
1658
- continue;
1659
- }
1660
- spreadDirectives = difference;
1661
- }
1662
- return {
1663
- spread: new FragmentSpreadSelection(this.parentType, fragments, candidate, spreadDirectives),
1664
- };
1665
- }
1666
- if (candidate.appliedDirectives.length > 0) {
1667
- continue;
1668
- }
1669
- const spread = new FragmentSpreadSelection(parentType, fragments, candidate, []);
1670
- optimizedSelection = diff
1671
- ? new SelectionSetUpdates().add(spread).add(diff).toSelectionSet(parentType, fragments)
1672
- : selectionSetOf(parentType, spread);
1673
- return { spread, optimizedSelection, hasDiff: !!diff };
1674
- }
1675
- }
1676
- return {};
1677
- }
1678
- withoutDefer(labelsToRemove) {
1679
- const newSelection = this.selectionSet.withoutDefer(labelsToRemove);
1680
- const deferArgs = this.element.deferDirectiveArgs();
1681
- const hasDeferToRemove = deferArgs && (!labelsToRemove || (deferArgs.label && labelsToRemove.has(deferArgs.label)));
1682
- if (newSelection === this.selectionSet && !hasDeferToRemove) {
1683
- return this;
1684
- }
1685
- const newElement = hasDeferToRemove ? this.element.withoutDefer() : this.element;
1686
- if (!newElement) {
1687
- return newSelection;
1688
- }
1689
- return this.withUpdatedComponents(newElement, newSelection);
1690
- }
1691
- withNormalizedDefer(normalizer) {
1692
- const newElement = this.element.withNormalizedDefer(normalizer);
1693
- const newSelection = this.selectionSet.withNormalizedDefer(normalizer);
1694
- if (!newElement) {
1695
- return newSelection;
1696
- }
1697
- return newElement === this.element && newSelection === this.selectionSet
1698
- ? this
1699
- : this.withUpdatedComponents(newElement, newSelection);
1700
- }
1701
- trimUnsatisfiableBranches(currentType, options) {
1702
- var _a, _b, _c;
1703
- const recursive = (_a = options === null || options === void 0 ? void 0 : options.recursive) !== null && _a !== void 0 ? _a : true;
1704
- const thisCondition = this.element.typeCondition;
1705
- if (this.element.appliedDirectives.length === 0) {
1706
- if (!thisCondition || currentType === this.element.typeCondition) {
1707
- const trimmed = this.selectionSet.trimUnsatisfiableBranches(currentType, options);
1708
- return trimmed.isEmpty() ? undefined : trimmed;
1709
- }
1710
- if ((0, definitions_1.isObjectType)(currentType)) {
1711
- if ((0, definitions_1.isObjectType)(thisCondition) || !(0, definitions_1.possibleRuntimeTypes)(thisCondition).includes(currentType)) {
1712
- return undefined;
1713
- }
1714
- else {
1715
- const trimmed = this.selectionSet.trimUnsatisfiableBranches(currentType, options);
1716
- return trimmed.isEmpty() ? undefined : trimmed;
1717
- }
1718
- }
1719
- }
1720
- if (!recursive) {
1721
- return this;
1722
- }
1723
- const trimmedSelectionSet = this.selectionSet.trimUnsatisfiableBranches((_b = this.element.typeCondition) !== null && _b !== void 0 ? _b : this.parentType);
1724
- if (trimmedSelectionSet.isEmpty()) {
1725
- if (this.element.appliedDirectives.length === 0) {
1726
- return undefined;
1727
- }
1728
- else {
1729
- return this.withUpdatedSelectionSet(selectionSetOfElement(new Field(((_c = this.element.typeCondition) !== null && _c !== void 0 ? _c : this.parentType).typenameField(), undefined, [new definitions_1.Directive('include', { 'if': false })])));
1730
- }
1731
- }
1732
- if (this.element.appliedDirectives.length === 0 && (0, definitions_1.isAbstractType)(thisCondition)) {
1733
- (0, utils_1.assert)(!(0, definitions_1.isObjectType)(currentType), () => `Should not have got here if ${currentType} is an object type`);
1734
- const currentRuntimes = (0, definitions_1.possibleRuntimeTypes)(currentType);
1735
- const liftableSelections = [];
1736
- for (const selection of trimmedSelectionSet.selections()) {
1737
- if (selection.kind === 'FragmentSelection'
1738
- && selection.element.typeCondition
1739
- && (0, definitions_1.isObjectType)(selection.element.typeCondition)
1740
- && currentRuntimes.includes(selection.element.typeCondition)) {
1741
- liftableSelections.push(selection);
1742
- }
1743
- }
1744
- if (liftableSelections.length === trimmedSelectionSet.selections().length) {
1745
- return trimmedSelectionSet;
1746
- }
1747
- if (liftableSelections.length > 0) {
1748
- const newSet = new SelectionSetUpdates();
1749
- newSet.add(liftableSelections);
1750
- newSet.add(this.withUpdatedSelectionSet(trimmedSelectionSet.filter((s) => !liftableSelections.includes(s))));
1751
- return newSet.toSelectionSet(this.parentType);
1752
- }
1753
- }
1754
- return this.selectionSet === trimmedSelectionSet ? this : this.withUpdatedSelectionSet(trimmedSelectionSet);
1755
- }
1756
- expandAllFragments() {
1757
- return this.mapToSelectionSet((s) => s.expandAllFragments());
1758
- }
1759
- expandFragments(names, updatedFragments) {
1760
- return this.mapToSelectionSet((s) => s.expandFragments(names, updatedFragments));
1761
- }
1762
- equals(that) {
1763
- if (this === that) {
1764
- return true;
1765
- }
1766
- return (that instanceof FragmentSelection)
1767
- && this.element.equals(that.element)
1768
- && this.selectionSet.equals(that.selectionSet);
1769
- }
1770
- contains(that) {
1771
- return (that instanceof FragmentSelection)
1772
- && this.element.equals(that.element)
1773
- && this.selectionSet.contains(that.selectionSet);
1774
- }
1775
- toString(expandFragments = true, indent) {
1776
- return (indent !== null && indent !== void 0 ? indent : '') + this.element + ' ' + this.selectionSet.toString(expandFragments, true, indent);
1777
- }
1778
- }
1779
- function diffDirectives(superset, maybeSubset) {
1780
- if (maybeSubset.every((d) => superset.some((s) => (0, definitions_1.sameDirectiveApplication)(d, s)))) {
1781
- return { isSubset: true, difference: superset.filter((s) => !maybeSubset.some((d) => (0, definitions_1.sameDirectiveApplication)(d, s))) };
1782
- }
1783
- else {
1784
- return { isSubset: false, difference: [] };
1785
- }
1786
- }
1787
- class FragmentSpreadSelection extends FragmentSelection {
1788
- constructor(sourceType, fragments, namedFragment, spreadDirectives) {
1789
- super(new FragmentElement(sourceType, namedFragment.typeCondition, namedFragment.appliedDirectives.concat(spreadDirectives)));
1790
- this.fragments = fragments;
1791
- this.namedFragment = namedFragment;
1792
- this.spreadDirectives = spreadDirectives;
1793
- }
1794
- get selectionSet() {
1795
- return this.namedFragment.selectionSet;
1796
- }
1797
- key() {
1798
- if (!this.computedKey) {
1799
- this.computedKey = '...' + this.namedFragment.name + (this.spreadDirectives.length === 0 ? '' : ' ' + this.spreadDirectives.join(' '));
1800
- }
1801
- return this.computedKey;
1802
- }
1803
- withUpdatedComponents(_fragment, _selectionSet) {
1804
- (0, utils_1.assert)(false, `Unsupported`);
1805
- }
1806
- trimUnsatisfiableBranches(_) {
1807
- return this;
1808
- }
1809
- namedFragments() {
1810
- return this.fragments;
1811
- }
1812
- validate() {
1813
- this.validateDeferAndStream();
1814
- }
1815
- toSelectionNode() {
1816
- const directiveNodes = this.spreadDirectives.length === 0
1817
- ? undefined
1818
- : this.spreadDirectives.map(directive => {
1819
- return {
1820
- kind: graphql_1.Kind.DIRECTIVE,
1821
- name: {
1822
- kind: graphql_1.Kind.NAME,
1823
- value: directive.name,
1824
- },
1825
- arguments: directive.argumentsToAST()
1826
- };
1827
- });
1828
- return {
1829
- kind: graphql_1.Kind.FRAGMENT_SPREAD,
1830
- name: { kind: graphql_1.Kind.NAME, value: this.namedFragment.name },
1831
- directives: directiveNodes,
1832
- };
1833
- }
1834
- optimize(_) {
1835
- return this;
1836
- }
1837
- rebaseOn(_parentType) {
1838
- return this;
1839
- }
1840
- canAddTo(_) {
1841
- return true;
1842
- }
1843
- expandAllFragments() {
1844
- const expandedSubSelections = this.selectionSet.expandAllFragments();
1845
- return (0, types_1.sameType)(this.parentType, this.namedFragment.typeCondition) && this.element.appliedDirectives.length === 0
1846
- ? expandedSubSelections.selections()
1847
- : new InlineFragmentSelection(this.element, expandedSubSelections);
1848
- }
1849
- expandFragments(names, updatedFragments) {
1850
- if (!names.includes(this.namedFragment.name)) {
1851
- return this;
1852
- }
1853
- const expandedSubSelections = this.selectionSet.expandFragments(names, updatedFragments);
1854
- return (0, types_1.sameType)(this.parentType, this.namedFragment.typeCondition) && this.element.appliedDirectives.length === 0
1855
- ? expandedSubSelections.selections()
1856
- : new InlineFragmentSelection(this.element, expandedSubSelections);
1857
- }
1858
- collectUsedFragmentNames(collector) {
1859
- this.selectionSet.collectUsedFragmentNames(collector);
1860
- const usageCount = collector.get(this.namedFragment.name);
1861
- collector.set(this.namedFragment.name, usageCount === undefined ? 1 : usageCount + 1);
1862
- }
1863
- withoutDefer(_labelsToRemove) {
1864
- (0, utils_1.assert)(false, 'Unsupported, see `Operation.withAllDeferLabelled`');
1865
- }
1866
- withNormalizedDefer(_normalizer) {
1867
- (0, utils_1.assert)(false, 'Unsupported, see `Operation.withAllDeferLabelled`');
1868
- }
1869
- minus(that) {
1870
- (0, utils_1.assert)(this.equals(that), () => `Invalid operation for ${this.toString(false)} and ${that.toString(false)}`);
1871
- return undefined;
1872
- }
1873
- equals(that) {
1874
- if (this === that) {
1875
- return true;
1876
- }
1877
- return (that instanceof FragmentSpreadSelection)
1878
- && this.namedFragment.name === that.namedFragment.name
1879
- && (0, definitions_1.sameDirectiveApplications)(this.spreadDirectives, that.spreadDirectives);
1880
- }
1881
- contains(that) {
1882
- if (this.equals(that)) {
1883
- return true;
1884
- }
1885
- return (that instanceof FragmentSelection)
1886
- && this.element.equals(that.element)
1887
- && this.selectionSet.contains(that.selectionSet);
1888
- }
1889
- toString(expandFragments = true, indent) {
1890
- if (expandFragments) {
1891
- return (indent !== null && indent !== void 0 ? indent : '') + this.element + ' ' + this.selectionSet.toString(true, true, indent);
1892
- }
1893
- else {
1894
- const directives = this.spreadDirectives;
1895
- const directiveString = directives.length == 0 ? '' : ' ' + directives.join(' ');
1896
- return (indent !== null && indent !== void 0 ? indent : '') + '...' + this.namedFragment.name + directiveString;
1897
- }
1898
- }
1899
- }
1900
- function selectionSetOfNode(parentType, node, variableDefinitions, fragments, fieldAccessor = (type, name) => type.field(name)) {
1901
- if (node.selections.length === 1) {
1902
- return selectionSetOf(parentType, selectionOfNode(parentType, node.selections[0], variableDefinitions, fragments, fieldAccessor), fragments);
1903
- }
1904
- const selections = new SelectionSetUpdates();
1905
- for (const selectionNode of node.selections) {
1906
- selections.add(selectionOfNode(parentType, selectionNode, variableDefinitions, fragments, fieldAccessor));
1907
- }
1908
- return selections.toSelectionSet(parentType, fragments);
1909
- }
1910
- function directiveOfNode(schema, node) {
1911
- const directiveDef = schema.directive(node.name.value);
1912
- validate(directiveDef, () => `Unknown directive "@${node.name.value}"`);
1913
- return new definitions_1.Directive(directiveDef.name, (0, values_1.argumentsFromAST)(directiveDef.coordinate, node.arguments, directiveDef));
1914
- }
1915
- function directivesOfNodes(schema, nodes) {
1916
- var _a;
1917
- return (_a = nodes === null || nodes === void 0 ? void 0 : nodes.map((n) => directiveOfNode(schema, n))) !== null && _a !== void 0 ? _a : [];
1918
- }
1919
- function selectionOfNode(parentType, node, variableDefinitions, fragments, fieldAccessor = (type, name) => type.field(name)) {
1920
- var _a, _b;
1921
- let selection;
1922
- const directives = directivesOfNodes(parentType.schema(), node.directives);
1923
- switch (node.kind) {
1924
- case graphql_1.Kind.FIELD:
1925
- const definition = fieldAccessor(parentType, node.name.value);
1926
- validate(definition, () => `Cannot query field "${node.name.value}" on type "${parentType}".`, parentType.sourceAST);
1927
- const type = (0, definitions_1.baseType)(definition.type);
1928
- const selectionSet = node.selectionSet
1929
- ? selectionSetOfNode(type, node.selectionSet, variableDefinitions, fragments, fieldAccessor)
1930
- : undefined;
1931
- selection = new FieldSelection(new Field(definition, (0, values_1.argumentsFromAST)(definition.coordinate, node.arguments, definition), directives, (_a = node.alias) === null || _a === void 0 ? void 0 : _a.value), selectionSet);
1932
- break;
1933
- case graphql_1.Kind.INLINE_FRAGMENT:
1934
- const element = new FragmentElement(parentType, (_b = node.typeCondition) === null || _b === void 0 ? void 0 : _b.name.value, directives);
1935
- selection = new InlineFragmentSelection(element, selectionSetOfNode(element.typeCondition ? element.typeCondition : element.parentType, node.selectionSet, variableDefinitions, fragments, fieldAccessor));
1936
- break;
1937
- case graphql_1.Kind.FRAGMENT_SPREAD:
1938
- const fragmentName = node.name.value;
1939
- validate(fragments, () => `Cannot find fragment name "${fragmentName}" (no fragments were provided)`);
1940
- const fragment = fragments.get(fragmentName);
1941
- validate(fragment, () => `Cannot find fragment name "${fragmentName}" (provided fragments are: [${fragments.names().join(', ')}])`);
1942
- selection = new FragmentSpreadSelection(parentType, fragments, fragment, directives);
1943
- break;
1944
- }
1945
- return selection;
1946
- }
1947
- function operationFromDocument(schema, document, options) {
1948
- let operation;
1949
- const operationName = options === null || options === void 0 ? void 0 : options.operationName;
1950
- const fragments = new NamedFragments();
1951
- document.definitions.forEach(definition => {
1952
- switch (definition.kind) {
1953
- case graphql_1.Kind.OPERATION_DEFINITION:
1954
- validate(!operation || operationName, () => 'Must provide operation name if query contains multiple operations.');
1955
- if (!operationName || (definition.name && definition.name.value === operationName)) {
1956
- operation = definition;
1957
- }
1958
- break;
1959
- case graphql_1.Kind.FRAGMENT_DEFINITION:
1960
- const name = definition.name.value;
1961
- const typeName = definition.typeCondition.name.value;
1962
- const typeCondition = schema.type(typeName);
1963
- if (!typeCondition) {
1964
- throw error_1.ERRORS.INVALID_GRAPHQL.err(`Unknown type "${typeName}" for fragment "${name}"`, { nodes: definition });
1965
- }
1966
- if (!(0, definitions_1.isCompositeType)(typeCondition)) {
1967
- throw error_1.ERRORS.INVALID_GRAPHQL.err(`Invalid fragment "${name}" on non-composite type "${typeName}"`, { nodes: definition });
1968
- }
1969
- fragments.add(new NamedFragmentDefinition(schema, name, typeCondition, directivesOfNodes(schema, definition.directives)));
1970
- break;
1971
- }
1972
- });
1973
- validate(operation, () => operationName ? `Unknown operation named "${operationName}"` : 'No operation found in provided document.');
1974
- const variableDefinitions = operation.variableDefinitions
1975
- ? (0, definitions_1.variableDefinitionsFromAST)(schema, operation.variableDefinitions)
1976
- : new definitions_1.VariableDefinitions();
1977
- document.definitions.forEach(definition => {
1978
- switch (definition.kind) {
1979
- case graphql_1.Kind.FRAGMENT_DEFINITION:
1980
- const fragment = fragments.get(definition.name.value);
1981
- fragment.setSelectionSet(selectionSetOfNode(fragment.typeCondition, definition.selectionSet, variableDefinitions, fragments));
1982
- break;
1983
- }
1984
- });
1985
- fragments.validate(variableDefinitions);
1986
- return operationFromAST({ schema, operation, variableDefinitions, fragments, validateInput: options === null || options === void 0 ? void 0 : options.validate });
1987
- }
1988
- exports.operationFromDocument = operationFromDocument;
1989
- function operationFromAST({ schema, operation, variableDefinitions, fragments, validateInput, }) {
1990
- var _a;
1991
- const rootType = schema.schemaDefinition.root(operation.operation);
1992
- validate(rootType, () => `The schema has no "${operation.operation}" root type defined`);
1993
- return new Operation(schema, operation.operation, parseSelectionSet({
1994
- parentType: rootType.type,
1995
- source: operation.selectionSet,
1996
- variableDefinitions,
1997
- fragments: fragments.isEmpty() ? undefined : fragments,
1998
- validate: validateInput,
1999
- }), variableDefinitions, (_a = operation.name) === null || _a === void 0 ? void 0 : _a.value);
2000
- }
2001
- function parseOperation(schema, operation, options) {
2002
- return operationFromDocument(schema, (0, graphql_1.parse)(operation), options);
2003
- }
2004
- exports.parseOperation = parseOperation;
2005
- function parseSelectionSet({ parentType, source, variableDefinitions = new definitions_1.VariableDefinitions(), fragments, fieldAccessor, validate = true, }) {
2006
- const node = typeof source === 'string'
2007
- ? parseOperationAST(source.trim().startsWith('{') ? source : `{${source}}`).selectionSet
2008
- : source;
2009
- const selectionSet = selectionSetOfNode(parentType, node, variableDefinitions !== null && variableDefinitions !== void 0 ? variableDefinitions : new definitions_1.VariableDefinitions(), fragments, fieldAccessor);
2010
- if (validate)
2011
- selectionSet.validate(variableDefinitions);
2012
- return selectionSet;
2013
- }
2014
- exports.parseSelectionSet = parseSelectionSet;
2015
- function parseOperationAST(source) {
2016
- const parsed = (0, graphql_1.parse)(source);
2017
- validate(parsed.definitions.length === 1, () => 'Selections should contain a single definitions, found ' + parsed.definitions.length);
2018
- const def = parsed.definitions[0];
2019
- validate(def.kind === graphql_1.Kind.OPERATION_DEFINITION, () => 'Expected an operation definition but got a ' + def.kind);
2020
- return def;
2021
- }
2022
- function operationToDocument(operation) {
2023
- var _a;
2024
- const operationAST = {
2025
- kind: graphql_1.Kind.OPERATION_DEFINITION,
2026
- operation: operation.rootKind,
2027
- name: operation.name ? { kind: graphql_1.Kind.NAME, value: operation.name } : undefined,
2028
- selectionSet: operation.selectionSet.toSelectionSetNode(),
2029
- variableDefinitions: operation.variableDefinitions.toVariableDefinitionNodes(),
2030
- };
2031
- const fragmentASTs = operation.selectionSet.fragments
2032
- ? (_a = operation.selectionSet.fragments) === null || _a === void 0 ? void 0 : _a.toFragmentDefinitionNodes()
2033
- : [];
2034
- return {
2035
- kind: graphql_1.Kind.DOCUMENT,
2036
- definitions: [operationAST].concat(fragmentASTs),
2037
- };
2038
- }
2039
- exports.operationToDocument = operationToDocument;
2040
- //# sourceMappingURL=operations.js.map