@apollo/federation-internals 2.4.4 → 2.4.5

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 +6 -0
  2. package/dist/Subgraph.d.ts +1 -0
  3. package/dist/Subgraph.d.ts.map +1 -0
  4. package/dist/Subgraph.js +2 -0
  5. package/dist/Subgraph.js.map +1 -0
  6. package/dist/argumentCompositionStrategies.d.ts +34 -0
  7. package/dist/argumentCompositionStrategies.d.ts.map +1 -0
  8. package/dist/argumentCompositionStrategies.js +35 -0
  9. package/dist/argumentCompositionStrategies.js.map +1 -0
  10. package/dist/buildSchema.d.ts +10 -0
  11. package/dist/buildSchema.d.ts.map +1 -0
  12. package/dist/buildSchema.js +362 -0
  13. package/dist/buildSchema.js.map +1 -0
  14. package/dist/coreSpec.d.ts +127 -0
  15. package/dist/coreSpec.d.ts.map +1 -0
  16. package/dist/coreSpec.js +590 -0
  17. package/dist/coreSpec.js.map +1 -0
  18. package/dist/debug.d.ts +15 -0
  19. package/dist/debug.d.ts.map +1 -0
  20. package/dist/debug.js +122 -0
  21. package/dist/debug.js.map +1 -0
  22. package/dist/definitions.d.ts +663 -0
  23. package/dist/definitions.d.ts.map +1 -0
  24. package/dist/definitions.js +2841 -0
  25. package/dist/definitions.js.map +1 -0
  26. package/dist/directiveAndTypeSpecification.d.ts +67 -0
  27. package/dist/directiveAndTypeSpecification.d.ts.map +1 -0
  28. package/dist/directiveAndTypeSpecification.js +271 -0
  29. package/dist/directiveAndTypeSpecification.js.map +1 -0
  30. package/dist/error.d.ts +128 -0
  31. package/dist/error.d.ts.map +1 -0
  32. package/dist/error.js +315 -0
  33. package/dist/error.js.map +1 -0
  34. package/dist/extractSubgraphsFromSupergraph.d.ts +8 -0
  35. package/dist/extractSubgraphsFromSupergraph.d.ts.map +1 -0
  36. package/dist/extractSubgraphsFromSupergraph.js +576 -0
  37. package/dist/extractSubgraphsFromSupergraph.js.map +1 -0
  38. package/dist/federation.d.ts +175 -0
  39. package/dist/federation.d.ts.map +1 -0
  40. package/dist/federation.js +1414 -0
  41. package/dist/federation.js.map +1 -0
  42. package/dist/federationSpec.d.ts +25 -0
  43. package/dist/federationSpec.d.ts.map +1 -0
  44. package/dist/federationSpec.js +125 -0
  45. package/dist/federationSpec.js.map +1 -0
  46. package/dist/genErrorCodeDoc.d.ts +2 -0
  47. package/dist/genErrorCodeDoc.d.ts.map +1 -0
  48. package/dist/genErrorCodeDoc.js +61 -0
  49. package/dist/genErrorCodeDoc.js.map +1 -0
  50. package/dist/graphQLJSSchemaToAST.d.ts +8 -0
  51. package/dist/graphQLJSSchemaToAST.d.ts.map +1 -0
  52. package/dist/graphQLJSSchemaToAST.js +96 -0
  53. package/dist/graphQLJSSchemaToAST.js.map +1 -0
  54. package/dist/inaccessibleSpec.d.ts +18 -0
  55. package/dist/inaccessibleSpec.d.ts.map +1 -0
  56. package/dist/inaccessibleSpec.js +655 -0
  57. package/dist/inaccessibleSpec.js.map +1 -0
  58. package/dist/index.d.ts +24 -0
  59. package/dist/index.d.ts.map +1 -0
  60. package/dist/index.js +42 -0
  61. package/dist/index.js.map +1 -0
  62. package/dist/introspection.d.ts +6 -0
  63. package/dist/introspection.d.ts.map +1 -0
  64. package/dist/introspection.js +96 -0
  65. package/dist/introspection.js.map +1 -0
  66. package/dist/joinSpec.d.ts +51 -0
  67. package/dist/joinSpec.d.ts.map +1 -0
  68. package/dist/joinSpec.js +160 -0
  69. package/dist/joinSpec.js.map +1 -0
  70. package/dist/knownCoreFeatures.d.ts +5 -0
  71. package/dist/knownCoreFeatures.d.ts.map +1 -0
  72. package/dist/knownCoreFeatures.js +20 -0
  73. package/dist/knownCoreFeatures.js.map +1 -0
  74. package/dist/operations.d.ts +415 -0
  75. package/dist/operations.d.ts.map +1 -0
  76. package/dist/operations.js +2040 -0
  77. package/dist/operations.js.map +1 -0
  78. package/dist/precompute.d.ts +3 -0
  79. package/dist/precompute.d.ts.map +1 -0
  80. package/dist/precompute.js +54 -0
  81. package/dist/precompute.js.map +1 -0
  82. package/dist/print.d.ts +28 -0
  83. package/dist/print.d.ts.map +1 -0
  84. package/dist/print.js +299 -0
  85. package/dist/print.js.map +1 -0
  86. package/dist/schemaUpgrader.d.ts +121 -0
  87. package/dist/schemaUpgrader.d.ts.map +1 -0
  88. package/dist/schemaUpgrader.js +570 -0
  89. package/dist/schemaUpgrader.js.map +1 -0
  90. package/dist/suggestions.d.ts +3 -0
  91. package/dist/suggestions.d.ts.map +1 -0
  92. package/dist/suggestions.js +44 -0
  93. package/dist/suggestions.js.map +1 -0
  94. package/dist/supergraphs.d.ts +10 -0
  95. package/dist/supergraphs.d.ts.map +1 -0
  96. package/dist/supergraphs.js +76 -0
  97. package/dist/supergraphs.js.map +1 -0
  98. package/dist/tagSpec.d.ts +19 -0
  99. package/dist/tagSpec.d.ts.map +1 -0
  100. package/dist/tagSpec.js +66 -0
  101. package/dist/tagSpec.js.map +1 -0
  102. package/dist/types.d.ts +9 -0
  103. package/dist/types.d.ts.map +1 -0
  104. package/dist/types.js +64 -0
  105. package/dist/types.js.map +1 -0
  106. package/dist/utils.d.ts +64 -0
  107. package/dist/utils.d.ts.map +1 -0
  108. package/dist/utils.js +326 -0
  109. package/dist/utils.js.map +1 -0
  110. package/dist/validate.d.ts +4 -0
  111. package/dist/validate.d.ts.map +1 -0
  112. package/dist/validate.js +239 -0
  113. package/dist/validate.js.map +1 -0
  114. package/dist/validation/KnownTypeNamesInFederationRule.d.ts +4 -0
  115. package/dist/validation/KnownTypeNamesInFederationRule.d.ts.map +1 -0
  116. package/dist/validation/KnownTypeNamesInFederationRule.js +41 -0
  117. package/dist/validation/KnownTypeNamesInFederationRule.js.map +1 -0
  118. package/dist/values.d.ts +23 -0
  119. package/dist/values.d.ts.map +1 -0
  120. package/dist/values.js +580 -0
  121. package/dist/values.js.map +1 -0
  122. package/package.json +1 -1
  123. package/tsconfig.tsbuildinfo +1 -0
@@ -0,0 +1,2841 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CoreFeature = exports.defaultSchemaBlueprint = exports.SchemaBlueprint = exports.NamedSchemaElementWithType = exports.NamedSchemaElement = exports.SchemaElement = exports.Extension = exports.sourceASTs = exports.DirectiveTargetElement = exports.isLeafType = exports.typeFromAST = exports.typeToAST = exports.isTypeSystemDirectiveLocation = exports.typeSystemDirectiveLocations = exports.isExecutableDirectiveLocation = exports.executableDirectiveLocations = exports.isConditionalDirective = exports.supertypes = exports.runtimeTypesIntersects = exports.possibleRuntimeTypes = exports.isCompositeType = exports.isAbstractType = exports.isNullableType = exports.baseType = exports.filterTypesOfKind = exports.isTypeOfKind = exports.isInputType = exports.isOutputType = exports.isInputObjectType = exports.isUnionType = exports.isEnumType = exports.isInterfaceType = exports.isObjectType = exports.isIDType = exports.isBooleanType = exports.isFloatType = exports.isStringType = exports.isIntType = exports.isCustomScalarType = exports.isScalarType = exports.isNonNullType = exports.isListType = exports.isWrapperType = exports.isNamedType = exports.isSchemaRootType = exports.defaultRootName = exports.allSchemaRootKinds = exports.typenameFieldName = exports.ErrGraphQLAPISchemaValidationFailed = exports.ErrGraphQLValidationFailed = void 0;
4
+ exports.copyDirectiveDefinitionToSchema = exports.newNamedType = exports.variableDefinitionFromAST = exports.variableDefinitionsFromAST = exports.VariableDefinitions = exports.VariableDefinition = exports.isVariable = exports.VariableCollector = exports.Variable = exports.directiveApplicationsSubstraction = exports.isDirectiveApplicationsSubset = exports.sameDirectiveApplications = exports.sameDirectiveApplication = exports.Directive = exports.DirectiveDefinition = exports.EnumValue = exports.ArgumentDefinition = exports.InputFieldDefinition = exports.FieldDefinition = exports.NonNullType = exports.ListType = exports.InputObjectType = exports.EnumType = exports.UnionType = exports.UnionMember = exports.InterfaceType = exports.ObjectType = exports.InterfaceImplementation = exports.ScalarType = exports.SchemaDefinition = exports.RootType = exports.Schema = exports.CoreFeatures = void 0;
5
+ const graphql_1 = require("graphql");
6
+ const coreSpec_1 = require("./coreSpec");
7
+ const utils_1 = require("./utils");
8
+ const values_1 = require("./values");
9
+ const inaccessibleSpec_1 = require("./inaccessibleSpec");
10
+ const print_1 = require("./print");
11
+ const types_1 = require("./types");
12
+ const introspection_1 = require("./introspection");
13
+ const validate_1 = require("graphql/validation/validate");
14
+ const specifiedRules_1 = require("graphql/validation/specifiedRules");
15
+ const validate_2 = require("./validate");
16
+ const directiveAndTypeSpecification_1 = require("./directiveAndTypeSpecification");
17
+ const suggestions_1 = require("./suggestions");
18
+ const error_1 = require("./error");
19
+ const validationErrorCode = 'GraphQLValidationFailed';
20
+ const DEFAULT_VALIDATION_ERROR_MESSAGE = 'The schema is not a valid GraphQL schema.';
21
+ const ErrGraphQLValidationFailed = (causes, message = DEFAULT_VALIDATION_ERROR_MESSAGE) => (0, error_1.aggregateError)(validationErrorCode, message, causes);
22
+ exports.ErrGraphQLValidationFailed = ErrGraphQLValidationFailed;
23
+ const apiSchemaValidationErrorCode = 'GraphQLAPISchemaValidationFailed';
24
+ const ErrGraphQLAPISchemaValidationFailed = (causes) => (0, error_1.aggregateError)(apiSchemaValidationErrorCode, 'The supergraph schema failed to produce a valid API schema', causes);
25
+ exports.ErrGraphQLAPISchemaValidationFailed = ErrGraphQLAPISchemaValidationFailed;
26
+ exports.typenameFieldName = '__typename';
27
+ exports.allSchemaRootKinds = ['query', 'mutation', 'subscription'];
28
+ function defaultRootName(rootKind) {
29
+ return rootKind.charAt(0).toUpperCase() + rootKind.slice(1);
30
+ }
31
+ exports.defaultRootName = defaultRootName;
32
+ function checkDefaultSchemaRoot(type) {
33
+ if (type.kind !== 'ObjectType') {
34
+ return undefined;
35
+ }
36
+ switch (type.name) {
37
+ case 'Query': return 'query';
38
+ case 'Mutation': return 'mutation';
39
+ case 'Subscription': return 'subscription';
40
+ default: return undefined;
41
+ }
42
+ }
43
+ function isSchemaRootType(type) {
44
+ return isObjectType(type) && type.isRootType();
45
+ }
46
+ exports.isSchemaRootType = isSchemaRootType;
47
+ function isNamedType(type) {
48
+ return type instanceof BaseNamedType;
49
+ }
50
+ exports.isNamedType = isNamedType;
51
+ function isWrapperType(type) {
52
+ return isListType(type) || isNonNullType(type);
53
+ }
54
+ exports.isWrapperType = isWrapperType;
55
+ function isListType(type) {
56
+ return type.kind == 'ListType';
57
+ }
58
+ exports.isListType = isListType;
59
+ function isNonNullType(type) {
60
+ return type.kind == 'NonNullType';
61
+ }
62
+ exports.isNonNullType = isNonNullType;
63
+ function isScalarType(type) {
64
+ return type.kind == 'ScalarType';
65
+ }
66
+ exports.isScalarType = isScalarType;
67
+ function isCustomScalarType(type) {
68
+ return isScalarType(type) && !graphQLBuiltInTypes.includes(type.name);
69
+ }
70
+ exports.isCustomScalarType = isCustomScalarType;
71
+ function isIntType(type) {
72
+ return type === type.schema().intType();
73
+ }
74
+ exports.isIntType = isIntType;
75
+ function isStringType(type) {
76
+ return type === type.schema().stringType();
77
+ }
78
+ exports.isStringType = isStringType;
79
+ function isFloatType(type) {
80
+ return type === type.schema().floatType();
81
+ }
82
+ exports.isFloatType = isFloatType;
83
+ function isBooleanType(type) {
84
+ return type === type.schema().booleanType();
85
+ }
86
+ exports.isBooleanType = isBooleanType;
87
+ function isIDType(type) {
88
+ return type === type.schema().idType();
89
+ }
90
+ exports.isIDType = isIDType;
91
+ function isObjectType(type) {
92
+ return type.kind == 'ObjectType';
93
+ }
94
+ exports.isObjectType = isObjectType;
95
+ function isInterfaceType(type) {
96
+ return type.kind == 'InterfaceType';
97
+ }
98
+ exports.isInterfaceType = isInterfaceType;
99
+ function isEnumType(type) {
100
+ return type.kind == 'EnumType';
101
+ }
102
+ exports.isEnumType = isEnumType;
103
+ function isUnionType(type) {
104
+ return type.kind == 'UnionType';
105
+ }
106
+ exports.isUnionType = isUnionType;
107
+ function isInputObjectType(type) {
108
+ return type.kind == 'InputObjectType';
109
+ }
110
+ exports.isInputObjectType = isInputObjectType;
111
+ function isOutputType(type) {
112
+ switch (baseType(type).kind) {
113
+ case 'ScalarType':
114
+ case 'ObjectType':
115
+ case 'UnionType':
116
+ case 'EnumType':
117
+ case 'InterfaceType':
118
+ return true;
119
+ default:
120
+ return false;
121
+ }
122
+ }
123
+ exports.isOutputType = isOutputType;
124
+ function isInputType(type) {
125
+ switch (baseType(type).kind) {
126
+ case 'ScalarType':
127
+ case 'EnumType':
128
+ case 'InputObjectType':
129
+ return true;
130
+ default:
131
+ return false;
132
+ }
133
+ }
134
+ exports.isInputType = isInputType;
135
+ function isTypeOfKind(type, kind) {
136
+ return type.kind === kind;
137
+ }
138
+ exports.isTypeOfKind = isTypeOfKind;
139
+ function filterTypesOfKind(types, kind) {
140
+ return types.reduce((acc, type) => {
141
+ if (isTypeOfKind(type, kind)) {
142
+ acc.push(type);
143
+ }
144
+ return acc;
145
+ }, []);
146
+ }
147
+ exports.filterTypesOfKind = filterTypesOfKind;
148
+ function baseType(type) {
149
+ return isWrapperType(type) ? type.baseType() : type;
150
+ }
151
+ exports.baseType = baseType;
152
+ function isNullableType(type) {
153
+ return !isNonNullType(type);
154
+ }
155
+ exports.isNullableType = isNullableType;
156
+ function isAbstractType(type) {
157
+ return isInterfaceType(type) || isUnionType(type);
158
+ }
159
+ exports.isAbstractType = isAbstractType;
160
+ function isCompositeType(type) {
161
+ return isObjectType(type) || isInterfaceType(type) || isUnionType(type);
162
+ }
163
+ exports.isCompositeType = isCompositeType;
164
+ function possibleRuntimeTypes(type) {
165
+ switch (type.kind) {
166
+ case 'InterfaceType': return type.possibleRuntimeTypes();
167
+ case 'UnionType': return type.types();
168
+ case 'ObjectType': return [type];
169
+ }
170
+ }
171
+ exports.possibleRuntimeTypes = possibleRuntimeTypes;
172
+ function runtimeTypesIntersects(t1, t2) {
173
+ const rt1 = possibleRuntimeTypes(t1);
174
+ const rt2 = possibleRuntimeTypes(t2);
175
+ for (const obj1 of rt1) {
176
+ if (rt2.some(obj2 => obj1.name === obj2.name)) {
177
+ return true;
178
+ }
179
+ }
180
+ return false;
181
+ }
182
+ exports.runtimeTypesIntersects = runtimeTypesIntersects;
183
+ function supertypes(type) {
184
+ switch (type.kind) {
185
+ case 'InterfaceType': return type.interfaces();
186
+ case 'UnionType': return [];
187
+ case 'ObjectType': return type.interfaces().concat(type.unionsWhereMember());
188
+ }
189
+ }
190
+ exports.supertypes = supertypes;
191
+ function isConditionalDirective(directive) {
192
+ return ['include', 'skip'].includes(directive.name);
193
+ }
194
+ exports.isConditionalDirective = isConditionalDirective;
195
+ exports.executableDirectiveLocations = [
196
+ graphql_1.DirectiveLocation.QUERY,
197
+ graphql_1.DirectiveLocation.MUTATION,
198
+ graphql_1.DirectiveLocation.SUBSCRIPTION,
199
+ graphql_1.DirectiveLocation.FIELD,
200
+ graphql_1.DirectiveLocation.FRAGMENT_DEFINITION,
201
+ graphql_1.DirectiveLocation.FRAGMENT_SPREAD,
202
+ graphql_1.DirectiveLocation.INLINE_FRAGMENT,
203
+ graphql_1.DirectiveLocation.VARIABLE_DEFINITION,
204
+ ];
205
+ const executableDirectiveLocationsSet = new Set(exports.executableDirectiveLocations);
206
+ function isExecutableDirectiveLocation(loc) {
207
+ return executableDirectiveLocationsSet.has(loc);
208
+ }
209
+ exports.isExecutableDirectiveLocation = isExecutableDirectiveLocation;
210
+ exports.typeSystemDirectiveLocations = [
211
+ graphql_1.DirectiveLocation.SCHEMA,
212
+ graphql_1.DirectiveLocation.SCALAR,
213
+ graphql_1.DirectiveLocation.OBJECT,
214
+ graphql_1.DirectiveLocation.FIELD_DEFINITION,
215
+ graphql_1.DirectiveLocation.ARGUMENT_DEFINITION,
216
+ graphql_1.DirectiveLocation.INTERFACE,
217
+ graphql_1.DirectiveLocation.UNION,
218
+ graphql_1.DirectiveLocation.ENUM,
219
+ graphql_1.DirectiveLocation.ENUM_VALUE,
220
+ graphql_1.DirectiveLocation.INPUT_OBJECT,
221
+ graphql_1.DirectiveLocation.INPUT_FIELD_DEFINITION,
222
+ ];
223
+ const typeSystemDirectiveLocationsSet = new Set(exports.typeSystemDirectiveLocations);
224
+ function isTypeSystemDirectiveLocation(loc) {
225
+ return typeSystemDirectiveLocationsSet.has(loc);
226
+ }
227
+ exports.isTypeSystemDirectiveLocation = isTypeSystemDirectiveLocation;
228
+ function typeToAST(type) {
229
+ switch (type.kind) {
230
+ case 'ListType':
231
+ return {
232
+ kind: graphql_1.Kind.LIST_TYPE,
233
+ type: typeToAST(type.ofType)
234
+ };
235
+ case 'NonNullType':
236
+ return {
237
+ kind: graphql_1.Kind.NON_NULL_TYPE,
238
+ type: typeToAST(type.ofType)
239
+ };
240
+ default:
241
+ return {
242
+ kind: graphql_1.Kind.NAMED_TYPE,
243
+ name: { kind: graphql_1.Kind.NAME, value: type.name }
244
+ };
245
+ }
246
+ }
247
+ exports.typeToAST = typeToAST;
248
+ function typeFromAST(schema, node) {
249
+ switch (node.kind) {
250
+ case graphql_1.Kind.LIST_TYPE:
251
+ return new ListType(typeFromAST(schema, node.type));
252
+ case graphql_1.Kind.NON_NULL_TYPE:
253
+ return new NonNullType(typeFromAST(schema, node.type));
254
+ default:
255
+ const type = schema.type(node.name.value);
256
+ if (!type) {
257
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Unknown type "${node.name.value}"`, { nodes: node });
258
+ }
259
+ return type;
260
+ }
261
+ }
262
+ exports.typeFromAST = typeFromAST;
263
+ function isLeafType(type) {
264
+ return isScalarType(type) || isEnumType(type);
265
+ }
266
+ exports.isLeafType = isLeafType;
267
+ class DirectiveTargetElement {
268
+ constructor(_schema, directives = []) {
269
+ this._schema = _schema;
270
+ this.appliedDirectives = directives.map((d) => this.attachDirective(d));
271
+ }
272
+ schema() {
273
+ return this._schema;
274
+ }
275
+ attachDirective(directive) {
276
+ const toAdd = directive.isAttached()
277
+ ? new Directive(directive.name, directive.arguments())
278
+ : directive;
279
+ Element.prototype['setParent'].call(toAdd, this);
280
+ return toAdd;
281
+ }
282
+ appliedDirectivesOf(nameOrDefinition) {
283
+ const directiveName = typeof nameOrDefinition === 'string' ? nameOrDefinition : nameOrDefinition.name;
284
+ return this.appliedDirectives.filter(d => d.name == directiveName);
285
+ }
286
+ hasAppliedDirective(nameOrDefinition) {
287
+ const directiveName = typeof nameOrDefinition === 'string' ? nameOrDefinition : nameOrDefinition.name;
288
+ return this.appliedDirectives.some(d => d.name == directiveName);
289
+ }
290
+ appliedDirectivesToDirectiveNodes() {
291
+ if (this.appliedDirectives.length == 0) {
292
+ return undefined;
293
+ }
294
+ return this.appliedDirectives.map(directive => {
295
+ return {
296
+ kind: graphql_1.Kind.DIRECTIVE,
297
+ name: {
298
+ kind: graphql_1.Kind.NAME,
299
+ value: directive.name,
300
+ },
301
+ arguments: directive.argumentsToAST()
302
+ };
303
+ });
304
+ }
305
+ appliedDirectivesToString() {
306
+ return this.appliedDirectives.length == 0
307
+ ? ''
308
+ : ' ' + this.appliedDirectives.join(' ');
309
+ }
310
+ collectVariablesInAppliedDirectives(collector) {
311
+ for (const applied of this.appliedDirectives) {
312
+ collector.collectInArguments(applied.arguments());
313
+ }
314
+ }
315
+ }
316
+ exports.DirectiveTargetElement = DirectiveTargetElement;
317
+ function sourceASTs(...elts) {
318
+ return elts.map(elt => elt === null || elt === void 0 ? void 0 : elt.sourceAST).filter((elt) => elt !== undefined);
319
+ }
320
+ exports.sourceASTs = sourceASTs;
321
+ class Element {
322
+ schema() {
323
+ const schema = this.schemaInternal();
324
+ (0, utils_1.assert)(schema, 'requested schema does not exist. Probably because the element is unattached');
325
+ return schema;
326
+ }
327
+ schemaInternal() {
328
+ if (!this._parent) {
329
+ return undefined;
330
+ }
331
+ else if (this._parent instanceof Schema) {
332
+ return this._parent;
333
+ }
334
+ else if (this._parent instanceof SchemaElement) {
335
+ return this._parent.schemaInternal();
336
+ }
337
+ else if (this._parent instanceof DirectiveTargetElement) {
338
+ return this._parent.schema();
339
+ }
340
+ (0, utils_1.assert)(false, 'unreachable code. parent is of unknown type');
341
+ }
342
+ get parent() {
343
+ (0, utils_1.assert)(this._parent, 'trying to access non-existent parent');
344
+ return this._parent;
345
+ }
346
+ isAttached() {
347
+ return !!this._parent;
348
+ }
349
+ setParent(parent) {
350
+ (0, utils_1.assert)(!this._parent, "Cannot set parent of an already attached element");
351
+ this._parent = parent;
352
+ this.onAttached();
353
+ }
354
+ onAttached() {
355
+ }
356
+ checkUpdate() {
357
+ (0, utils_1.assert)(this.isAttached(), () => `Cannot modify detached element ${this}`);
358
+ }
359
+ }
360
+ class Extension {
361
+ get extendedElement() {
362
+ return this._extendedElement;
363
+ }
364
+ setExtendedElement(element) {
365
+ (0, utils_1.assert)(!this._extendedElement, "Cannot attached already attached extension");
366
+ this._extendedElement = element;
367
+ }
368
+ }
369
+ exports.Extension = Extension;
370
+ class SchemaElement extends Element {
371
+ addUnappliedDirective({ nameOrDef, args, extension, directive }) {
372
+ const toAdd = {
373
+ nameOrDef,
374
+ args: args !== null && args !== void 0 ? args : {},
375
+ extension,
376
+ directive,
377
+ };
378
+ if (this._unappliedDirectives) {
379
+ this._unappliedDirectives.push(toAdd);
380
+ }
381
+ else {
382
+ this._unappliedDirectives = [toAdd];
383
+ }
384
+ }
385
+ processUnappliedDirectives() {
386
+ var _a;
387
+ for (const { nameOrDef, args, extension, directive } of (_a = this._unappliedDirectives) !== null && _a !== void 0 ? _a : []) {
388
+ const d = this.applyDirective(nameOrDef, args);
389
+ d.setOfExtension(extension);
390
+ d.sourceAST = directive;
391
+ }
392
+ this._unappliedDirectives = undefined;
393
+ }
394
+ get appliedDirectives() {
395
+ var _a;
396
+ return (_a = this._appliedDirectives) !== null && _a !== void 0 ? _a : [];
397
+ }
398
+ appliedDirectivesOf(nameOrDefinition) {
399
+ const directiveName = typeof nameOrDefinition === 'string' ? nameOrDefinition : nameOrDefinition.name;
400
+ return this.appliedDirectives.filter(d => d.name == directiveName);
401
+ }
402
+ hasAppliedDirective(nameOrDefinition) {
403
+ return (typeof nameOrDefinition === 'string'
404
+ ? this.appliedDirectivesOf(nameOrDefinition)
405
+ : this.appliedDirectivesOf(nameOrDefinition)).length !== 0;
406
+ }
407
+ applyDirective(nameOrDef, args, asFirstDirective = false) {
408
+ var _a;
409
+ let name;
410
+ if (typeof nameOrDef === 'string') {
411
+ this.checkUpdate();
412
+ const def = (_a = this.schema().directive(nameOrDef)) !== null && _a !== void 0 ? _a : this.schema().blueprint.onMissingDirectiveDefinition(this.schema(), nameOrDef, args);
413
+ if (!def) {
414
+ throw this.schema().blueprint.onGraphQLJSValidationError(this.schema(), error_1.ERRORS.INVALID_GRAPHQL.err(`Unknown directive "@${nameOrDef}".`));
415
+ }
416
+ if (Array.isArray(def)) {
417
+ throw (0, exports.ErrGraphQLValidationFailed)(def);
418
+ }
419
+ name = nameOrDef;
420
+ }
421
+ else {
422
+ this.checkUpdate(nameOrDef);
423
+ name = nameOrDef.name;
424
+ }
425
+ const toAdd = new Directive(name, args !== null && args !== void 0 ? args : Object.create(null));
426
+ Element.prototype['setParent'].call(toAdd, this);
427
+ if (this._appliedDirectives) {
428
+ if (asFirstDirective) {
429
+ this._appliedDirectives.unshift(toAdd);
430
+ }
431
+ else {
432
+ this._appliedDirectives.push(toAdd);
433
+ }
434
+ }
435
+ else {
436
+ this._appliedDirectives = [toAdd];
437
+ }
438
+ DirectiveDefinition.prototype['addReferencer'].call(toAdd.definition, toAdd);
439
+ this.onModification();
440
+ return toAdd;
441
+ }
442
+ removeAppliedDirectives() {
443
+ if (!this._appliedDirectives) {
444
+ return;
445
+ }
446
+ const applied = this._appliedDirectives.concat();
447
+ applied.forEach(d => d.remove());
448
+ }
449
+ onModification() {
450
+ const schema = this.schemaInternal();
451
+ if (schema) {
452
+ Schema.prototype['onModification'].call(schema);
453
+ }
454
+ }
455
+ isElementBuiltIn() {
456
+ return false;
457
+ }
458
+ removeTypeReferenceInternal(type) {
459
+ this.removeTypeReference(type);
460
+ }
461
+ checkRemoval() {
462
+ (0, utils_1.assert)(!this.isElementBuiltIn() || Schema.prototype['canModifyBuiltIn'].call(this.schema()), () => `Cannot modify built-in ${this}`);
463
+ }
464
+ checkUpdate(addedElement) {
465
+ super.checkUpdate();
466
+ if (!Schema.prototype['canModifyBuiltIn'].call(this.schema())) {
467
+ let thisElement = this;
468
+ while (thisElement && thisElement instanceof SchemaElement) {
469
+ (0, utils_1.assert)(!thisElement.isElementBuiltIn(), () => `Cannot modify built-in (or part of built-in) ${this}`);
470
+ thisElement = thisElement.parent;
471
+ }
472
+ }
473
+ if (addedElement && addedElement.isAttached()) {
474
+ const thatSchema = addedElement.schema();
475
+ (0, utils_1.assert)(!thatSchema || thatSchema === this.schema(), () => `Cannot add element ${addedElement} to ${this} as it is attached to another schema`);
476
+ }
477
+ }
478
+ }
479
+ exports.SchemaElement = SchemaElement;
480
+ class NamedSchemaElement extends SchemaElement {
481
+ constructor(name) {
482
+ super();
483
+ this._name = name;
484
+ }
485
+ get name() {
486
+ return this._name;
487
+ }
488
+ }
489
+ exports.NamedSchemaElement = NamedSchemaElement;
490
+ class BaseNamedType extends NamedSchemaElement {
491
+ constructor(name, isBuiltIn = false) {
492
+ super(name);
493
+ this.isBuiltIn = isBuiltIn;
494
+ this.preserveEmptyDefinition = false;
495
+ }
496
+ addReferencer(referencer) {
497
+ if (this._referencers) {
498
+ if (!this._referencers.includes(referencer)) {
499
+ this._referencers.push(referencer);
500
+ }
501
+ }
502
+ else {
503
+ this._referencers = [referencer];
504
+ }
505
+ }
506
+ removeReferencer(referencer) {
507
+ if (this._referencers) {
508
+ (0, utils_1.removeArrayElement)(referencer, this._referencers);
509
+ }
510
+ }
511
+ get coordinate() {
512
+ return this.name;
513
+ }
514
+ *allChildElements() {
515
+ }
516
+ extensions() {
517
+ var _a;
518
+ return (_a = this._extensions) !== null && _a !== void 0 ? _a : [];
519
+ }
520
+ hasExtension(extension) {
521
+ var _a, _b;
522
+ return (_b = (_a = this._extensions) === null || _a === void 0 ? void 0 : _a.includes(extension)) !== null && _b !== void 0 ? _b : false;
523
+ }
524
+ newExtension() {
525
+ return this.addExtension(new Extension());
526
+ }
527
+ addExtension(extension) {
528
+ this.checkUpdate();
529
+ if (this.hasExtension(extension)) {
530
+ return extension;
531
+ }
532
+ (0, utils_1.assert)(!extension.extendedElement, () => `Cannot add extension to type ${this}: it is already added to another type`);
533
+ if (this._extensions) {
534
+ this._extensions.push(extension);
535
+ }
536
+ else {
537
+ this._extensions = [extension];
538
+ }
539
+ Extension.prototype['setExtendedElement'].call(extension, this);
540
+ this.onModification();
541
+ return extension;
542
+ }
543
+ removeExtensions() {
544
+ if (!this._extensions) {
545
+ return;
546
+ }
547
+ this._extensions = undefined;
548
+ for (const directive of this.appliedDirectives) {
549
+ directive.removeOfExtension();
550
+ }
551
+ this.removeInnerElementsExtensions();
552
+ }
553
+ isIntrospectionType() {
554
+ return (0, introspection_1.isIntrospectionName)(this.name);
555
+ }
556
+ hasExtensionElements() {
557
+ return !!this._extensions;
558
+ }
559
+ hasNonExtensionElements() {
560
+ return this.preserveEmptyDefinition
561
+ || this.appliedDirectives.some(d => d.ofExtension() === undefined)
562
+ || this.hasNonExtensionInnerElements();
563
+ }
564
+ isElementBuiltIn() {
565
+ return this.isBuiltIn;
566
+ }
567
+ rename(newName) {
568
+ this.checkUpdate();
569
+ const oldName = this._name;
570
+ this._name = newName;
571
+ Schema.prototype['renameTypeInternal'].call(this._parent, oldName, newName);
572
+ this.onModification();
573
+ }
574
+ remove() {
575
+ var _a, _b;
576
+ if (!this._parent) {
577
+ return [];
578
+ }
579
+ this.checkRemoval();
580
+ this.onModification();
581
+ this.sourceAST = undefined;
582
+ this.removeAppliedDirectives();
583
+ this.removeInnerElements();
584
+ const toReturn = (_b = (_a = this._referencers) === null || _a === void 0 ? void 0 : _a.map(r => {
585
+ SchemaElement.prototype['removeTypeReferenceInternal'].call(r, this);
586
+ return r;
587
+ })) !== null && _b !== void 0 ? _b : [];
588
+ this._referencers = undefined;
589
+ Schema.prototype['removeTypeInternal'].call(this._parent, this);
590
+ this._parent = undefined;
591
+ return toReturn;
592
+ }
593
+ removeRecursive() {
594
+ this.remove().forEach(ref => this.removeReferenceRecursive(ref));
595
+ }
596
+ referencers() {
597
+ var _a;
598
+ return (_a = this._referencers) !== null && _a !== void 0 ? _a : [];
599
+ }
600
+ isReferenced() {
601
+ return !!this._referencers;
602
+ }
603
+ toString() {
604
+ return this.name;
605
+ }
606
+ }
607
+ class NamedSchemaElementWithType extends NamedSchemaElement {
608
+ get type() {
609
+ return this._type;
610
+ }
611
+ set type(type) {
612
+ if (type) {
613
+ this.checkUpdate(type);
614
+ }
615
+ else {
616
+ this.checkRemoval();
617
+ }
618
+ if (this._type) {
619
+ removeReferenceToType(this, this._type);
620
+ }
621
+ this._type = type;
622
+ if (type) {
623
+ addReferenceToType(this, type);
624
+ }
625
+ }
626
+ removeTypeReference(type) {
627
+ (0, utils_1.assert)(this._type && baseType(this._type) === type, () => `Cannot remove reference to type ${type} on ${this} as its type is ${this._type}`);
628
+ this._type = undefined;
629
+ }
630
+ }
631
+ exports.NamedSchemaElementWithType = NamedSchemaElementWithType;
632
+ class BaseExtensionMember extends Element {
633
+ ofExtension() {
634
+ return this._extension;
635
+ }
636
+ removeOfExtension() {
637
+ this._extension = undefined;
638
+ }
639
+ setOfExtension(extension) {
640
+ var _a;
641
+ this.checkUpdate();
642
+ (0, utils_1.assert)(!extension || ((_a = this._parent) === null || _a === void 0 ? void 0 : _a.hasExtension(extension)), () => `Cannot set object as part of the provided extension: it is not an extension of parent ${this.parent}`);
643
+ this._extension = extension;
644
+ }
645
+ remove() {
646
+ this.removeInner();
647
+ Schema.prototype['onModification'].call(this.schema());
648
+ this._extension = undefined;
649
+ this._parent = undefined;
650
+ }
651
+ }
652
+ class SchemaBlueprint {
653
+ onMissingDirectiveDefinition(_schema, _name, _args) {
654
+ return undefined;
655
+ }
656
+ onDirectiveDefinitionAndSchemaParsed(_) {
657
+ return [];
658
+ }
659
+ ignoreParsedField(_type, _fieldName) {
660
+ return false;
661
+ }
662
+ onConstructed(_) {
663
+ }
664
+ onAddedCoreFeature(_schema, _feature) {
665
+ }
666
+ onInvalidation(_) {
667
+ }
668
+ onValidation(_schema) {
669
+ return [];
670
+ }
671
+ validationRules() {
672
+ return specifiedRules_1.specifiedSDLRules;
673
+ }
674
+ onGraphQLJSValidationError(schema, error) {
675
+ var _a;
676
+ const matcher = /^Unknown directive "@(?<directive>[_A-Za-z][_0-9A-Za-z]*)"\.$/.exec(error.message);
677
+ const name = (_a = matcher === null || matcher === void 0 ? void 0 : matcher.groups) === null || _a === void 0 ? void 0 : _a.directive;
678
+ if (!name) {
679
+ return error;
680
+ }
681
+ const allDefinedDirectiveNames = schema.allDirectives().map((d) => d.name);
682
+ const suggestions = (0, suggestions_1.suggestionList)(name, allDefinedDirectiveNames);
683
+ if (suggestions.length === 0) {
684
+ return this.onUnknownDirectiveValidationError(schema, name, error);
685
+ }
686
+ else {
687
+ return (0, error_1.withModifiedErrorMessage)(error, `${error.message}${(0, suggestions_1.didYouMean)(suggestions.map((s) => '@' + s))}`);
688
+ }
689
+ }
690
+ onUnknownDirectiveValidationError(_schema, _unknownDirectiveName, error) {
691
+ return error;
692
+ }
693
+ applyDirectivesAfterParsing() {
694
+ return false;
695
+ }
696
+ }
697
+ exports.SchemaBlueprint = SchemaBlueprint;
698
+ exports.defaultSchemaBlueprint = new SchemaBlueprint();
699
+ class CoreFeature {
700
+ constructor(url, nameInSchema, directive, imports, purpose) {
701
+ this.url = url;
702
+ this.nameInSchema = nameInSchema;
703
+ this.directive = directive;
704
+ this.imports = imports;
705
+ this.purpose = purpose;
706
+ }
707
+ isFeatureDefinition(element) {
708
+ const importName = element.kind === 'DirectiveDefinition'
709
+ ? '@' + element.name
710
+ : element.name;
711
+ return element.name.startsWith(this.nameInSchema + '__')
712
+ || (element.kind === 'DirectiveDefinition' && element.name === this.nameInSchema)
713
+ || !!this.imports.find((i) => { var _a; return importName === ((_a = i.as) !== null && _a !== void 0 ? _a : i.name); });
714
+ }
715
+ directiveNameInSchema(name) {
716
+ var _a, _b;
717
+ const elementImport = this.imports.find((i) => i.name.charAt(0) === '@' && i.name.slice(1) === name);
718
+ return elementImport
719
+ ? ((_b = (_a = elementImport.as) === null || _a === void 0 ? void 0 : _a.slice(1)) !== null && _b !== void 0 ? _b : name)
720
+ : (name === this.url.name
721
+ ? this.nameInSchema
722
+ : this.nameInSchema + '__' + name);
723
+ }
724
+ typeNameInSchema(name) {
725
+ var _a;
726
+ const elementImport = this.imports.find((i) => i.name === name);
727
+ return elementImport ? ((_a = elementImport.as) !== null && _a !== void 0 ? _a : name) : this.nameInSchema + '__' + name;
728
+ }
729
+ }
730
+ exports.CoreFeature = CoreFeature;
731
+ class CoreFeatures {
732
+ constructor(coreItself) {
733
+ this.coreItself = coreItself;
734
+ this.byAlias = new Map();
735
+ this.byIdentity = new Map();
736
+ this.add(coreItself);
737
+ const coreDef = (0, coreSpec_1.findCoreSpecVersion)(coreItself.url);
738
+ if (!coreDef) {
739
+ throw error_1.ERRORS.UNKNOWN_LINK_VERSION.err(`Schema uses unknown version ${coreItself.url.version} of the ${coreItself.url.name} spec`);
740
+ }
741
+ this.coreDefinition = coreDef;
742
+ }
743
+ getByIdentity(identity) {
744
+ return this.byIdentity.get(identity);
745
+ }
746
+ allFeatures() {
747
+ return this.byIdentity.values();
748
+ }
749
+ removeFeature(featureIdentity) {
750
+ const feature = this.byIdentity.get(featureIdentity);
751
+ if (feature) {
752
+ this.byIdentity.delete(featureIdentity);
753
+ this.byAlias.delete(feature.nameInSchema);
754
+ }
755
+ }
756
+ maybeAddFeature(directive) {
757
+ var _a, _b;
758
+ if (((_a = directive.definition) === null || _a === void 0 ? void 0 : _a.name) !== this.coreItself.nameInSchema) {
759
+ return undefined;
760
+ }
761
+ const typedDirective = directive;
762
+ const args = typedDirective.arguments();
763
+ const url = this.coreDefinition.extractFeatureUrl(args);
764
+ const existing = this.byIdentity.get(url.identity);
765
+ if (existing) {
766
+ throw error_1.ERRORS.INVALID_LINK_DIRECTIVE_USAGE.err(`Duplicate inclusion of feature ${url.identity}`);
767
+ }
768
+ const imports = (0, coreSpec_1.extractCoreFeatureImports)(url, typedDirective);
769
+ const feature = new CoreFeature(url, (_b = args.as) !== null && _b !== void 0 ? _b : url.name, directive, imports, args.for);
770
+ this.add(feature);
771
+ directive.schema().blueprint.onAddedCoreFeature(directive.schema(), feature);
772
+ return feature;
773
+ }
774
+ add(feature) {
775
+ this.byAlias.set(feature.nameInSchema, feature);
776
+ this.byIdentity.set(feature.url.identity, feature);
777
+ }
778
+ sourceFeature(element) {
779
+ var _a, _b;
780
+ const isDirective = element instanceof DirectiveDefinition || element instanceof Directive;
781
+ const splitted = element.name.split('__');
782
+ if (splitted.length > 1) {
783
+ const feature = this.byAlias.get(splitted[0]);
784
+ return feature ? {
785
+ feature,
786
+ nameInFeature: splitted[1],
787
+ isImported: false,
788
+ } : undefined;
789
+ }
790
+ else {
791
+ const importName = isDirective ? '@' + element.name : element.name;
792
+ const allFeatures = [this.coreItself, ...this.byIdentity.values()];
793
+ for (const feature of allFeatures) {
794
+ for (const { as, name } of feature.imports) {
795
+ if ((as !== null && as !== void 0 ? as : name) === importName) {
796
+ return {
797
+ feature,
798
+ nameInFeature: name.slice(1),
799
+ isImported: true,
800
+ };
801
+ }
802
+ }
803
+ }
804
+ const directFeature = this.byAlias.get(element.name);
805
+ if (directFeature && isDirective) {
806
+ return {
807
+ feature: directFeature,
808
+ nameInFeature: (_b = (_a = directFeature.imports.find(imp => imp.as === `@${element.name}`)) === null || _a === void 0 ? void 0 : _a.name.slice(1)) !== null && _b !== void 0 ? _b : element.name,
809
+ isImported: true,
810
+ };
811
+ }
812
+ return undefined;
813
+ }
814
+ }
815
+ }
816
+ exports.CoreFeatures = CoreFeatures;
817
+ const graphQLBuiltInTypes = ['Int', 'Float', 'String', 'Boolean', 'ID'];
818
+ const graphQLBuiltInTypesSpecifications = graphQLBuiltInTypes.map((name) => (0, directiveAndTypeSpecification_1.createScalarTypeSpecification)({ name }));
819
+ const graphQLBuiltInDirectivesSpecifications = [
820
+ (0, directiveAndTypeSpecification_1.createDirectiveSpecification)({
821
+ name: 'include',
822
+ locations: [graphql_1.DirectiveLocation.FIELD, graphql_1.DirectiveLocation.FRAGMENT_SPREAD, graphql_1.DirectiveLocation.INLINE_FRAGMENT],
823
+ args: [{ name: 'if', type: (schema) => new NonNullType(schema.booleanType()) }],
824
+ }),
825
+ (0, directiveAndTypeSpecification_1.createDirectiveSpecification)({
826
+ name: 'skip',
827
+ locations: [graphql_1.DirectiveLocation.FIELD, graphql_1.DirectiveLocation.FRAGMENT_SPREAD, graphql_1.DirectiveLocation.INLINE_FRAGMENT],
828
+ args: [{ name: 'if', type: (schema) => new NonNullType(schema.booleanType()) }],
829
+ }),
830
+ (0, directiveAndTypeSpecification_1.createDirectiveSpecification)({
831
+ name: 'deprecated',
832
+ locations: [graphql_1.DirectiveLocation.FIELD_DEFINITION, graphql_1.DirectiveLocation.ENUM_VALUE, graphql_1.DirectiveLocation.ARGUMENT_DEFINITION, graphql_1.DirectiveLocation.INPUT_FIELD_DEFINITION],
833
+ args: [{ name: 'reason', type: (schema) => schema.stringType(), defaultValue: 'No longer supported' }],
834
+ }),
835
+ (0, directiveAndTypeSpecification_1.createDirectiveSpecification)({
836
+ name: 'specifiedBy',
837
+ locations: [graphql_1.DirectiveLocation.SCALAR],
838
+ args: [{ name: 'url', type: (schema) => new NonNullType(schema.stringType()) }],
839
+ }),
840
+ (0, directiveAndTypeSpecification_1.createDirectiveSpecification)({
841
+ name: 'defer',
842
+ locations: [graphql_1.DirectiveLocation.FRAGMENT_SPREAD, graphql_1.DirectiveLocation.INLINE_FRAGMENT],
843
+ args: [
844
+ { name: 'label', type: (schema) => schema.stringType() },
845
+ { name: 'if', type: (schema) => new NonNullType(schema.booleanType()), defaultValue: true },
846
+ ],
847
+ }),
848
+ (0, directiveAndTypeSpecification_1.createDirectiveSpecification)({
849
+ name: 'stream',
850
+ locations: [graphql_1.DirectiveLocation.FIELD],
851
+ args: [
852
+ { name: 'label', type: (schema) => schema.stringType() },
853
+ { name: 'initialCount', type: (schema) => schema.intType(), defaultValue: 0 },
854
+ { name: 'if', type: (schema) => new NonNullType(schema.booleanType()), defaultValue: true },
855
+ ],
856
+ }),
857
+ ];
858
+ const coordinateRegexp = /^@?[_A-Za-z][_0-9A-Za-z]*(\.[_A-Za-z][_0-9A-Za-z]*)?(\([_A-Za-z][_0-9A-Za-z]*:\))?$/;
859
+ class Schema {
860
+ constructor(blueprint = exports.defaultSchemaBlueprint, config = {}) {
861
+ this.blueprint = blueprint;
862
+ this.config = config;
863
+ this._builtInTypes = new utils_1.MapWithCachedArrays();
864
+ this._types = new utils_1.MapWithCachedArrays();
865
+ this._builtInDirectives = new utils_1.MapWithCachedArrays();
866
+ this._directives = new utils_1.MapWithCachedArrays();
867
+ this.isConstructed = false;
868
+ this.isValidated = false;
869
+ this._schemaDefinition = new SchemaDefinition();
870
+ Element.prototype['setParent'].call(this._schemaDefinition, this);
871
+ graphQLBuiltInTypesSpecifications.forEach((spec) => spec.checkOrAdd(this, undefined, true));
872
+ graphQLBuiltInDirectivesSpecifications.forEach((spec) => spec.checkOrAdd(this, undefined, true));
873
+ blueprint.onConstructed(this);
874
+ this.isConstructed = true;
875
+ }
876
+ canModifyBuiltIn() {
877
+ return !this.isConstructed;
878
+ }
879
+ runWithBuiltInModificationAllowed(fct) {
880
+ const wasConstructed = this.isConstructed;
881
+ this.isConstructed = false;
882
+ fct();
883
+ this.isConstructed = wasConstructed;
884
+ }
885
+ renameTypeInternal(oldName, newName) {
886
+ this._types.set(newName, this._types.get(oldName));
887
+ this._types.delete(oldName);
888
+ }
889
+ removeTypeInternal(type) {
890
+ this._types.delete(type.name);
891
+ }
892
+ removeDirectiveInternal(definition) {
893
+ this._directives.delete(definition.name);
894
+ }
895
+ markAsCoreSchema(coreItself) {
896
+ this._coreFeatures = new CoreFeatures(coreItself);
897
+ }
898
+ unmarkAsCoreSchema() {
899
+ this._coreFeatures = undefined;
900
+ }
901
+ onModification() {
902
+ if (this.isConstructed) {
903
+ this.invalidate();
904
+ this.cachedDocument = undefined;
905
+ this.apiSchema = undefined;
906
+ }
907
+ }
908
+ isCoreSchema() {
909
+ return this.coreFeatures !== undefined;
910
+ }
911
+ get coreFeatures() {
912
+ return this._coreFeatures;
913
+ }
914
+ toAST() {
915
+ var _a;
916
+ if (!this.cachedDocument) {
917
+ const ast = (0, graphql_1.parse)((0, print_1.printSchema)(this), { noLocation: true });
918
+ const shouldCache = (_a = this.config.cacheAST) !== null && _a !== void 0 ? _a : false;
919
+ if (!shouldCache) {
920
+ return ast;
921
+ }
922
+ this.cachedDocument = ast;
923
+ }
924
+ return this.cachedDocument;
925
+ }
926
+ toAPISchema() {
927
+ if (!this.apiSchema) {
928
+ this.validate();
929
+ const apiSchema = this.clone();
930
+ for (const toRemoveIfCustom of ['defer', 'stream']) {
931
+ const directive = apiSchema.directive(toRemoveIfCustom);
932
+ if (directive && !directive.isBuiltIn) {
933
+ directive.removeRecursive();
934
+ }
935
+ }
936
+ (0, inaccessibleSpec_1.removeInaccessibleElements)(apiSchema);
937
+ (0, coreSpec_1.removeAllCoreFeatures)(apiSchema);
938
+ (0, utils_1.assert)(!apiSchema.isCoreSchema(), "The API schema shouldn't be a core schema");
939
+ apiSchema.validate();
940
+ this.apiSchema = apiSchema;
941
+ }
942
+ return this.apiSchema;
943
+ }
944
+ emptyASTDefinitionsForExtensionsWithoutDefinition() {
945
+ const nodes = [];
946
+ if (this.schemaDefinition.hasExtensionElements() && !this.schemaDefinition.hasNonExtensionElements()) {
947
+ const node = { kind: graphql_1.Kind.SCHEMA_DEFINITION, operationTypes: [] };
948
+ nodes.push(node);
949
+ }
950
+ for (const type of this.types()) {
951
+ if (type.hasExtensionElements() && !type.hasNonExtensionElements()) {
952
+ const node = {
953
+ kind: type.astDefinitionKind,
954
+ name: { kind: graphql_1.Kind.NAME, value: type.name },
955
+ };
956
+ nodes.push(node);
957
+ }
958
+ }
959
+ return nodes;
960
+ }
961
+ toGraphQLJSSchema(config) {
962
+ var _a, _b;
963
+ const includeDefer = (_a = config === null || config === void 0 ? void 0 : config.includeDefer) !== null && _a !== void 0 ? _a : false;
964
+ const includeStream = (_b = config === null || config === void 0 ? void 0 : config.includeStream) !== null && _b !== void 0 ? _b : false;
965
+ let ast = this.toAST();
966
+ const additionalNodes = this.emptyASTDefinitionsForExtensionsWithoutDefinition();
967
+ if (includeDefer) {
968
+ additionalNodes.push(this.deferDirective().toAST());
969
+ }
970
+ if (includeStream) {
971
+ additionalNodes.push(this.streamDirective().toAST());
972
+ }
973
+ if (additionalNodes.length > 0) {
974
+ ast = {
975
+ kind: graphql_1.Kind.DOCUMENT,
976
+ definitions: ast.definitions.concat(additionalNodes),
977
+ };
978
+ }
979
+ const graphQLSchema = (0, graphql_1.buildASTSchema)(ast);
980
+ if (additionalNodes.length > 0) {
981
+ for (const node of additionalNodes) {
982
+ switch (node.kind) {
983
+ case graphql_1.Kind.SCHEMA_DEFINITION:
984
+ graphQLSchema.astNode = undefined;
985
+ break;
986
+ case graphql_1.Kind.SCALAR_TYPE_DEFINITION:
987
+ case graphql_1.Kind.OBJECT_TYPE_DEFINITION:
988
+ case graphql_1.Kind.INTERFACE_TYPE_DEFINITION:
989
+ case graphql_1.Kind.ENUM_TYPE_DEFINITION:
990
+ case graphql_1.Kind.UNION_TYPE_DEFINITION:
991
+ case graphql_1.Kind.INPUT_OBJECT_TYPE_DEFINITION:
992
+ const type = graphQLSchema.getType(node.name.value);
993
+ if (type) {
994
+ type.astNode = undefined;
995
+ }
996
+ }
997
+ }
998
+ }
999
+ return graphQLSchema;
1000
+ }
1001
+ get schemaDefinition() {
1002
+ return this._schemaDefinition;
1003
+ }
1004
+ types() {
1005
+ return this._types.values();
1006
+ }
1007
+ interfaceTypes() {
1008
+ return filterTypesOfKind(this.types(), 'InterfaceType');
1009
+ }
1010
+ objectTypes() {
1011
+ return filterTypesOfKind(this.types(), 'ObjectType');
1012
+ }
1013
+ unionTypes() {
1014
+ return filterTypesOfKind(this.types(), 'UnionType');
1015
+ }
1016
+ scalarTypes() {
1017
+ return filterTypesOfKind(this.types(), 'ScalarType');
1018
+ }
1019
+ inputTypes() {
1020
+ return filterTypesOfKind(this.types(), 'InputObjectType');
1021
+ }
1022
+ enumTypes() {
1023
+ return filterTypesOfKind(this.types(), 'EnumType');
1024
+ }
1025
+ builtInTypes(includeShadowed = false) {
1026
+ const allBuiltIns = this._builtInTypes.values();
1027
+ return includeShadowed
1028
+ ? allBuiltIns
1029
+ : allBuiltIns.filter(t => !this.isShadowedBuiltInType(t));
1030
+ }
1031
+ isShadowedBuiltInType(type) {
1032
+ return type.isBuiltIn && this._types.has(type.name);
1033
+ }
1034
+ allTypes() {
1035
+ return this.builtInTypes().concat(this.types());
1036
+ }
1037
+ type(name) {
1038
+ const type = this._types.get(name);
1039
+ return type ? type : this._builtInTypes.get(name);
1040
+ }
1041
+ typeOfKind(name, kind) {
1042
+ const type = this.type(name);
1043
+ return type && type.kind === kind ? type : undefined;
1044
+ }
1045
+ intType() {
1046
+ return this._builtInTypes.get('Int');
1047
+ }
1048
+ floatType() {
1049
+ return this._builtInTypes.get('Float');
1050
+ }
1051
+ stringType() {
1052
+ return this._builtInTypes.get('String');
1053
+ }
1054
+ booleanType() {
1055
+ return this._builtInTypes.get('Boolean');
1056
+ }
1057
+ idType() {
1058
+ return this._builtInTypes.get('ID');
1059
+ }
1060
+ builtInScalarTypes() {
1061
+ return [
1062
+ this.intType(),
1063
+ this.floatType(),
1064
+ this.stringType(),
1065
+ this.booleanType(),
1066
+ this.idType(),
1067
+ ];
1068
+ }
1069
+ addType(type) {
1070
+ const existing = this.type(type.name);
1071
+ if (existing) {
1072
+ (0, utils_1.assert)(existing.isBuiltIn, () => `Type ${type} already exists in this schema`);
1073
+ }
1074
+ if (type.isAttached()) {
1075
+ (0, utils_1.assert)(type.parent == this, () => `Cannot add type ${type} to this schema; it is already attached to another schema`);
1076
+ return type;
1077
+ }
1078
+ if (type.isBuiltIn) {
1079
+ (0, utils_1.assert)(!this.isConstructed, `Cannot add built-in ${type} to this schema (built-ins can only be added at schema construction time)`);
1080
+ this._builtInTypes.set(type.name, type);
1081
+ }
1082
+ else {
1083
+ this._types.set(type.name, type);
1084
+ }
1085
+ Element.prototype['setParent'].call(type, this);
1086
+ const defaultSchemaRoot = checkDefaultSchemaRoot(type);
1087
+ if (defaultSchemaRoot && !this.schemaDefinition.root(defaultSchemaRoot)) {
1088
+ this.schemaDefinition.setRoot(defaultSchemaRoot, type);
1089
+ }
1090
+ this.onModification();
1091
+ return type;
1092
+ }
1093
+ directives() {
1094
+ return this._directives.values();
1095
+ }
1096
+ builtInDirectives(includeShadowed = false) {
1097
+ return includeShadowed
1098
+ ? this._builtInDirectives.values()
1099
+ : this._builtInDirectives.values().filter(d => !this.isShadowedBuiltInDirective(d));
1100
+ }
1101
+ allDirectives() {
1102
+ return this.builtInDirectives().concat(this.directives());
1103
+ }
1104
+ isShadowedBuiltInDirective(directive) {
1105
+ return directive.isBuiltIn && this._directives.has(directive.name);
1106
+ }
1107
+ directive(name) {
1108
+ const directive = this._directives.get(name);
1109
+ return directive ? directive : this.builtInDirective(name);
1110
+ }
1111
+ builtInDirective(name) {
1112
+ return this._builtInDirectives.get(name);
1113
+ }
1114
+ *allNamedSchemaElement() {
1115
+ for (const type of this.types()) {
1116
+ yield type;
1117
+ yield* type.allChildElements();
1118
+ }
1119
+ for (const directive of this.directives()) {
1120
+ yield directive;
1121
+ yield* directive.arguments();
1122
+ }
1123
+ }
1124
+ *allSchemaElement() {
1125
+ yield this._schemaDefinition;
1126
+ yield* this.allNamedSchemaElement();
1127
+ }
1128
+ addDirectiveDefinition(directiveOrName) {
1129
+ const definition = typeof directiveOrName === 'string' ? new DirectiveDefinition(directiveOrName) : directiveOrName;
1130
+ const existing = this.directive(definition.name);
1131
+ (0, utils_1.assert)(!existing || existing.isBuiltIn, () => `Directive ${definition} already exists in this schema`);
1132
+ if (definition.isAttached()) {
1133
+ (0, utils_1.assert)(definition.parent == this, () => `Cannot add directive ${definition} to this schema; it is already attached to another schema`);
1134
+ return definition;
1135
+ }
1136
+ if (definition.isBuiltIn) {
1137
+ (0, utils_1.assert)(!this.isConstructed, () => `Cannot add built-in ${definition} to this schema (built-ins can only be added at schema construction time)`);
1138
+ this._builtInDirectives.set(definition.name, definition);
1139
+ }
1140
+ else {
1141
+ this._directives.set(definition.name, definition);
1142
+ }
1143
+ Element.prototype['setParent'].call(definition, this);
1144
+ this.onModification();
1145
+ return definition;
1146
+ }
1147
+ invalidate() {
1148
+ if (this.isValidated) {
1149
+ this.blueprint.onInvalidation(this);
1150
+ }
1151
+ this.isValidated = false;
1152
+ }
1153
+ validate() {
1154
+ if (this.isValidated) {
1155
+ return;
1156
+ }
1157
+ this.runWithBuiltInModificationAllowed(() => {
1158
+ (0, introspection_1.addIntrospectionFields)(this);
1159
+ });
1160
+ let errors = (0, validate_1.validateSDL)(this.toAST(), undefined, this.blueprint.validationRules()).map((e) => this.blueprint.onGraphQLJSValidationError(this, e));
1161
+ errors = errors.concat((0, validate_2.validateSchema)(this));
1162
+ if (errors.length === 0) {
1163
+ this.runWithBuiltInModificationAllowed(() => {
1164
+ errors = this.blueprint.onValidation(this);
1165
+ });
1166
+ }
1167
+ if (errors.length > 0) {
1168
+ throw (0, exports.ErrGraphQLValidationFailed)(errors);
1169
+ }
1170
+ this.isValidated = true;
1171
+ }
1172
+ clone(builtIns) {
1173
+ const cloned = new Schema(builtIns !== null && builtIns !== void 0 ? builtIns : this.blueprint);
1174
+ copy(this, cloned);
1175
+ if (this.isValidated) {
1176
+ cloned.validate();
1177
+ }
1178
+ return cloned;
1179
+ }
1180
+ getBuiltInDirective(name) {
1181
+ const directive = this.directive(name);
1182
+ (0, utils_1.assert)(directive, `The provided schema has not be built with the ${name} directive built-in`);
1183
+ return directive;
1184
+ }
1185
+ includeDirective() {
1186
+ return this.getBuiltInDirective('include');
1187
+ }
1188
+ skipDirective() {
1189
+ return this.getBuiltInDirective('skip');
1190
+ }
1191
+ deprecatedDirective() {
1192
+ return this.getBuiltInDirective('deprecated');
1193
+ }
1194
+ specifiedByDirective() {
1195
+ return this.getBuiltInDirective('specifiedBy');
1196
+ }
1197
+ deferDirective() {
1198
+ return this.getBuiltInDirective('defer');
1199
+ }
1200
+ streamDirective() {
1201
+ return this.getBuiltInDirective('stream');
1202
+ }
1203
+ elementByCoordinate(coordinate) {
1204
+ if (!coordinate.match(coordinateRegexp)) {
1205
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Invalid argument "${coordinate}: it is not a syntactically valid graphQL coordinate."`);
1206
+ }
1207
+ const argStartIdx = coordinate.indexOf('(');
1208
+ const start = argStartIdx < 0 ? coordinate : coordinate.slice(0, argStartIdx);
1209
+ const argName = argStartIdx < 0 ? undefined : coordinate.slice(argStartIdx + 1, coordinate.length - 2);
1210
+ const splittedStart = start.split('.');
1211
+ const typeOrDirectiveName = splittedStart[0];
1212
+ const fieldOrEnumName = splittedStart[1];
1213
+ const isDirective = typeOrDirectiveName.startsWith('@');
1214
+ if (isDirective) {
1215
+ if (fieldOrEnumName) {
1216
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Invalid argument "${coordinate}: it is not a syntactically valid graphQL coordinate."`);
1217
+ }
1218
+ const directive = this.directive(typeOrDirectiveName.slice(1));
1219
+ return argName ? directive === null || directive === void 0 ? void 0 : directive.argument(argName) : directive;
1220
+ }
1221
+ else {
1222
+ const type = this.type(typeOrDirectiveName);
1223
+ if (!type || !fieldOrEnumName) {
1224
+ return type;
1225
+ }
1226
+ switch (type.kind) {
1227
+ case 'ObjectType':
1228
+ case 'InterfaceType':
1229
+ const field = type.field(fieldOrEnumName);
1230
+ return argName ? field === null || field === void 0 ? void 0 : field.argument(argName) : field;
1231
+ case 'InputObjectType':
1232
+ if (argName) {
1233
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Invalid argument "${coordinate}: it is not a syntactically valid graphQL coordinate."`);
1234
+ }
1235
+ return type.field(fieldOrEnumName);
1236
+ case 'EnumType':
1237
+ if (argName) {
1238
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Invalid argument "${coordinate}: it is not a syntactically valid graphQL coordinate."`);
1239
+ }
1240
+ return type.value(fieldOrEnumName);
1241
+ default:
1242
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Invalid argument "${coordinate}: it is not a syntactically valid graphQL coordinate."`);
1243
+ }
1244
+ }
1245
+ }
1246
+ }
1247
+ exports.Schema = Schema;
1248
+ class RootType extends BaseExtensionMember {
1249
+ constructor(rootKind, type) {
1250
+ super();
1251
+ this.rootKind = rootKind;
1252
+ this.type = type;
1253
+ }
1254
+ isDefaultRootName() {
1255
+ return defaultRootName(this.rootKind) == this.type.name;
1256
+ }
1257
+ removeInner() {
1258
+ SchemaDefinition.prototype['removeRootType'].call(this._parent, this);
1259
+ }
1260
+ }
1261
+ exports.RootType = RootType;
1262
+ class SchemaDefinition extends SchemaElement {
1263
+ constructor() {
1264
+ super(...arguments);
1265
+ this.kind = 'SchemaDefinition';
1266
+ this._roots = new utils_1.MapWithCachedArrays();
1267
+ this.preserveEmptyDefinition = false;
1268
+ }
1269
+ roots() {
1270
+ return this._roots.values();
1271
+ }
1272
+ applyDirective(nameOrDef, args, asFirstDirective = false) {
1273
+ var _a, _b;
1274
+ const applied = super.applyDirective(nameOrDef, args, asFirstDirective);
1275
+ const schema = this.schema();
1276
+ const coreFeatures = schema.coreFeatures;
1277
+ if ((0, coreSpec_1.isCoreSpecDirectiveApplication)(applied)) {
1278
+ if (coreFeatures) {
1279
+ throw error_1.ERRORS.INVALID_LINK_DIRECTIVE_USAGE.err(`Invalid duplicate application of @core/@link`);
1280
+ }
1281
+ const schemaDirective = applied;
1282
+ const args = schemaDirective.arguments();
1283
+ const url = coreSpec_1.FeatureUrl.parse(((_a = args.url) !== null && _a !== void 0 ? _a : args.feature));
1284
+ const imports = (0, coreSpec_1.extractCoreFeatureImports)(url, schemaDirective);
1285
+ const core = new CoreFeature(url, (_b = args.as) !== null && _b !== void 0 ? _b : url.name, schemaDirective, imports, args.for);
1286
+ Schema.prototype['markAsCoreSchema'].call(schema, core);
1287
+ this.appliedDirectives
1288
+ .filter((a) => a !== applied)
1289
+ .forEach((other) => CoreFeatures.prototype['maybeAddFeature'].call(schema.coreFeatures, other));
1290
+ }
1291
+ else if (coreFeatures) {
1292
+ CoreFeatures.prototype['maybeAddFeature'].call(coreFeatures, applied);
1293
+ }
1294
+ this.onModification();
1295
+ return applied;
1296
+ }
1297
+ root(rootKind) {
1298
+ return this._roots.get(rootKind);
1299
+ }
1300
+ rootType(rootKind) {
1301
+ var _a;
1302
+ return (_a = this.root(rootKind)) === null || _a === void 0 ? void 0 : _a.type;
1303
+ }
1304
+ setRoot(rootKind, nameOrType) {
1305
+ let toSet;
1306
+ if (typeof nameOrType === 'string') {
1307
+ this.checkUpdate();
1308
+ const obj = this.schema().type(nameOrType);
1309
+ if (!obj) {
1310
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Cannot set schema ${rootKind} root to unknown type ${nameOrType}`);
1311
+ }
1312
+ else if (obj.kind != 'ObjectType') {
1313
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`${defaultRootName(rootKind)} root type must be an Object type${rootKind === 'query' ? '' : ' if provided'}, it cannot be set to ${nameOrType} (an ${obj.kind}).`);
1314
+ }
1315
+ toSet = new RootType(rootKind, obj);
1316
+ }
1317
+ else {
1318
+ this.checkUpdate(nameOrType);
1319
+ toSet = new RootType(rootKind, nameOrType);
1320
+ }
1321
+ const prevRoot = this._roots.get(rootKind);
1322
+ if (prevRoot) {
1323
+ removeReferenceToType(this, prevRoot.type);
1324
+ }
1325
+ this._roots.set(rootKind, toSet);
1326
+ Element.prototype['setParent'].call(toSet, this);
1327
+ addReferenceToType(this, toSet.type);
1328
+ this.onModification();
1329
+ return toSet;
1330
+ }
1331
+ extensions() {
1332
+ var _a;
1333
+ return (_a = this._extensions) !== null && _a !== void 0 ? _a : [];
1334
+ }
1335
+ hasExtension(extension) {
1336
+ var _a, _b;
1337
+ return (_b = (_a = this._extensions) === null || _a === void 0 ? void 0 : _a.includes(extension)) !== null && _b !== void 0 ? _b : false;
1338
+ }
1339
+ newExtension() {
1340
+ return this.addExtension(new Extension());
1341
+ }
1342
+ addExtension(extension) {
1343
+ this.checkUpdate();
1344
+ if (this.hasExtension(extension)) {
1345
+ return extension;
1346
+ }
1347
+ (0, utils_1.assert)(!extension.extendedElement, 'Cannot add extension to this schema: extension is already added to another schema');
1348
+ if (this._extensions) {
1349
+ this._extensions.push(extension);
1350
+ }
1351
+ else {
1352
+ this._extensions = [extension];
1353
+ }
1354
+ Extension.prototype['setExtendedElement'].call(extension, this);
1355
+ this.onModification();
1356
+ return extension;
1357
+ }
1358
+ hasExtensionElements() {
1359
+ return !!this._extensions;
1360
+ }
1361
+ hasNonExtensionElements() {
1362
+ return this.preserveEmptyDefinition
1363
+ || this.appliedDirectives.some((d) => d.ofExtension() === undefined)
1364
+ || this.roots().some((r) => r.ofExtension() === undefined);
1365
+ }
1366
+ removeRootType(rootType) {
1367
+ this._roots.delete(rootType.rootKind);
1368
+ removeReferenceToType(this, rootType.type);
1369
+ }
1370
+ removeTypeReference(toRemove) {
1371
+ for (const rootType of this.roots()) {
1372
+ if (rootType.type == toRemove) {
1373
+ this._roots.delete(rootType.rootKind);
1374
+ }
1375
+ }
1376
+ }
1377
+ toString() {
1378
+ return `schema[${this._roots.keys().join(', ')}]`;
1379
+ }
1380
+ }
1381
+ exports.SchemaDefinition = SchemaDefinition;
1382
+ class ScalarType extends BaseNamedType {
1383
+ constructor() {
1384
+ super(...arguments);
1385
+ this.kind = 'ScalarType';
1386
+ this.astDefinitionKind = graphql_1.Kind.SCALAR_TYPE_DEFINITION;
1387
+ }
1388
+ removeTypeReference(type) {
1389
+ (0, utils_1.assert)(false, `Scalar type ${this} can't reference other types; shouldn't be asked to remove reference to ${type}`);
1390
+ }
1391
+ hasNonExtensionInnerElements() {
1392
+ return false;
1393
+ }
1394
+ removeInnerElementsExtensions() {
1395
+ }
1396
+ removeInnerElements() {
1397
+ }
1398
+ removeReferenceRecursive(ref) {
1399
+ ref.remove();
1400
+ }
1401
+ }
1402
+ exports.ScalarType = ScalarType;
1403
+ class InterfaceImplementation extends BaseExtensionMember {
1404
+ constructor(itf) {
1405
+ super();
1406
+ this.interface = itf;
1407
+ }
1408
+ removeInner() {
1409
+ FieldBasedType.prototype['removeInterfaceImplementation'].call(this._parent, this.interface);
1410
+ }
1411
+ toString() {
1412
+ return `'implements ${this.interface}'`;
1413
+ }
1414
+ }
1415
+ exports.InterfaceImplementation = InterfaceImplementation;
1416
+ class FieldBasedType extends BaseNamedType {
1417
+ constructor() {
1418
+ super(...arguments);
1419
+ this._fields = new utils_1.MapWithCachedArrays();
1420
+ }
1421
+ onAttached() {
1422
+ Schema.prototype['runWithBuiltInModificationAllowed'].call(this.schema(), () => {
1423
+ this.addField(new FieldDefinition(exports.typenameFieldName, true), new NonNullType(this.schema().stringType()));
1424
+ });
1425
+ }
1426
+ removeFieldInternal(field) {
1427
+ this._fields.delete(field.name);
1428
+ this._cachedNonBuiltInFields = undefined;
1429
+ }
1430
+ interfaceImplementations() {
1431
+ var _a, _b;
1432
+ return (_b = (_a = this._interfaceImplementations) === null || _a === void 0 ? void 0 : _a.values()) !== null && _b !== void 0 ? _b : [];
1433
+ }
1434
+ interfaceImplementation(type) {
1435
+ return this._interfaceImplementations ? this._interfaceImplementations.get(typeof type === 'string' ? type : type.name) : undefined;
1436
+ }
1437
+ interfaces() {
1438
+ return this.interfaceImplementations().map(impl => impl.interface);
1439
+ }
1440
+ implementsInterface(type) {
1441
+ var _a, _b;
1442
+ return (_b = (_a = this._interfaceImplementations) === null || _a === void 0 ? void 0 : _a.has(typeof type === 'string' ? type : type.name)) !== null && _b !== void 0 ? _b : false;
1443
+ }
1444
+ addImplementedInterface(nameOrItfOrItfImpl) {
1445
+ var _a;
1446
+ let toAdd;
1447
+ if (nameOrItfOrItfImpl instanceof InterfaceImplementation) {
1448
+ this.checkUpdate(nameOrItfOrItfImpl);
1449
+ toAdd = nameOrItfOrItfImpl;
1450
+ }
1451
+ else {
1452
+ let itf;
1453
+ if (typeof nameOrItfOrItfImpl === 'string') {
1454
+ this.checkUpdate();
1455
+ const maybeItf = this.schema().type(nameOrItfOrItfImpl);
1456
+ if (!maybeItf) {
1457
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Cannot implement unknown type ${nameOrItfOrItfImpl}`);
1458
+ }
1459
+ else if (maybeItf.kind != 'InterfaceType') {
1460
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Cannot implement non-interface type ${nameOrItfOrItfImpl} (of type ${maybeItf.kind})`);
1461
+ }
1462
+ itf = maybeItf;
1463
+ }
1464
+ else {
1465
+ itf = nameOrItfOrItfImpl;
1466
+ }
1467
+ toAdd = new InterfaceImplementation(itf);
1468
+ }
1469
+ const existing = (_a = this._interfaceImplementations) === null || _a === void 0 ? void 0 : _a.get(toAdd.interface.name);
1470
+ if (!existing) {
1471
+ if (!this._interfaceImplementations) {
1472
+ this._interfaceImplementations = new utils_1.MapWithCachedArrays();
1473
+ }
1474
+ this._interfaceImplementations.set(toAdd.interface.name, toAdd);
1475
+ addReferenceToType(this, toAdd.interface);
1476
+ Element.prototype['setParent'].call(toAdd, this);
1477
+ this.onModification();
1478
+ return toAdd;
1479
+ }
1480
+ else {
1481
+ return existing;
1482
+ }
1483
+ }
1484
+ fields() {
1485
+ if (!this._cachedNonBuiltInFields) {
1486
+ this._cachedNonBuiltInFields = this._fields.values().filter(f => !f.isBuiltIn);
1487
+ }
1488
+ return this._cachedNonBuiltInFields;
1489
+ }
1490
+ hasFields() {
1491
+ return this.fields().length > 0;
1492
+ }
1493
+ builtInFields() {
1494
+ return this.allFields().filter(f => f.isBuiltIn);
1495
+ }
1496
+ allFields() {
1497
+ return this._fields.values();
1498
+ }
1499
+ field(name) {
1500
+ return this._fields.get(name);
1501
+ }
1502
+ typenameField() {
1503
+ return this.field(exports.typenameFieldName);
1504
+ }
1505
+ addField(nameOrField, type) {
1506
+ let toAdd;
1507
+ if (typeof nameOrField === 'string') {
1508
+ this.checkUpdate();
1509
+ toAdd = new FieldDefinition(nameOrField);
1510
+ }
1511
+ else {
1512
+ this.checkUpdate(nameOrField);
1513
+ toAdd = nameOrField;
1514
+ }
1515
+ if (this.field(toAdd.name)) {
1516
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Field ${toAdd.name} already exists on ${this}`);
1517
+ }
1518
+ if (type && !isOutputType(type)) {
1519
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Invalid input type ${type} for field ${toAdd.name}: object and interface field types should be output types.`);
1520
+ }
1521
+ this._fields.set(toAdd.name, toAdd);
1522
+ this._cachedNonBuiltInFields = undefined;
1523
+ Element.prototype['setParent'].call(toAdd, this);
1524
+ if (type) {
1525
+ toAdd.type = type;
1526
+ }
1527
+ this.onModification();
1528
+ return toAdd;
1529
+ }
1530
+ *allChildElements() {
1531
+ for (const field of this._fields.values()) {
1532
+ yield field;
1533
+ yield* field.arguments();
1534
+ }
1535
+ }
1536
+ removeInterfaceImplementation(itf) {
1537
+ var _a;
1538
+ (_a = this._interfaceImplementations) === null || _a === void 0 ? void 0 : _a.delete(itf.name);
1539
+ removeReferenceToType(this, itf);
1540
+ }
1541
+ removeTypeReference(type) {
1542
+ var _a;
1543
+ (_a = this._interfaceImplementations) === null || _a === void 0 ? void 0 : _a.delete(type.name);
1544
+ }
1545
+ removeInnerElements() {
1546
+ for (const interfaceImpl of this.interfaceImplementations()) {
1547
+ interfaceImpl.remove();
1548
+ }
1549
+ for (const field of this.allFields()) {
1550
+ if (field.isBuiltIn) {
1551
+ FieldDefinition.prototype['removeParent'].call(field);
1552
+ }
1553
+ else {
1554
+ field.remove();
1555
+ }
1556
+ }
1557
+ }
1558
+ hasNonExtensionInnerElements() {
1559
+ return this.interfaceImplementations().some(itf => itf.ofExtension() === undefined)
1560
+ || this.fields().some(f => f.ofExtension() === undefined);
1561
+ }
1562
+ removeInnerElementsExtensions() {
1563
+ this.interfaceImplementations().forEach(itf => itf.removeOfExtension());
1564
+ this.fields().forEach(f => f.removeOfExtension());
1565
+ }
1566
+ }
1567
+ class ObjectType extends FieldBasedType {
1568
+ constructor() {
1569
+ super(...arguments);
1570
+ this.kind = 'ObjectType';
1571
+ this.astDefinitionKind = graphql_1.Kind.OBJECT_TYPE_DEFINITION;
1572
+ }
1573
+ isRootType() {
1574
+ const schema = this.schema();
1575
+ return schema.schemaDefinition.roots().some(rt => rt.type == this);
1576
+ }
1577
+ isQueryRootType() {
1578
+ var _a;
1579
+ const schema = this.schema();
1580
+ return ((_a = schema.schemaDefinition.root('query')) === null || _a === void 0 ? void 0 : _a.type) === this;
1581
+ }
1582
+ isSubscriptionRootType() {
1583
+ var _a;
1584
+ const schema = this.schema();
1585
+ return ((_a = schema.schemaDefinition.root('subscription')) === null || _a === void 0 ? void 0 : _a.type) === this;
1586
+ }
1587
+ removeReferenceRecursive(ref) {
1588
+ switch (ref.kind) {
1589
+ case 'FieldDefinition':
1590
+ ref.removeRecursive();
1591
+ break;
1592
+ case 'UnionType':
1593
+ if (ref.membersCount() === 0) {
1594
+ ref.removeRecursive();
1595
+ }
1596
+ break;
1597
+ }
1598
+ }
1599
+ unionsWhereMember() {
1600
+ var _a, _b;
1601
+ return (_b = (_a = this._referencers) === null || _a === void 0 ? void 0 : _a.filter((r) => r instanceof BaseNamedType && isUnionType(r))) !== null && _b !== void 0 ? _b : [];
1602
+ }
1603
+ }
1604
+ exports.ObjectType = ObjectType;
1605
+ class InterfaceType extends FieldBasedType {
1606
+ constructor() {
1607
+ super(...arguments);
1608
+ this.kind = 'InterfaceType';
1609
+ this.astDefinitionKind = graphql_1.Kind.INTERFACE_TYPE_DEFINITION;
1610
+ }
1611
+ allImplementations() {
1612
+ return this.referencers().filter(ref => ref.kind === 'ObjectType' || ref.kind === 'InterfaceType');
1613
+ }
1614
+ possibleRuntimeTypes() {
1615
+ return this.allImplementations().filter(impl => impl.kind === 'ObjectType');
1616
+ }
1617
+ isPossibleRuntimeType(type) {
1618
+ const typeName = typeof type === 'string' ? type : type.name;
1619
+ return this.possibleRuntimeTypes().some(t => t.name == typeName);
1620
+ }
1621
+ removeReferenceRecursive(ref) {
1622
+ if (ref.kind === 'FieldDefinition') {
1623
+ ref.removeRecursive();
1624
+ }
1625
+ }
1626
+ }
1627
+ exports.InterfaceType = InterfaceType;
1628
+ class UnionMember extends BaseExtensionMember {
1629
+ constructor(type) {
1630
+ super();
1631
+ this.type = type;
1632
+ }
1633
+ removeInner() {
1634
+ UnionType.prototype['removeMember'].call(this._parent, this.type);
1635
+ }
1636
+ }
1637
+ exports.UnionMember = UnionMember;
1638
+ class UnionType extends BaseNamedType {
1639
+ constructor() {
1640
+ super(...arguments);
1641
+ this.kind = 'UnionType';
1642
+ this.astDefinitionKind = graphql_1.Kind.UNION_TYPE_DEFINITION;
1643
+ this._members = new utils_1.MapWithCachedArrays();
1644
+ }
1645
+ onAttached() {
1646
+ Schema.prototype['runWithBuiltInModificationAllowed'].call(this.schema(), () => {
1647
+ this._typenameField = new FieldDefinition(exports.typenameFieldName, true);
1648
+ Element.prototype['setParent'].call(this._typenameField, this);
1649
+ this._typenameField.type = new NonNullType(this.schema().stringType());
1650
+ });
1651
+ }
1652
+ types() {
1653
+ return this.members().map(m => m.type);
1654
+ }
1655
+ members() {
1656
+ return this._members.values();
1657
+ }
1658
+ membersCount() {
1659
+ return this._members.size;
1660
+ }
1661
+ hasTypeMember(type) {
1662
+ return this._members.has(typeof type === 'string' ? type : type.name);
1663
+ }
1664
+ addType(nameOrTypeOrMember) {
1665
+ let toAdd;
1666
+ if (nameOrTypeOrMember instanceof UnionMember) {
1667
+ this.checkUpdate(nameOrTypeOrMember);
1668
+ toAdd = nameOrTypeOrMember;
1669
+ }
1670
+ else {
1671
+ let obj;
1672
+ if (typeof nameOrTypeOrMember === 'string') {
1673
+ this.checkUpdate();
1674
+ const maybeObj = this.schema().type(nameOrTypeOrMember);
1675
+ if (!maybeObj) {
1676
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Cannot add unknown type ${nameOrTypeOrMember} as member of union type ${this.name}`);
1677
+ }
1678
+ else if (maybeObj.kind != 'ObjectType') {
1679
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Cannot add non-object type ${nameOrTypeOrMember} (of type ${maybeObj.kind}) as member of union type ${this.name}`);
1680
+ }
1681
+ obj = maybeObj;
1682
+ }
1683
+ else {
1684
+ this.checkUpdate(nameOrTypeOrMember);
1685
+ obj = nameOrTypeOrMember;
1686
+ }
1687
+ toAdd = new UnionMember(obj);
1688
+ }
1689
+ const existing = this._members.get(toAdd.type.name);
1690
+ if (!existing) {
1691
+ this._members.set(toAdd.type.name, toAdd);
1692
+ Element.prototype['setParent'].call(toAdd, this);
1693
+ addReferenceToType(this, toAdd.type);
1694
+ this.onModification();
1695
+ return toAdd;
1696
+ }
1697
+ else {
1698
+ return existing;
1699
+ }
1700
+ }
1701
+ clearTypes() {
1702
+ for (const type of this.types()) {
1703
+ this.removeMember(type);
1704
+ }
1705
+ this.onModification();
1706
+ }
1707
+ field(name) {
1708
+ if (name === exports.typenameFieldName && this._typenameField) {
1709
+ return this._typenameField;
1710
+ }
1711
+ return undefined;
1712
+ }
1713
+ typenameField() {
1714
+ return this._typenameField;
1715
+ }
1716
+ removeMember(type) {
1717
+ this._members.delete(type.name);
1718
+ removeReferenceToType(this, type);
1719
+ }
1720
+ removeTypeReference(type) {
1721
+ this._members.delete(type.name);
1722
+ }
1723
+ removeInnerElements() {
1724
+ for (const member of this.members()) {
1725
+ member.remove();
1726
+ }
1727
+ }
1728
+ hasNonExtensionInnerElements() {
1729
+ return this.members().some(m => m.ofExtension() === undefined);
1730
+ }
1731
+ removeReferenceRecursive(ref) {
1732
+ ref.removeRecursive();
1733
+ }
1734
+ removeInnerElementsExtensions() {
1735
+ this.members().forEach(m => m.removeOfExtension());
1736
+ }
1737
+ }
1738
+ exports.UnionType = UnionType;
1739
+ class EnumType extends BaseNamedType {
1740
+ constructor() {
1741
+ super(...arguments);
1742
+ this.kind = 'EnumType';
1743
+ this.astDefinitionKind = graphql_1.Kind.ENUM_TYPE_DEFINITION;
1744
+ this._values = [];
1745
+ }
1746
+ get values() {
1747
+ return Array.from(this._values);
1748
+ }
1749
+ value(name) {
1750
+ return this._values.find(v => v.name === name);
1751
+ }
1752
+ addValue(nameOrValue) {
1753
+ let toAdd;
1754
+ if (typeof nameOrValue === 'string') {
1755
+ this.checkUpdate();
1756
+ toAdd = new EnumValue(nameOrValue);
1757
+ }
1758
+ else {
1759
+ this.checkUpdate(nameOrValue);
1760
+ toAdd = nameOrValue;
1761
+ }
1762
+ const existing = this.value(toAdd.name);
1763
+ if (!existing) {
1764
+ this._values.push(toAdd);
1765
+ Element.prototype['setParent'].call(toAdd, this);
1766
+ this.onModification();
1767
+ return toAdd;
1768
+ }
1769
+ else {
1770
+ return existing;
1771
+ }
1772
+ }
1773
+ removeTypeReference(type) {
1774
+ (0, utils_1.assert)(false, `Eum type ${this} can't reference other types; shouldn't be asked to remove reference to ${type}`);
1775
+ }
1776
+ removeValueInternal(value) {
1777
+ (0, utils_1.removeArrayElement)(value, this._values);
1778
+ }
1779
+ removeInnerElements() {
1780
+ const values = this.values;
1781
+ for (const value of values) {
1782
+ value.remove();
1783
+ }
1784
+ }
1785
+ hasNonExtensionInnerElements() {
1786
+ return this._values.some(v => v.ofExtension() === undefined);
1787
+ }
1788
+ removeReferenceRecursive(ref) {
1789
+ ref.removeRecursive();
1790
+ }
1791
+ removeInnerElementsExtensions() {
1792
+ this._values.forEach(v => v.removeOfExtension());
1793
+ }
1794
+ }
1795
+ exports.EnumType = EnumType;
1796
+ class InputObjectType extends BaseNamedType {
1797
+ constructor() {
1798
+ super(...arguments);
1799
+ this.kind = 'InputObjectType';
1800
+ this.astDefinitionKind = graphql_1.Kind.INPUT_OBJECT_TYPE_DEFINITION;
1801
+ this._fields = new Map();
1802
+ }
1803
+ fields() {
1804
+ if (!this._cachedFieldsArray) {
1805
+ this._cachedFieldsArray = (0, utils_1.mapValues)(this._fields);
1806
+ }
1807
+ return this._cachedFieldsArray;
1808
+ }
1809
+ field(name) {
1810
+ return this._fields.get(name);
1811
+ }
1812
+ addField(nameOrField, type) {
1813
+ const toAdd = typeof nameOrField === 'string' ? new InputFieldDefinition(nameOrField) : nameOrField;
1814
+ this.checkUpdate(toAdd);
1815
+ if (this.field(toAdd.name)) {
1816
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Field ${toAdd.name} already exists on ${this}`);
1817
+ }
1818
+ if (type && !isInputType(type)) {
1819
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Invalid output type ${type} for field ${toAdd.name}: input field types should be input types.`);
1820
+ }
1821
+ this._fields.set(toAdd.name, toAdd);
1822
+ this._cachedFieldsArray = undefined;
1823
+ Element.prototype['setParent'].call(toAdd, this);
1824
+ if (typeof nameOrField === 'string' && type) {
1825
+ toAdd.type = type;
1826
+ }
1827
+ this.onModification();
1828
+ return toAdd;
1829
+ }
1830
+ hasFields() {
1831
+ return this._fields.size > 0;
1832
+ }
1833
+ *allChildElements() {
1834
+ yield* this._fields.values();
1835
+ }
1836
+ removeTypeReference(type) {
1837
+ (0, utils_1.assert)(false, `Input Object type ${this} can't reference other types; shouldn't be asked to remove reference to ${type}`);
1838
+ }
1839
+ removeInnerElements() {
1840
+ for (const field of this.fields()) {
1841
+ field.remove();
1842
+ }
1843
+ }
1844
+ removeFieldInternal(field) {
1845
+ this._fields.delete(field.name);
1846
+ this._cachedFieldsArray = undefined;
1847
+ }
1848
+ hasNonExtensionInnerElements() {
1849
+ return this.fields().some(f => f.ofExtension() === undefined);
1850
+ }
1851
+ removeReferenceRecursive(ref) {
1852
+ if (ref.kind === 'ArgumentDefinition') {
1853
+ ref.parent().removeRecursive();
1854
+ }
1855
+ else {
1856
+ ref.removeRecursive();
1857
+ }
1858
+ }
1859
+ removeInnerElementsExtensions() {
1860
+ this.fields().forEach(f => f.removeOfExtension());
1861
+ }
1862
+ }
1863
+ exports.InputObjectType = InputObjectType;
1864
+ class BaseWrapperType {
1865
+ constructor(_type) {
1866
+ this._type = _type;
1867
+ (0, utils_1.assert)(this._type, 'Cannot wrap an undefined/null type');
1868
+ }
1869
+ schema() {
1870
+ return this.baseType().schema();
1871
+ }
1872
+ isAttached() {
1873
+ return this.baseType().isAttached();
1874
+ }
1875
+ get ofType() {
1876
+ return this._type;
1877
+ }
1878
+ baseType() {
1879
+ return baseType(this._type);
1880
+ }
1881
+ }
1882
+ class ListType extends BaseWrapperType {
1883
+ constructor(type) {
1884
+ super(type);
1885
+ this.kind = 'ListType';
1886
+ }
1887
+ toString() {
1888
+ return `[${this.ofType}]`;
1889
+ }
1890
+ }
1891
+ exports.ListType = ListType;
1892
+ class NonNullType extends BaseWrapperType {
1893
+ constructor(type) {
1894
+ super(type);
1895
+ this.kind = 'NonNullType';
1896
+ }
1897
+ toString() {
1898
+ return `${this.ofType}!`;
1899
+ }
1900
+ }
1901
+ exports.NonNullType = NonNullType;
1902
+ class FieldDefinition extends NamedSchemaElementWithType {
1903
+ constructor(name, isBuiltIn = false) {
1904
+ super(name);
1905
+ this.isBuiltIn = isBuiltIn;
1906
+ this.kind = 'FieldDefinition';
1907
+ }
1908
+ isElementBuiltIn() {
1909
+ return this.isBuiltIn;
1910
+ }
1911
+ get coordinate() {
1912
+ const parent = this._parent;
1913
+ return `${parent == undefined ? '<detached>' : parent.coordinate}.${this.name}`;
1914
+ }
1915
+ hasArguments() {
1916
+ return !!this._args && this._args.size > 0;
1917
+ }
1918
+ arguments() {
1919
+ var _a, _b;
1920
+ return (_b = (_a = this._args) === null || _a === void 0 ? void 0 : _a.values()) !== null && _b !== void 0 ? _b : [];
1921
+ }
1922
+ argument(name) {
1923
+ var _a;
1924
+ return (_a = this._args) === null || _a === void 0 ? void 0 : _a.get(name);
1925
+ }
1926
+ addArgument(nameOrArg, type, defaultValue) {
1927
+ let toAdd;
1928
+ if (typeof nameOrArg === 'string') {
1929
+ this.checkUpdate();
1930
+ toAdd = new ArgumentDefinition(nameOrArg);
1931
+ toAdd.defaultValue = defaultValue;
1932
+ }
1933
+ else {
1934
+ this.checkUpdate(nameOrArg);
1935
+ toAdd = nameOrArg;
1936
+ }
1937
+ const existing = this.argument(toAdd.name);
1938
+ if (existing) {
1939
+ if (type && existing.type && !(0, types_1.sameType)(type, existing.type)) {
1940
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Argument ${toAdd.name} already exists on field ${this.name} with a different type (${existing.type})`);
1941
+ }
1942
+ if (defaultValue && (!existing.defaultValue || !(0, values_1.valueEquals)(defaultValue, existing.defaultValue))) {
1943
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Argument ${toAdd.name} already exists on field ${this.name} with a different default value (${(0, values_1.valueToString)(existing.defaultValue)})`);
1944
+ }
1945
+ return existing;
1946
+ }
1947
+ if (type && !isInputType(type)) {
1948
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Invalid output type ${type} for argument ${toAdd.name} of ${this}: arguments should be input types.`);
1949
+ }
1950
+ if (!this._args) {
1951
+ this._args = new utils_1.MapWithCachedArrays();
1952
+ }
1953
+ this._args.set(toAdd.name, toAdd);
1954
+ Element.prototype['setParent'].call(toAdd, this);
1955
+ if (typeof nameOrArg === 'string') {
1956
+ toAdd.type = type;
1957
+ }
1958
+ this.onModification();
1959
+ return toAdd;
1960
+ }
1961
+ ofExtension() {
1962
+ return this._extension;
1963
+ }
1964
+ removeOfExtension() {
1965
+ this._extension = undefined;
1966
+ }
1967
+ setOfExtension(extension) {
1968
+ var _a;
1969
+ this.checkUpdate();
1970
+ (0, utils_1.assert)(!extension || ((_a = this._parent) === null || _a === void 0 ? void 0 : _a.hasExtension(extension)), () => `Cannot mark field ${this.name} as part of the provided extension: it is not an extension of field parent type ${this.parent}`);
1971
+ this._extension = extension;
1972
+ this.onModification();
1973
+ }
1974
+ isIntrospectionField() {
1975
+ return (0, introspection_1.isIntrospectionName)(this.name);
1976
+ }
1977
+ isSchemaIntrospectionField() {
1978
+ return introspection_1.introspectionFieldNames.includes(this.name);
1979
+ }
1980
+ removeArgumentInternal(name) {
1981
+ if (this._args) {
1982
+ this._args.delete(name);
1983
+ }
1984
+ }
1985
+ removeParent() {
1986
+ this._parent = undefined;
1987
+ }
1988
+ isDeprecated() {
1989
+ return this.hasAppliedDirective('deprecated');
1990
+ }
1991
+ remove() {
1992
+ if (!this._parent) {
1993
+ return [];
1994
+ }
1995
+ this.checkRemoval();
1996
+ this.onModification();
1997
+ this.sourceAST = undefined;
1998
+ this.type = undefined;
1999
+ this.removeAppliedDirectives();
2000
+ for (const arg of this.arguments()) {
2001
+ arg.remove();
2002
+ }
2003
+ FieldBasedType.prototype['removeFieldInternal'].call(this._parent, this);
2004
+ this._parent = undefined;
2005
+ this._extension = undefined;
2006
+ return [];
2007
+ }
2008
+ removeRecursive() {
2009
+ const parent = this._parent;
2010
+ this.remove();
2011
+ if (parent && !isUnionType(parent) && parent.fields().length === 0) {
2012
+ parent.removeRecursive();
2013
+ }
2014
+ }
2015
+ toString() {
2016
+ const args = this.hasArguments()
2017
+ ? '(' + this.arguments().map(arg => arg.toString()).join(', ') + ')'
2018
+ : "";
2019
+ return `${this.name}${args}: ${this.type}`;
2020
+ }
2021
+ }
2022
+ exports.FieldDefinition = FieldDefinition;
2023
+ class InputFieldDefinition extends NamedSchemaElementWithType {
2024
+ constructor() {
2025
+ super(...arguments);
2026
+ this.kind = 'InputFieldDefinition';
2027
+ }
2028
+ get coordinate() {
2029
+ const parent = this._parent;
2030
+ return `${parent == undefined ? '<detached>' : parent.coordinate}.${this.name}`;
2031
+ }
2032
+ isRequired() {
2033
+ return isNonNullType(this.type) && this.defaultValue === undefined;
2034
+ }
2035
+ ofExtension() {
2036
+ return this._extension;
2037
+ }
2038
+ removeOfExtension() {
2039
+ this._extension = undefined;
2040
+ }
2041
+ setOfExtension(extension) {
2042
+ var _a;
2043
+ this.checkUpdate();
2044
+ (0, utils_1.assert)(!extension || ((_a = this._parent) === null || _a === void 0 ? void 0 : _a.hasExtension(extension)), () => `Cannot mark field ${this.name} as part of the provided extension: it is not an extension of field parent type ${this.parent}`);
2045
+ this._extension = extension;
2046
+ this.onModification();
2047
+ }
2048
+ isDeprecated() {
2049
+ return this.hasAppliedDirective('deprecated');
2050
+ }
2051
+ remove() {
2052
+ if (!this._parent) {
2053
+ return [];
2054
+ }
2055
+ this.checkRemoval();
2056
+ this.onModification();
2057
+ this.sourceAST = undefined;
2058
+ this.type = undefined;
2059
+ this.defaultValue = undefined;
2060
+ this.removeAppliedDirectives();
2061
+ InputObjectType.prototype['removeFieldInternal'].call(this._parent, this);
2062
+ this._parent = undefined;
2063
+ this._extension = undefined;
2064
+ return [];
2065
+ }
2066
+ removeRecursive() {
2067
+ const parent = this._parent;
2068
+ this.remove();
2069
+ if (parent && parent.fields().length === 0) {
2070
+ parent.removeRecursive();
2071
+ }
2072
+ }
2073
+ toString() {
2074
+ const defaultStr = this.defaultValue === undefined ? "" : ` = ${(0, values_1.valueToString)(this.defaultValue, this.type)}`;
2075
+ return `${this.name}: ${this.type}${defaultStr}`;
2076
+ }
2077
+ }
2078
+ exports.InputFieldDefinition = InputFieldDefinition;
2079
+ class ArgumentDefinition extends NamedSchemaElementWithType {
2080
+ constructor(name) {
2081
+ super(name);
2082
+ this.kind = 'ArgumentDefinition';
2083
+ }
2084
+ get coordinate() {
2085
+ const parent = this._parent;
2086
+ return `${parent == undefined ? '<detached>' : parent.coordinate}(${this.name}:)`;
2087
+ }
2088
+ isRequired() {
2089
+ return isNonNullType(this.type) && this.defaultValue === undefined;
2090
+ }
2091
+ isDeprecated() {
2092
+ return this.hasAppliedDirective('deprecated');
2093
+ }
2094
+ remove() {
2095
+ if (!this._parent) {
2096
+ return [];
2097
+ }
2098
+ this.checkRemoval();
2099
+ this.onModification();
2100
+ this.sourceAST = undefined;
2101
+ this.type = undefined;
2102
+ this.defaultValue = undefined;
2103
+ this.removeAppliedDirectives();
2104
+ if (this._parent instanceof FieldDefinition) {
2105
+ FieldDefinition.prototype['removeArgumentInternal'].call(this._parent, this.name);
2106
+ }
2107
+ else {
2108
+ DirectiveDefinition.prototype['removeArgumentInternal'].call(this._parent, this.name);
2109
+ }
2110
+ this._parent = undefined;
2111
+ return [];
2112
+ }
2113
+ toString() {
2114
+ const defaultStr = this.defaultValue === undefined ? "" : ` = ${(0, values_1.valueToString)(this.defaultValue, this.type)}`;
2115
+ return `${this.name}: ${this.type}${defaultStr}`;
2116
+ }
2117
+ }
2118
+ exports.ArgumentDefinition = ArgumentDefinition;
2119
+ class EnumValue extends NamedSchemaElement {
2120
+ constructor() {
2121
+ super(...arguments);
2122
+ this.kind = 'EnumValue';
2123
+ }
2124
+ get coordinate() {
2125
+ const parent = this._parent;
2126
+ return `${parent == undefined ? '<detached>' : parent.coordinate}.${this.name}`;
2127
+ }
2128
+ ofExtension() {
2129
+ return this._extension;
2130
+ }
2131
+ removeOfExtension() {
2132
+ this._extension = undefined;
2133
+ }
2134
+ setOfExtension(extension) {
2135
+ var _a;
2136
+ this.checkUpdate();
2137
+ (0, utils_1.assert)(!extension || ((_a = this._parent) === null || _a === void 0 ? void 0 : _a.hasExtension(extension)), () => `Cannot mark field ${this.name} as part of the provided extension: it is not an extension of enum value parent type ${this.parent}`);
2138
+ this._extension = extension;
2139
+ this.onModification();
2140
+ }
2141
+ isDeprecated() {
2142
+ return this.hasAppliedDirective('deprecated');
2143
+ }
2144
+ remove() {
2145
+ if (!this._parent) {
2146
+ return [];
2147
+ }
2148
+ this.checkRemoval();
2149
+ this.onModification();
2150
+ this.sourceAST = undefined;
2151
+ this.removeAppliedDirectives();
2152
+ EnumType.prototype['removeValueInternal'].call(this._parent, this);
2153
+ this._parent = undefined;
2154
+ this._extension = undefined;
2155
+ return [];
2156
+ }
2157
+ removeTypeReference(type) {
2158
+ (0, utils_1.assert)(false, `Enum value ${this} can't reference other types; shouldn't be asked to remove reference to ${type}`);
2159
+ }
2160
+ toString() {
2161
+ return `${this.name}`;
2162
+ }
2163
+ }
2164
+ exports.EnumValue = EnumValue;
2165
+ class DirectiveDefinition extends NamedSchemaElement {
2166
+ constructor(name, isBuiltIn = false) {
2167
+ super(name);
2168
+ this.isBuiltIn = isBuiltIn;
2169
+ this.kind = 'DirectiveDefinition';
2170
+ this.repeatable = false;
2171
+ this._locations = [];
2172
+ }
2173
+ get coordinate() {
2174
+ return `@${this.name}`;
2175
+ }
2176
+ arguments() {
2177
+ var _a, _b;
2178
+ return (_b = (_a = this._args) === null || _a === void 0 ? void 0 : _a.values()) !== null && _b !== void 0 ? _b : [];
2179
+ }
2180
+ argument(name) {
2181
+ var _a;
2182
+ return (_a = this._args) === null || _a === void 0 ? void 0 : _a.get(name);
2183
+ }
2184
+ addArgument(nameOrArg, type, defaultValue) {
2185
+ let toAdd;
2186
+ if (typeof nameOrArg === 'string') {
2187
+ this.checkUpdate();
2188
+ toAdd = new ArgumentDefinition(nameOrArg);
2189
+ toAdd.defaultValue = defaultValue;
2190
+ }
2191
+ else {
2192
+ this.checkUpdate(nameOrArg);
2193
+ toAdd = nameOrArg;
2194
+ }
2195
+ if (this.argument(toAdd.name)) {
2196
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Argument ${toAdd.name} already exists on field ${this.name}`);
2197
+ }
2198
+ if (!this._args) {
2199
+ this._args = new utils_1.MapWithCachedArrays();
2200
+ }
2201
+ this._args.set(toAdd.name, toAdd);
2202
+ Element.prototype['setParent'].call(toAdd, this);
2203
+ if (typeof nameOrArg === 'string') {
2204
+ toAdd.type = type;
2205
+ }
2206
+ this.onModification();
2207
+ return toAdd;
2208
+ }
2209
+ removeArgumentInternal(name) {
2210
+ var _a;
2211
+ (_a = this._args) === null || _a === void 0 ? void 0 : _a.delete(name);
2212
+ }
2213
+ get locations() {
2214
+ return this._locations;
2215
+ }
2216
+ addLocations(...locations) {
2217
+ let modified = false;
2218
+ for (const location of locations) {
2219
+ if (!this._locations.includes(location)) {
2220
+ this._locations.push(location);
2221
+ modified = true;
2222
+ }
2223
+ }
2224
+ if (modified) {
2225
+ this.onModification();
2226
+ }
2227
+ return this;
2228
+ }
2229
+ addAllLocations() {
2230
+ return this.addLocations(...Object.values(graphql_1.DirectiveLocation));
2231
+ }
2232
+ addAllTypeLocations() {
2233
+ return this.addLocations(graphql_1.DirectiveLocation.SCALAR, graphql_1.DirectiveLocation.OBJECT, graphql_1.DirectiveLocation.INTERFACE, graphql_1.DirectiveLocation.UNION, graphql_1.DirectiveLocation.ENUM, graphql_1.DirectiveLocation.INPUT_OBJECT);
2234
+ }
2235
+ removeLocations(...locations) {
2236
+ let modified = false;
2237
+ for (const location of locations) {
2238
+ modified || (modified = (0, utils_1.removeArrayElement)(location, this._locations));
2239
+ }
2240
+ if (modified) {
2241
+ this.onModification();
2242
+ }
2243
+ return this;
2244
+ }
2245
+ hasExecutableLocations() {
2246
+ return this.locations.some((loc) => isExecutableDirectiveLocation(loc));
2247
+ }
2248
+ hasTypeSystemLocations() {
2249
+ return this.locations.some((loc) => isTypeSystemDirectiveLocation(loc));
2250
+ }
2251
+ applications() {
2252
+ var _a;
2253
+ return (_a = this._referencers) !== null && _a !== void 0 ? _a : [];
2254
+ }
2255
+ addReferencer(referencer) {
2256
+ (0, utils_1.assert)(referencer, 'Referencer should exists');
2257
+ if (this._referencers) {
2258
+ if (!this._referencers.includes(referencer)) {
2259
+ this._referencers.push(referencer);
2260
+ }
2261
+ }
2262
+ else {
2263
+ this._referencers = [referencer];
2264
+ }
2265
+ }
2266
+ removeReferencer(referencer) {
2267
+ if (this._referencers) {
2268
+ (0, utils_1.removeArrayElement)(referencer, this._referencers);
2269
+ }
2270
+ }
2271
+ removeTypeReference(type) {
2272
+ (0, utils_1.assert)(false, `Directive definition ${this} can't reference other types (it's arguments can); shouldn't be asked to remove reference to ${type}`);
2273
+ }
2274
+ remove() {
2275
+ var _a;
2276
+ if (!this._parent) {
2277
+ return [];
2278
+ }
2279
+ this.checkRemoval();
2280
+ this.onModification();
2281
+ this.sourceAST = undefined;
2282
+ (0, utils_1.assert)(!this._appliedDirectives || this._appliedDirectives.length === 0, "Directive definition should not have directive applied to it");
2283
+ for (const arg of this.arguments()) {
2284
+ arg.remove();
2285
+ }
2286
+ const toReturn = (_a = this._referencers) !== null && _a !== void 0 ? _a : [];
2287
+ this._referencers = undefined;
2288
+ Schema.prototype['removeDirectiveInternal'].call(this._parent, this);
2289
+ this._parent = undefined;
2290
+ return toReturn;
2291
+ }
2292
+ removeRecursive() {
2293
+ this.remove().forEach(ref => ref.remove());
2294
+ }
2295
+ toAST() {
2296
+ const doc = (0, graphql_1.parse)((0, print_1.printDirectiveDefinition)(this));
2297
+ return doc.definitions[0];
2298
+ }
2299
+ toString() {
2300
+ return `@${this.name}`;
2301
+ }
2302
+ }
2303
+ exports.DirectiveDefinition = DirectiveDefinition;
2304
+ class Directive extends Element {
2305
+ constructor(name, _args = Object.create(null)) {
2306
+ super();
2307
+ this.name = name;
2308
+ this._args = _args;
2309
+ }
2310
+ schema() {
2311
+ return this.parent.schema();
2312
+ }
2313
+ get definition() {
2314
+ if (!this.isAttached()) {
2315
+ return undefined;
2316
+ }
2317
+ const doc = this.schema();
2318
+ return doc.directive(this.name);
2319
+ }
2320
+ arguments(includeDefaultValues = false) {
2321
+ if (!includeDefaultValues) {
2322
+ return this._args;
2323
+ }
2324
+ const definition = this.definition;
2325
+ (0, utils_1.assert)(definition, () => `Cannot include default values for arguments: cannot find directive definition for ${this.name}`);
2326
+ const updated = Object.create(null);
2327
+ for (const argDef of definition.arguments()) {
2328
+ const argValue = (0, values_1.withDefaultValues)(this._args[argDef.name], argDef);
2329
+ if (argValue !== undefined) {
2330
+ updated[argDef.name] = argValue;
2331
+ }
2332
+ }
2333
+ return updated;
2334
+ }
2335
+ onModification() {
2336
+ if (this.isAttachedToSchemaElement()) {
2337
+ Schema.prototype['onModification'].call(this.schema());
2338
+ }
2339
+ }
2340
+ isAttachedToSchemaElement() {
2341
+ return this.isAttached();
2342
+ }
2343
+ setArguments(args) {
2344
+ this._args = args;
2345
+ this.onModification();
2346
+ }
2347
+ argumentType(name) {
2348
+ var _a, _b;
2349
+ return (_b = (_a = this.definition) === null || _a === void 0 ? void 0 : _a.argument(name)) === null || _b === void 0 ? void 0 : _b.type;
2350
+ }
2351
+ matchArguments(expectedArgs) {
2352
+ const entries = Object.entries(this._args);
2353
+ if (entries.length !== Object.keys(expectedArgs).length) {
2354
+ return false;
2355
+ }
2356
+ for (const [key, val] of entries) {
2357
+ if (!(key in expectedArgs)) {
2358
+ return false;
2359
+ }
2360
+ const expectedVal = expectedArgs[key];
2361
+ if (!(0, values_1.valueEquals)(expectedVal, val)) {
2362
+ return false;
2363
+ }
2364
+ }
2365
+ return true;
2366
+ }
2367
+ ofExtension() {
2368
+ return this._extension;
2369
+ }
2370
+ removeOfExtension() {
2371
+ this._extension = undefined;
2372
+ }
2373
+ setOfExtension(extension) {
2374
+ this.checkUpdate();
2375
+ if (extension) {
2376
+ const parent = this.parent;
2377
+ (0, utils_1.assert)(parent instanceof SchemaDefinition || parent instanceof BaseNamedType, 'Can only mark directive parts of extensions when directly apply to type or schema definition.');
2378
+ (0, utils_1.assert)(parent.hasExtension(extension), () => `Cannot mark directive ${this.name} as part of the provided extension: it is not an extension of parent ${parent}`);
2379
+ }
2380
+ this._extension = extension;
2381
+ this.onModification();
2382
+ }
2383
+ argumentsToAST() {
2384
+ const entries = Object.entries(this._args);
2385
+ if (entries.length === 0) {
2386
+ return undefined;
2387
+ }
2388
+ const definition = this.definition;
2389
+ (0, utils_1.assert)(definition, () => `Cannot convert arguments of detached directive ${this}`);
2390
+ return entries.map(([n, v]) => {
2391
+ return {
2392
+ kind: graphql_1.Kind.ARGUMENT,
2393
+ name: { kind: graphql_1.Kind.NAME, value: n },
2394
+ value: (0, values_1.valueToAST)(v, definition.argument(n).type),
2395
+ };
2396
+ });
2397
+ }
2398
+ remove() {
2399
+ if (!this._parent) {
2400
+ return false;
2401
+ }
2402
+ this.onModification();
2403
+ const coreFeatures = this.schema().coreFeatures;
2404
+ if (coreFeatures && this.name === coreFeatures.coreItself.nameInSchema) {
2405
+ const url = coreSpec_1.FeatureUrl.parse(this._args[coreFeatures.coreDefinition.urlArgName()]);
2406
+ if (url.identity === coreFeatures.coreItself.url.identity) {
2407
+ Schema.prototype['unmarkAsCoreSchema'].call(this.schema());
2408
+ for (const d of this.schema().schemaDefinition.appliedDirectivesOf(coreFeatures.coreItself.nameInSchema)) {
2409
+ d.removeInternal();
2410
+ }
2411
+ return true;
2412
+ }
2413
+ else {
2414
+ CoreFeatures.prototype['removeFeature'].call(coreFeatures, url.identity);
2415
+ }
2416
+ }
2417
+ return this.removeInternal();
2418
+ }
2419
+ removeInternal() {
2420
+ if (!this._parent) {
2421
+ return false;
2422
+ }
2423
+ const definition = this.definition;
2424
+ if (definition && this.isAttachedToSchemaElement()) {
2425
+ DirectiveDefinition.prototype['removeReferencer'].call(definition, this);
2426
+ }
2427
+ const parentDirectives = this._parent.appliedDirectives;
2428
+ const removed = (0, utils_1.removeArrayElement)(this, parentDirectives);
2429
+ (0, utils_1.assert)(removed, () => `Directive ${this} lists ${this._parent} as parent, but that parent doesn't list it as applied directive`);
2430
+ this._parent = undefined;
2431
+ this._extension = undefined;
2432
+ return true;
2433
+ }
2434
+ toString() {
2435
+ const entries = Object.entries(this._args).filter(([_, v]) => v !== undefined);
2436
+ const args = entries.length == 0 ? '' : '(' + entries.map(([n, v]) => `${n}: ${(0, values_1.valueToString)(v, this.argumentType(n))}`).join(', ') + ')';
2437
+ return `@${this.name}${args}`;
2438
+ }
2439
+ }
2440
+ exports.Directive = Directive;
2441
+ function sameDirectiveApplication(application1, application2, directivesNeverEqualToThemselves = ['defer']) {
2442
+ return application1.name === application2.name
2443
+ && !directivesNeverEqualToThemselves.includes(application1.name)
2444
+ && !directivesNeverEqualToThemselves.includes(application2.name)
2445
+ && (0, values_1.argumentsEquals)(application1.arguments(), application2.arguments());
2446
+ }
2447
+ exports.sameDirectiveApplication = sameDirectiveApplication;
2448
+ function sameDirectiveApplications(applications1, applications2, directivesNeverEqualToThemselves = ['defer']) {
2449
+ if (applications1.length !== applications2.length) {
2450
+ return false;
2451
+ }
2452
+ for (const directive1 of applications1) {
2453
+ if (!applications2.some(directive2 => sameDirectiveApplication(directive1, directive2, directivesNeverEqualToThemselves))) {
2454
+ return false;
2455
+ }
2456
+ }
2457
+ return true;
2458
+ }
2459
+ exports.sameDirectiveApplications = sameDirectiveApplications;
2460
+ function isDirectiveApplicationsSubset(applications, maybeSubset) {
2461
+ if (maybeSubset.length > applications.length) {
2462
+ return false;
2463
+ }
2464
+ for (const directive1 of maybeSubset) {
2465
+ if (!applications.some(directive2 => sameDirectiveApplication(directive1, directive2))) {
2466
+ return false;
2467
+ }
2468
+ }
2469
+ return true;
2470
+ }
2471
+ exports.isDirectiveApplicationsSubset = isDirectiveApplicationsSubset;
2472
+ function directiveApplicationsSubstraction(baseApplications, toRemove) {
2473
+ return baseApplications.filter((application) => !toRemove.some((other) => sameDirectiveApplication(application, other)));
2474
+ }
2475
+ exports.directiveApplicationsSubstraction = directiveApplicationsSubstraction;
2476
+ class Variable {
2477
+ constructor(name) {
2478
+ this.name = name;
2479
+ }
2480
+ toVariableNode() {
2481
+ return {
2482
+ kind: graphql_1.Kind.VARIABLE,
2483
+ name: { kind: graphql_1.Kind.NAME, value: this.name },
2484
+ };
2485
+ }
2486
+ toString() {
2487
+ return '$' + this.name;
2488
+ }
2489
+ }
2490
+ exports.Variable = Variable;
2491
+ class VariableCollector {
2492
+ constructor() {
2493
+ this._variables = new Map();
2494
+ }
2495
+ add(variable) {
2496
+ this._variables.set(variable.name, variable);
2497
+ }
2498
+ addAll(variables) {
2499
+ for (const variable of variables) {
2500
+ this.add(variable);
2501
+ }
2502
+ }
2503
+ collectInArguments(args) {
2504
+ for (const value of Object.values(args)) {
2505
+ (0, values_1.collectVariablesInValue)(value, this);
2506
+ }
2507
+ }
2508
+ variables() {
2509
+ return (0, utils_1.mapValues)(this._variables);
2510
+ }
2511
+ toString() {
2512
+ return this.variables().toString();
2513
+ }
2514
+ }
2515
+ exports.VariableCollector = VariableCollector;
2516
+ function isVariable(v) {
2517
+ return v instanceof Variable;
2518
+ }
2519
+ exports.isVariable = isVariable;
2520
+ class VariableDefinition extends DirectiveTargetElement {
2521
+ constructor(schema, variable, type, defaultValue) {
2522
+ super(schema);
2523
+ this.variable = variable;
2524
+ this.type = type;
2525
+ this.defaultValue = defaultValue;
2526
+ }
2527
+ toVariableDefinitionNode() {
2528
+ const ast = (0, values_1.valueToAST)(this.defaultValue, this.type);
2529
+ return {
2530
+ kind: graphql_1.Kind.VARIABLE_DEFINITION,
2531
+ variable: this.variable.toVariableNode(),
2532
+ type: typeToAST(this.type),
2533
+ defaultValue: (ast !== undefined) ? (0, values_1.valueNodeToConstValueNode)(ast) : undefined,
2534
+ directives: this.appliedDirectivesToDirectiveNodes(),
2535
+ };
2536
+ }
2537
+ toString() {
2538
+ let base = this.variable + ': ' + this.type;
2539
+ if (this.defaultValue !== undefined) {
2540
+ base = base + ' = ' + (0, values_1.valueToString)(this.defaultValue, this.type);
2541
+ }
2542
+ return base + this.appliedDirectivesToString();
2543
+ }
2544
+ }
2545
+ exports.VariableDefinition = VariableDefinition;
2546
+ class VariableDefinitions {
2547
+ constructor() {
2548
+ this._definitions = new utils_1.MapWithCachedArrays();
2549
+ }
2550
+ add(definition) {
2551
+ if (this._definitions.has(definition.variable.name)) {
2552
+ return false;
2553
+ }
2554
+ this._definitions.set(definition.variable.name, definition);
2555
+ return true;
2556
+ }
2557
+ addAll(definitions) {
2558
+ for (const definition of definitions._definitions.values()) {
2559
+ this.add(definition);
2560
+ }
2561
+ }
2562
+ definition(variable) {
2563
+ const varName = typeof variable === 'string' ? variable : variable.name;
2564
+ return this._definitions.get(varName);
2565
+ }
2566
+ isEmpty() {
2567
+ return this._definitions.size === 0;
2568
+ }
2569
+ definitions() {
2570
+ return this._definitions.values();
2571
+ }
2572
+ filter(variables) {
2573
+ if (variables.length === 0) {
2574
+ return new VariableDefinitions();
2575
+ }
2576
+ const newDefs = new VariableDefinitions();
2577
+ for (const variable of variables) {
2578
+ const def = this.definition(variable);
2579
+ if (!def) {
2580
+ throw new Error(`Cannot find variable ${variable} in definitions ${this}`);
2581
+ }
2582
+ newDefs.add(def);
2583
+ }
2584
+ return newDefs;
2585
+ }
2586
+ toVariableDefinitionNodes() {
2587
+ if (this._definitions.size === 0) {
2588
+ return undefined;
2589
+ }
2590
+ return this.definitions().map(def => def.toVariableDefinitionNode());
2591
+ }
2592
+ toString() {
2593
+ return '(' + this.definitions().join(', ') + ')';
2594
+ }
2595
+ }
2596
+ exports.VariableDefinitions = VariableDefinitions;
2597
+ function variableDefinitionsFromAST(schema, definitionNodes) {
2598
+ const definitions = new VariableDefinitions();
2599
+ for (const definitionNode of definitionNodes) {
2600
+ if (!definitions.add(variableDefinitionFromAST(schema, definitionNode))) {
2601
+ const name = definitionNode.variable.name.value;
2602
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Duplicate definition for variable ${name}`, { nodes: definitionNodes.filter(n => n.variable.name.value === name) });
2603
+ }
2604
+ }
2605
+ return definitions;
2606
+ }
2607
+ exports.variableDefinitionsFromAST = variableDefinitionsFromAST;
2608
+ function variableDefinitionFromAST(schema, definitionNode) {
2609
+ const variable = new Variable(definitionNode.variable.name.value);
2610
+ const type = typeFromAST(schema, definitionNode.type);
2611
+ if (!isInputType(type)) {
2612
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Invalid type "${type}" for variable $${variable}: not an input type`, { nodes: definitionNode.type });
2613
+ }
2614
+ const def = new VariableDefinition(schema, variable, type, definitionNode.defaultValue ? (0, values_1.valueFromAST)(definitionNode.defaultValue, type) : undefined);
2615
+ return def;
2616
+ }
2617
+ exports.variableDefinitionFromAST = variableDefinitionFromAST;
2618
+ function addReferenceToType(referencer, type) {
2619
+ switch (type.kind) {
2620
+ case 'ListType':
2621
+ addReferenceToType(referencer, type.baseType());
2622
+ break;
2623
+ case 'NonNullType':
2624
+ addReferenceToType(referencer, type.baseType());
2625
+ break;
2626
+ default:
2627
+ BaseNamedType.prototype['addReferencer'].call(type, referencer);
2628
+ break;
2629
+ }
2630
+ }
2631
+ function removeReferenceToType(referencer, type) {
2632
+ switch (type.kind) {
2633
+ case 'ListType':
2634
+ removeReferenceToType(referencer, type.baseType());
2635
+ break;
2636
+ case 'NonNullType':
2637
+ removeReferenceToType(referencer, type.baseType());
2638
+ break;
2639
+ default:
2640
+ BaseNamedType.prototype['removeReferencer'].call(type, referencer);
2641
+ break;
2642
+ }
2643
+ }
2644
+ function newNamedType(kind, name) {
2645
+ switch (kind) {
2646
+ case 'ScalarType':
2647
+ return new ScalarType(name);
2648
+ case 'ObjectType':
2649
+ return new ObjectType(name);
2650
+ case 'InterfaceType':
2651
+ return new InterfaceType(name);
2652
+ case 'UnionType':
2653
+ return new UnionType(name);
2654
+ case 'EnumType':
2655
+ return new EnumType(name);
2656
+ case 'InputObjectType':
2657
+ return new InputObjectType(name);
2658
+ default:
2659
+ (0, utils_1.assert)(false, `Unhandled kind ${kind} for type ${name}`);
2660
+ }
2661
+ }
2662
+ exports.newNamedType = newNamedType;
2663
+ function* typesToCopy(source, dest) {
2664
+ var _a;
2665
+ for (const type of source.builtInTypes()) {
2666
+ if (!type.isIntrospectionType() && !((_a = dest.type(type.name)) === null || _a === void 0 ? void 0 : _a.isBuiltIn)) {
2667
+ yield type;
2668
+ }
2669
+ }
2670
+ yield* source.types();
2671
+ }
2672
+ function* directivesToCopy(source, dest) {
2673
+ var _a;
2674
+ for (const directive of source.builtInDirectives()) {
2675
+ if (!((_a = dest.directive(directive.name)) === null || _a === void 0 ? void 0 : _a.isBuiltIn)) {
2676
+ yield directive;
2677
+ }
2678
+ }
2679
+ yield* source.directives();
2680
+ }
2681
+ function copyDirectiveDefinitionToSchema({ definition, schema, copyDirectiveApplicationsInArguments = true, locationFilter, }) {
2682
+ copyDirectiveDefinitionInner(definition, schema.addDirectiveDefinition(definition.name), copyDirectiveApplicationsInArguments, locationFilter);
2683
+ }
2684
+ exports.copyDirectiveDefinitionToSchema = copyDirectiveDefinitionToSchema;
2685
+ function copy(source, dest) {
2686
+ for (const type of typesToCopy(source, dest)) {
2687
+ dest.addType(newNamedType(type.kind, type.name));
2688
+ }
2689
+ for (const directive of directivesToCopy(source, dest)) {
2690
+ dest.addDirectiveDefinition(directive.name);
2691
+ }
2692
+ for (const directive of directivesToCopy(source, dest)) {
2693
+ copyDirectiveDefinitionInner(directive, dest.directive(directive.name));
2694
+ }
2695
+ copySchemaDefinitionInner(source.schemaDefinition, dest.schemaDefinition);
2696
+ for (const type of typesToCopy(source, dest)) {
2697
+ copyNamedTypeInner(type, dest.type(type.name));
2698
+ }
2699
+ }
2700
+ function copyExtensions(source, dest) {
2701
+ const extensionMap = new Map();
2702
+ for (const sourceExtension of source.extensions()) {
2703
+ const destExtension = new Extension();
2704
+ dest.addExtension(destExtension);
2705
+ extensionMap.set(sourceExtension, destExtension);
2706
+ }
2707
+ return extensionMap;
2708
+ }
2709
+ function copyOfExtension(extensionsMap, source, dest) {
2710
+ const toCopy = source.ofExtension();
2711
+ if (toCopy) {
2712
+ dest.setOfExtension(extensionsMap.get(toCopy));
2713
+ }
2714
+ }
2715
+ function copySchemaDefinitionInner(source, dest) {
2716
+ dest.preserveEmptyDefinition = source.preserveEmptyDefinition;
2717
+ const extensionsMap = copyExtensions(source, dest);
2718
+ for (const rootType of source.roots()) {
2719
+ copyOfExtension(extensionsMap, rootType, dest.setRoot(rootType.rootKind, rootType.type.name));
2720
+ }
2721
+ for (const directive of source.appliedDirectives) {
2722
+ copyOfExtension(extensionsMap, directive, copyAppliedDirective(directive, dest));
2723
+ }
2724
+ dest.description = source.description;
2725
+ dest.sourceAST = source.sourceAST;
2726
+ }
2727
+ function copyNamedTypeInner(source, dest) {
2728
+ dest.preserveEmptyDefinition = source.preserveEmptyDefinition;
2729
+ const extensionsMap = copyExtensions(source, dest);
2730
+ for (const directive of source.appliedDirectives) {
2731
+ copyOfExtension(extensionsMap, directive, copyAppliedDirective(directive, dest));
2732
+ }
2733
+ dest.description = source.description;
2734
+ dest.sourceAST = source.sourceAST;
2735
+ switch (source.kind) {
2736
+ case 'ObjectType':
2737
+ case 'InterfaceType':
2738
+ const destFieldBasedType = dest;
2739
+ for (const sourceField of source.fields()) {
2740
+ const destField = destFieldBasedType.addField(new FieldDefinition(sourceField.name));
2741
+ copyOfExtension(extensionsMap, sourceField, destField);
2742
+ copyFieldDefinitionInner(sourceField, destField);
2743
+ }
2744
+ for (const sourceImpl of source.interfaceImplementations()) {
2745
+ const destImpl = destFieldBasedType.addImplementedInterface(sourceImpl.interface.name);
2746
+ copyOfExtension(extensionsMap, sourceImpl, destImpl);
2747
+ }
2748
+ break;
2749
+ case 'UnionType':
2750
+ const destUnionType = dest;
2751
+ for (const sourceType of source.members()) {
2752
+ const destType = destUnionType.addType(sourceType.type.name);
2753
+ copyOfExtension(extensionsMap, sourceType, destType);
2754
+ }
2755
+ break;
2756
+ case 'EnumType':
2757
+ const destEnumType = dest;
2758
+ for (const sourceValue of source.values) {
2759
+ const destValue = destEnumType.addValue(sourceValue.name);
2760
+ destValue.description = sourceValue.description;
2761
+ copyOfExtension(extensionsMap, sourceValue, destValue);
2762
+ copyAppliedDirectives(sourceValue, destValue);
2763
+ }
2764
+ break;
2765
+ case 'InputObjectType':
2766
+ const destInputType = dest;
2767
+ for (const sourceField of source.fields()) {
2768
+ const destField = destInputType.addField(new InputFieldDefinition(sourceField.name));
2769
+ copyOfExtension(extensionsMap, sourceField, destField);
2770
+ copyInputFieldDefinitionInner(sourceField, destField);
2771
+ }
2772
+ }
2773
+ }
2774
+ function copyAppliedDirectives(source, dest) {
2775
+ source.appliedDirectives.forEach((d) => copyAppliedDirective(d, dest));
2776
+ }
2777
+ function copyAppliedDirective(source, dest) {
2778
+ const res = dest.applyDirective(source.name, { ...source.arguments() });
2779
+ res.sourceAST = source.sourceAST;
2780
+ return res;
2781
+ }
2782
+ function copyFieldDefinitionInner(source, dest) {
2783
+ const type = copyWrapperTypeOrTypeRef(source.type, dest.schema());
2784
+ dest.type = type;
2785
+ for (const arg of source.arguments()) {
2786
+ const argType = copyWrapperTypeOrTypeRef(arg.type, dest.schema());
2787
+ copyArgumentDefinitionInner(arg, dest.addArgument(arg.name, argType));
2788
+ }
2789
+ copyAppliedDirectives(source, dest);
2790
+ dest.description = source.description;
2791
+ dest.sourceAST = source.sourceAST;
2792
+ }
2793
+ function copyInputFieldDefinitionInner(source, dest) {
2794
+ const type = copyWrapperTypeOrTypeRef(source.type, dest.schema());
2795
+ dest.type = type;
2796
+ dest.defaultValue = source.defaultValue;
2797
+ copyAppliedDirectives(source, dest);
2798
+ dest.description = source.description;
2799
+ dest.sourceAST = source.sourceAST;
2800
+ }
2801
+ function copyWrapperTypeOrTypeRef(source, destParent) {
2802
+ if (!source) {
2803
+ return undefined;
2804
+ }
2805
+ switch (source.kind) {
2806
+ case 'ListType':
2807
+ return new ListType(copyWrapperTypeOrTypeRef(source.ofType, destParent));
2808
+ case 'NonNullType':
2809
+ return new NonNullType(copyWrapperTypeOrTypeRef(source.ofType, destParent));
2810
+ default:
2811
+ return destParent.type(source.name);
2812
+ }
2813
+ }
2814
+ function copyArgumentDefinitionInner(source, dest, copyDirectiveApplications = true) {
2815
+ const type = copyWrapperTypeOrTypeRef(source.type, dest.schema());
2816
+ dest.type = type;
2817
+ dest.defaultValue = source.defaultValue;
2818
+ if (copyDirectiveApplications) {
2819
+ copyAppliedDirectives(source, dest);
2820
+ }
2821
+ dest.description = source.description;
2822
+ dest.sourceAST = source.sourceAST;
2823
+ }
2824
+ function copyDirectiveDefinitionInner(source, dest, copyDirectiveApplicationsInArguments = true, locationFilter) {
2825
+ let locations = source.locations;
2826
+ if (locationFilter) {
2827
+ locations = locations.filter((loc) => locationFilter(loc));
2828
+ }
2829
+ if (locations.length === 0) {
2830
+ return;
2831
+ }
2832
+ for (const arg of source.arguments()) {
2833
+ const type = copyWrapperTypeOrTypeRef(arg.type, dest.schema());
2834
+ copyArgumentDefinitionInner(arg, dest.addArgument(arg.name, type), copyDirectiveApplicationsInArguments);
2835
+ }
2836
+ dest.repeatable = source.repeatable;
2837
+ dest.addLocations(...locations);
2838
+ dest.sourceAST = source.sourceAST;
2839
+ dest.description = source.description;
2840
+ }
2841
+ //# sourceMappingURL=definitions.js.map