@graphql-tools/wrap 8.4.21 → 8.5.0-alpha-b76ec274.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/generateProxyingResolvers.js +74 -0
- package/cjs/index.js +12 -0
- package/cjs/introspect.js +30 -0
- package/cjs/package.json +1 -0
- package/cjs/transforms/ExtractField.js +51 -0
- package/cjs/transforms/FilterInputObjectFields.js +16 -0
- package/cjs/transforms/FilterInterfaceFields.js +13 -0
- package/cjs/transforms/FilterObjectFieldDirectives.js +33 -0
- package/cjs/transforms/FilterObjectFields.js +13 -0
- package/cjs/transforms/FilterRootFields.js +18 -0
- package/cjs/transforms/FilterTypes.js +19 -0
- package/cjs/transforms/HoistField.js +176 -0
- package/cjs/transforms/MapFields.js +61 -0
- package/cjs/transforms/MapLeafValues.js +111 -0
- package/cjs/transforms/PruneSchema.js +12 -0
- package/cjs/transforms/RemoveObjectFieldDeprecations.js +25 -0
- package/cjs/transforms/RemoveObjectFieldDirectives.js +16 -0
- package/cjs/transforms/RemoveObjectFieldsWithDeprecation.js +19 -0
- package/cjs/transforms/RemoveObjectFieldsWithDirective.js +19 -0
- package/cjs/transforms/RenameInputObjectFields.js +57 -0
- package/cjs/transforms/RenameInterfaceFields.js +19 -0
- package/cjs/transforms/RenameObjectFieldArguments.js +72 -0
- package/cjs/transforms/RenameObjectFields.js +19 -0
- package/cjs/transforms/RenameRootFields.js +16 -0
- package/cjs/transforms/RenameRootTypes.js +57 -0
- package/cjs/transforms/RenameTypes.js +69 -0
- package/cjs/transforms/TransformCompositeFields.js +143 -0
- package/cjs/transforms/TransformEnumValues.js +55 -0
- package/cjs/transforms/TransformInputObjectFields.js +160 -0
- package/cjs/transforms/TransformInterfaceFields.js +35 -0
- package/cjs/transforms/TransformObjectFields.js +35 -0
- package/cjs/transforms/TransformQuery.js +89 -0
- package/cjs/transforms/TransformRootFields.js +41 -0
- package/cjs/transforms/WrapFields.js +220 -0
- package/cjs/transforms/WrapQuery.js +62 -0
- package/cjs/transforms/WrapType.js +19 -0
- package/cjs/transforms/index.js +71 -0
- package/cjs/types.js +2 -0
- package/cjs/wrapSchema.js +58 -0
- package/esm/generateProxyingResolvers.js +69 -0
- package/esm/index.js +5 -0
- package/esm/introspect.js +26 -0
- package/esm/transforms/ExtractField.js +48 -0
- package/esm/transforms/FilterInputObjectFields.js +12 -0
- package/esm/transforms/FilterInterfaceFields.js +9 -0
- package/esm/transforms/FilterObjectFieldDirectives.js +29 -0
- package/esm/transforms/FilterObjectFields.js +9 -0
- package/esm/transforms/FilterRootFields.js +14 -0
- package/esm/transforms/FilterTypes.js +16 -0
- package/esm/transforms/HoistField.js +168 -0
- package/esm/transforms/MapFields.js +57 -0
- package/esm/transforms/MapLeafValues.js +108 -0
- package/esm/transforms/PruneSchema.js +9 -0
- package/esm/transforms/RemoveObjectFieldDeprecations.js +21 -0
- package/esm/transforms/RemoveObjectFieldDirectives.js +12 -0
- package/esm/transforms/RemoveObjectFieldsWithDeprecation.js +15 -0
- package/esm/transforms/RemoveObjectFieldsWithDirective.js +15 -0
- package/esm/transforms/RenameInputObjectFields.js +53 -0
- package/esm/transforms/RenameInterfaceFields.js +15 -0
- package/esm/transforms/RenameObjectFieldArguments.js +68 -0
- package/esm/transforms/RenameObjectFields.js +15 -0
- package/esm/transforms/RenameRootFields.js +12 -0
- package/esm/transforms/RenameRootTypes.js +54 -0
- package/esm/transforms/RenameTypes.js +66 -0
- package/esm/transforms/TransformCompositeFields.js +140 -0
- package/esm/transforms/TransformEnumValues.js +51 -0
- package/esm/transforms/TransformInputObjectFields.js +157 -0
- package/esm/transforms/TransformInterfaceFields.js +31 -0
- package/esm/transforms/TransformObjectFields.js +31 -0
- package/esm/transforms/TransformQuery.js +86 -0
- package/esm/transforms/TransformRootFields.js +37 -0
- package/esm/transforms/WrapFields.js +214 -0
- package/esm/transforms/WrapQuery.js +59 -0
- package/esm/transforms/WrapType.js +15 -0
- package/esm/transforms/index.js +33 -0
- package/esm/types.js +1 -0
- package/esm/wrapSchema.js +54 -0
- package/package.json +34 -13
- package/{generateProxyingResolvers.d.ts → typings/generateProxyingResolvers.d.ts} +0 -0
- package/typings/index.d.ts +5 -0
- package/{introspect.d.ts → typings/introspect.d.ts} +0 -0
- package/{transforms → typings/transforms}/ExtractField.d.ts +0 -0
- package/{transforms → typings/transforms}/FilterInputObjectFields.d.ts +1 -1
- package/{transforms → typings/transforms}/FilterInterfaceFields.d.ts +0 -0
- package/{transforms → typings/transforms}/FilterObjectFieldDirectives.d.ts +0 -0
- package/{transforms → typings/transforms}/FilterObjectFields.d.ts +0 -0
- package/{transforms → typings/transforms}/FilterRootFields.d.ts +0 -0
- package/{transforms → typings/transforms}/FilterTypes.d.ts +0 -0
- package/{transforms → typings/transforms}/HoistField.d.ts +0 -0
- package/{transforms → typings/transforms}/MapFields.d.ts +1 -1
- package/{transforms → typings/transforms}/MapLeafValues.d.ts +1 -1
- package/{transforms → typings/transforms}/PruneSchema.d.ts +0 -0
- package/{transforms → typings/transforms}/RemoveObjectFieldDeprecations.d.ts +0 -0
- package/{transforms → typings/transforms}/RemoveObjectFieldDirectives.d.ts +0 -0
- package/{transforms → typings/transforms}/RemoveObjectFieldsWithDeprecation.d.ts +0 -0
- package/{transforms → typings/transforms}/RemoveObjectFieldsWithDirective.d.ts +0 -0
- package/{transforms → typings/transforms}/RenameInputObjectFields.d.ts +0 -0
- package/{transforms → typings/transforms}/RenameInterfaceFields.d.ts +0 -0
- package/{transforms → typings/transforms}/RenameObjectFieldArguments.d.ts +0 -0
- package/{transforms → typings/transforms}/RenameObjectFields.d.ts +0 -0
- package/{transforms → typings/transforms}/RenameRootFields.d.ts +0 -0
- package/{transforms → typings/transforms}/RenameRootTypes.d.ts +0 -0
- package/{transforms → typings/transforms}/RenameTypes.d.ts +0 -0
- package/{transforms → typings/transforms}/TransformCompositeFields.d.ts +1 -1
- package/{transforms → typings/transforms}/TransformEnumValues.d.ts +2 -2
- package/{transforms → typings/transforms}/TransformInputObjectFields.d.ts +1 -1
- package/{transforms → typings/transforms}/TransformInterfaceFields.d.ts +1 -1
- package/{transforms → typings/transforms}/TransformObjectFields.d.ts +1 -1
- package/{transforms → typings/transforms}/TransformQuery.d.ts +0 -0
- package/{transforms → typings/transforms}/TransformRootFields.d.ts +1 -1
- package/{transforms → typings/transforms}/WrapFields.d.ts +0 -0
- package/{transforms → typings/transforms}/WrapQuery.d.ts +0 -0
- package/{transforms → typings/transforms}/WrapType.d.ts +0 -0
- package/typings/transforms/index.d.ts +32 -0
- package/{types.d.ts → typings/types.d.ts} +0 -0
- package/{wrapSchema.d.ts → typings/wrapSchema.d.ts} +0 -0
- package/README.md +0 -5
- package/index.d.ts +0 -5
- package/index.js +0 -1806
- package/index.mjs +0 -1767
- package/transforms/index.d.ts +0 -32
package/index.mjs
DELETED
|
@@ -1,1767 +0,0 @@
|
|
|
1
|
-
import { GraphQLObjectType, GraphQLInterfaceType, GraphQLUnionType, isSpecifiedScalarType, isScalarType, visit, Kind, TypeInfo, visitWithTypeInfo, isObjectType, isInterfaceType, typeFromAST, isInputType, isLeafType, valueFromAST, GraphQLNonNull, getNullableType, BREAK, parse, getIntrospectionQuery, buildClientSchema } from 'graphql';
|
|
2
|
-
import { getRootTypeMap, getResponseKeyFromInfo, mapSchema, MapperKind, renameType, visitData, transformInputValue, visitResult, createVariableNameGenerator, updateArgument, relocatedError, getArgumentValues, valueMatchesCriteria, getDirectives, pruneSchema, selectObjectFields, appendObjectFields, modifyObjectFields, removeObjectFields, isAsyncIterable } from '@graphql-tools/utils';
|
|
3
|
-
import { applySchemaTransforms, delegateToSchema, isExternalObject, getUnpathedErrors, getSubschema, resolveExternalValue, defaultMergedResolver } from '@graphql-tools/delegate';
|
|
4
|
-
import { ValueOrPromise } from 'value-or-promise';
|
|
5
|
-
|
|
6
|
-
function generateProxyingResolvers(subschemaConfig) {
|
|
7
|
-
var _a;
|
|
8
|
-
const targetSchema = subschemaConfig.schema;
|
|
9
|
-
const createProxyingResolver = (_a = subschemaConfig.createProxyingResolver) !== null && _a !== void 0 ? _a : defaultCreateProxyingResolver;
|
|
10
|
-
const transformedSchema = applySchemaTransforms(targetSchema, subschemaConfig);
|
|
11
|
-
const rootTypeMap = getRootTypeMap(targetSchema);
|
|
12
|
-
const resolvers = {};
|
|
13
|
-
for (const [operation, rootType] of rootTypeMap.entries()) {
|
|
14
|
-
const typeName = rootType.name;
|
|
15
|
-
const fields = rootType.getFields();
|
|
16
|
-
resolvers[typeName] = {};
|
|
17
|
-
for (const fieldName in fields) {
|
|
18
|
-
const proxyingResolver = createProxyingResolver({
|
|
19
|
-
subschemaConfig,
|
|
20
|
-
transformedSchema,
|
|
21
|
-
operation,
|
|
22
|
-
fieldName,
|
|
23
|
-
});
|
|
24
|
-
const finalResolver = createPossiblyNestedProxyingResolver(subschemaConfig, proxyingResolver);
|
|
25
|
-
if (operation === 'subscription') {
|
|
26
|
-
resolvers[typeName][fieldName] = {
|
|
27
|
-
subscribe: finalResolver,
|
|
28
|
-
resolve: identical,
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
else {
|
|
32
|
-
resolvers[typeName][fieldName] = {
|
|
33
|
-
resolve: finalResolver,
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
return resolvers;
|
|
39
|
-
}
|
|
40
|
-
function identical(value) {
|
|
41
|
-
return value;
|
|
42
|
-
}
|
|
43
|
-
function createPossiblyNestedProxyingResolver(subschemaConfig, proxyingResolver) {
|
|
44
|
-
return function possiblyNestedProxyingResolver(parent, args, context, info) {
|
|
45
|
-
if (parent != null) {
|
|
46
|
-
const responseKey = getResponseKeyFromInfo(info);
|
|
47
|
-
// Check to see if the parent contains a proxied result
|
|
48
|
-
if (isExternalObject(parent)) {
|
|
49
|
-
const unpathedErrors = getUnpathedErrors(parent);
|
|
50
|
-
const subschema = getSubschema(parent, responseKey);
|
|
51
|
-
// If there is a proxied result from this subschema, return it
|
|
52
|
-
// This can happen even for a root field when the root type ia
|
|
53
|
-
// also nested as a field within a different type.
|
|
54
|
-
if (subschemaConfig === subschema && parent[responseKey] !== undefined) {
|
|
55
|
-
return resolveExternalValue(parent[responseKey], unpathedErrors, subschema, context, info);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
return proxyingResolver(parent, args, context, info);
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
function defaultCreateProxyingResolver({ subschemaConfig, operation, transformedSchema, }) {
|
|
63
|
-
return function proxyingResolver(_parent, _args, context, info) {
|
|
64
|
-
return delegateToSchema({
|
|
65
|
-
schema: subschemaConfig,
|
|
66
|
-
operation,
|
|
67
|
-
context,
|
|
68
|
-
info,
|
|
69
|
-
transformedSchema,
|
|
70
|
-
});
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function wrapSchema(subschemaConfig) {
|
|
75
|
-
const targetSchema = subschemaConfig.schema;
|
|
76
|
-
const proxyingResolvers = generateProxyingResolvers(subschemaConfig);
|
|
77
|
-
const schema = createWrappingSchema(targetSchema, proxyingResolvers);
|
|
78
|
-
const transformedSchema = applySchemaTransforms(schema, subschemaConfig);
|
|
79
|
-
return applySchemaTransforms(schema, subschemaConfig, transformedSchema);
|
|
80
|
-
}
|
|
81
|
-
function createWrappingSchema(schema, proxyingResolvers) {
|
|
82
|
-
return mapSchema(schema, {
|
|
83
|
-
[MapperKind.ROOT_OBJECT]: type => {
|
|
84
|
-
var _a;
|
|
85
|
-
const config = type.toConfig();
|
|
86
|
-
const fieldConfigMap = config.fields;
|
|
87
|
-
for (const fieldName in fieldConfigMap) {
|
|
88
|
-
const field = fieldConfigMap[fieldName];
|
|
89
|
-
if (field == null) {
|
|
90
|
-
continue;
|
|
91
|
-
}
|
|
92
|
-
fieldConfigMap[fieldName] = {
|
|
93
|
-
...field,
|
|
94
|
-
...(_a = proxyingResolvers[type.name]) === null || _a === void 0 ? void 0 : _a[fieldName],
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
return new GraphQLObjectType(config);
|
|
98
|
-
},
|
|
99
|
-
[MapperKind.OBJECT_TYPE]: type => {
|
|
100
|
-
const config = type.toConfig();
|
|
101
|
-
config.isTypeOf = undefined;
|
|
102
|
-
for (const fieldName in config.fields) {
|
|
103
|
-
const field = config.fields[fieldName];
|
|
104
|
-
if (field == null) {
|
|
105
|
-
continue;
|
|
106
|
-
}
|
|
107
|
-
field.resolve = defaultMergedResolver;
|
|
108
|
-
field.subscribe = undefined;
|
|
109
|
-
}
|
|
110
|
-
return new GraphQLObjectType(config);
|
|
111
|
-
},
|
|
112
|
-
[MapperKind.INTERFACE_TYPE]: type => {
|
|
113
|
-
const config = type.toConfig();
|
|
114
|
-
delete config.resolveType;
|
|
115
|
-
return new GraphQLInterfaceType(config);
|
|
116
|
-
},
|
|
117
|
-
[MapperKind.UNION_TYPE]: type => {
|
|
118
|
-
const config = type.toConfig();
|
|
119
|
-
delete config.resolveType;
|
|
120
|
-
return new GraphQLUnionType(config);
|
|
121
|
-
},
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
class RenameTypes {
|
|
126
|
-
constructor(renamer, options) {
|
|
127
|
-
this.renamer = renamer;
|
|
128
|
-
this.map = Object.create(null);
|
|
129
|
-
this.reverseMap = Object.create(null);
|
|
130
|
-
const { renameBuiltins = false, renameScalars = true } = options != null ? options : {};
|
|
131
|
-
this.renameBuiltins = renameBuiltins;
|
|
132
|
-
this.renameScalars = renameScalars;
|
|
133
|
-
}
|
|
134
|
-
transformSchema(originalWrappingSchema, _subschemaConfig, _transformedSchema) {
|
|
135
|
-
return mapSchema(originalWrappingSchema, {
|
|
136
|
-
[MapperKind.TYPE]: (type) => {
|
|
137
|
-
if (isSpecifiedScalarType(type) && !this.renameBuiltins) {
|
|
138
|
-
return undefined;
|
|
139
|
-
}
|
|
140
|
-
if (isScalarType(type) && !this.renameScalars) {
|
|
141
|
-
return undefined;
|
|
142
|
-
}
|
|
143
|
-
const oldName = type.name;
|
|
144
|
-
const newName = this.renamer(oldName);
|
|
145
|
-
if (newName !== undefined && newName !== oldName) {
|
|
146
|
-
this.map[oldName] = newName;
|
|
147
|
-
this.reverseMap[newName] = oldName;
|
|
148
|
-
return renameType(type, newName);
|
|
149
|
-
}
|
|
150
|
-
},
|
|
151
|
-
[MapperKind.ROOT_OBJECT]() {
|
|
152
|
-
return undefined;
|
|
153
|
-
},
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
|
-
transformRequest(originalRequest, _delegationContext, _transformationContext) {
|
|
157
|
-
const document = visit(originalRequest.document, {
|
|
158
|
-
[Kind.NAMED_TYPE]: (node) => {
|
|
159
|
-
const name = node.name.value;
|
|
160
|
-
if (name in this.reverseMap) {
|
|
161
|
-
return {
|
|
162
|
-
...node,
|
|
163
|
-
name: {
|
|
164
|
-
kind: Kind.NAME,
|
|
165
|
-
value: this.reverseMap[name],
|
|
166
|
-
},
|
|
167
|
-
};
|
|
168
|
-
}
|
|
169
|
-
},
|
|
170
|
-
});
|
|
171
|
-
return {
|
|
172
|
-
...originalRequest,
|
|
173
|
-
document,
|
|
174
|
-
};
|
|
175
|
-
}
|
|
176
|
-
transformResult(originalResult, _delegationContext, _transformationContext) {
|
|
177
|
-
return {
|
|
178
|
-
...originalResult,
|
|
179
|
-
data: visitData(originalResult.data, object => {
|
|
180
|
-
const typeName = object === null || object === void 0 ? void 0 : object.__typename;
|
|
181
|
-
if (typeName != null && typeName in this.map) {
|
|
182
|
-
object.__typename = this.map[typeName];
|
|
183
|
-
}
|
|
184
|
-
return object;
|
|
185
|
-
}),
|
|
186
|
-
};
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
class FilterTypes {
|
|
191
|
-
constructor(filter) {
|
|
192
|
-
this.filter = filter;
|
|
193
|
-
}
|
|
194
|
-
transformSchema(originalWrappingSchema, _subschemaConfig, _transformedSchema) {
|
|
195
|
-
return mapSchema(originalWrappingSchema, {
|
|
196
|
-
[MapperKind.TYPE]: (type) => {
|
|
197
|
-
if (this.filter(type)) {
|
|
198
|
-
return undefined;
|
|
199
|
-
}
|
|
200
|
-
return null;
|
|
201
|
-
},
|
|
202
|
-
});
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
class RenameRootTypes {
|
|
207
|
-
constructor(renamer) {
|
|
208
|
-
this.renamer = renamer;
|
|
209
|
-
this.map = Object.create(null);
|
|
210
|
-
this.reverseMap = Object.create(null);
|
|
211
|
-
}
|
|
212
|
-
transformSchema(originalWrappingSchema, _subschemaConfig, _transformedSchema) {
|
|
213
|
-
return mapSchema(originalWrappingSchema, {
|
|
214
|
-
[MapperKind.ROOT_OBJECT]: type => {
|
|
215
|
-
const oldName = type.name;
|
|
216
|
-
const newName = this.renamer(oldName);
|
|
217
|
-
if (newName !== undefined && newName !== oldName) {
|
|
218
|
-
this.map[oldName] = newName;
|
|
219
|
-
this.reverseMap[newName] = oldName;
|
|
220
|
-
return renameType(type, newName);
|
|
221
|
-
}
|
|
222
|
-
},
|
|
223
|
-
});
|
|
224
|
-
}
|
|
225
|
-
transformRequest(originalRequest, _delegationContext, _transformationContext) {
|
|
226
|
-
const document = visit(originalRequest.document, {
|
|
227
|
-
[Kind.NAMED_TYPE]: (node) => {
|
|
228
|
-
const name = node.name.value;
|
|
229
|
-
if (name in this.reverseMap) {
|
|
230
|
-
return {
|
|
231
|
-
...node,
|
|
232
|
-
name: {
|
|
233
|
-
kind: Kind.NAME,
|
|
234
|
-
value: this.reverseMap[name],
|
|
235
|
-
},
|
|
236
|
-
};
|
|
237
|
-
}
|
|
238
|
-
},
|
|
239
|
-
});
|
|
240
|
-
return {
|
|
241
|
-
...originalRequest,
|
|
242
|
-
document,
|
|
243
|
-
};
|
|
244
|
-
}
|
|
245
|
-
transformResult(originalResult, _delegationContext, _transformationContext) {
|
|
246
|
-
return {
|
|
247
|
-
...originalResult,
|
|
248
|
-
data: visitData(originalResult.data, object => {
|
|
249
|
-
const typeName = object === null || object === void 0 ? void 0 : object.__typename;
|
|
250
|
-
if (typeName != null && typeName in this.map) {
|
|
251
|
-
object.__typename = this.map[typeName];
|
|
252
|
-
}
|
|
253
|
-
return object;
|
|
254
|
-
}),
|
|
255
|
-
};
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
class TransformCompositeFields {
|
|
260
|
-
constructor(fieldTransformer, fieldNodeTransformer, dataTransformer, errorsTransformer) {
|
|
261
|
-
this.fieldTransformer = fieldTransformer;
|
|
262
|
-
this.fieldNodeTransformer = fieldNodeTransformer;
|
|
263
|
-
this.dataTransformer = dataTransformer;
|
|
264
|
-
this.errorsTransformer = errorsTransformer;
|
|
265
|
-
this.mapping = {};
|
|
266
|
-
}
|
|
267
|
-
_getTypeInfo() {
|
|
268
|
-
const typeInfo = this.typeInfo;
|
|
269
|
-
if (typeInfo === undefined) {
|
|
270
|
-
throw new Error(`The TransformCompositeFields transform's "transformRequest" and "transformResult" methods cannot be used without first calling "transformSchema".`);
|
|
271
|
-
}
|
|
272
|
-
return typeInfo;
|
|
273
|
-
}
|
|
274
|
-
transformSchema(originalWrappingSchema, _subschemaConfig, _transformedSchema) {
|
|
275
|
-
var _a;
|
|
276
|
-
this.transformedSchema = mapSchema(originalWrappingSchema, {
|
|
277
|
-
[MapperKind.COMPOSITE_FIELD]: (fieldConfig, fieldName, typeName) => {
|
|
278
|
-
const transformedField = this.fieldTransformer(typeName, fieldName, fieldConfig);
|
|
279
|
-
if (Array.isArray(transformedField)) {
|
|
280
|
-
const newFieldName = transformedField[0];
|
|
281
|
-
if (newFieldName !== fieldName) {
|
|
282
|
-
if (!(typeName in this.mapping)) {
|
|
283
|
-
this.mapping[typeName] = {};
|
|
284
|
-
}
|
|
285
|
-
this.mapping[typeName][newFieldName] = fieldName;
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
return transformedField;
|
|
289
|
-
},
|
|
290
|
-
});
|
|
291
|
-
this.typeInfo = new TypeInfo(this.transformedSchema);
|
|
292
|
-
this.subscriptionTypeName = (_a = originalWrappingSchema.getSubscriptionType()) === null || _a === void 0 ? void 0 : _a.name;
|
|
293
|
-
return this.transformedSchema;
|
|
294
|
-
}
|
|
295
|
-
transformRequest(originalRequest, _delegationContext, transformationContext) {
|
|
296
|
-
const document = originalRequest.document;
|
|
297
|
-
return {
|
|
298
|
-
...originalRequest,
|
|
299
|
-
document: this.transformDocument(document, transformationContext),
|
|
300
|
-
};
|
|
301
|
-
}
|
|
302
|
-
transformResult(result, _delegationContext, transformationContext) {
|
|
303
|
-
const dataTransformer = this.dataTransformer;
|
|
304
|
-
if (dataTransformer != null) {
|
|
305
|
-
result.data = visitData(result.data, value => dataTransformer(value, transformationContext));
|
|
306
|
-
}
|
|
307
|
-
if (this.errorsTransformer != null && Array.isArray(result.errors)) {
|
|
308
|
-
result.errors = this.errorsTransformer(result.errors, transformationContext);
|
|
309
|
-
}
|
|
310
|
-
return result;
|
|
311
|
-
}
|
|
312
|
-
transformDocument(document, transformationContext) {
|
|
313
|
-
const fragments = Object.create(null);
|
|
314
|
-
for (const def of document.definitions) {
|
|
315
|
-
if (def.kind === Kind.FRAGMENT_DEFINITION) {
|
|
316
|
-
fragments[def.name.value] = def;
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
return visit(document, visitWithTypeInfo(this._getTypeInfo(), {
|
|
320
|
-
[Kind.SELECTION_SET]: {
|
|
321
|
-
leave: node => this.transformSelectionSet(node, this._getTypeInfo(), fragments, transformationContext),
|
|
322
|
-
},
|
|
323
|
-
}));
|
|
324
|
-
}
|
|
325
|
-
transformSelectionSet(node, typeInfo, fragments, transformationContext) {
|
|
326
|
-
var _a, _b;
|
|
327
|
-
const parentType = typeInfo.getParentType();
|
|
328
|
-
if (parentType == null) {
|
|
329
|
-
return undefined;
|
|
330
|
-
}
|
|
331
|
-
const parentTypeName = parentType.name;
|
|
332
|
-
let newSelections = [];
|
|
333
|
-
for (const selection of node.selections) {
|
|
334
|
-
if (selection.kind !== Kind.FIELD) {
|
|
335
|
-
newSelections.push(selection);
|
|
336
|
-
continue;
|
|
337
|
-
}
|
|
338
|
-
const newName = selection.name.value;
|
|
339
|
-
// See https://github.com/ardatan/graphql-tools/issues/2282
|
|
340
|
-
if ((this.dataTransformer != null || this.errorsTransformer != null) &&
|
|
341
|
-
(this.subscriptionTypeName == null || parentTypeName !== this.subscriptionTypeName)) {
|
|
342
|
-
newSelections.push({
|
|
343
|
-
kind: Kind.FIELD,
|
|
344
|
-
name: {
|
|
345
|
-
kind: Kind.NAME,
|
|
346
|
-
value: '__typename',
|
|
347
|
-
},
|
|
348
|
-
});
|
|
349
|
-
}
|
|
350
|
-
let transformedSelection;
|
|
351
|
-
if (this.fieldNodeTransformer == null) {
|
|
352
|
-
transformedSelection = selection;
|
|
353
|
-
}
|
|
354
|
-
else {
|
|
355
|
-
transformedSelection = this.fieldNodeTransformer(parentTypeName, newName, selection, fragments, transformationContext);
|
|
356
|
-
transformedSelection = transformedSelection === undefined ? selection : transformedSelection;
|
|
357
|
-
}
|
|
358
|
-
if (transformedSelection == null) {
|
|
359
|
-
continue;
|
|
360
|
-
}
|
|
361
|
-
else if (Array.isArray(transformedSelection)) {
|
|
362
|
-
newSelections = newSelections.concat(transformedSelection);
|
|
363
|
-
continue;
|
|
364
|
-
}
|
|
365
|
-
else if (transformedSelection.kind !== Kind.FIELD) {
|
|
366
|
-
newSelections.push(transformedSelection);
|
|
367
|
-
continue;
|
|
368
|
-
}
|
|
369
|
-
const typeMapping = this.mapping[parentTypeName];
|
|
370
|
-
if (typeMapping == null) {
|
|
371
|
-
newSelections.push(transformedSelection);
|
|
372
|
-
continue;
|
|
373
|
-
}
|
|
374
|
-
const oldName = this.mapping[parentTypeName][newName];
|
|
375
|
-
if (oldName == null) {
|
|
376
|
-
newSelections.push(transformedSelection);
|
|
377
|
-
continue;
|
|
378
|
-
}
|
|
379
|
-
newSelections.push({
|
|
380
|
-
...transformedSelection,
|
|
381
|
-
name: {
|
|
382
|
-
kind: Kind.NAME,
|
|
383
|
-
value: oldName,
|
|
384
|
-
},
|
|
385
|
-
alias: {
|
|
386
|
-
kind: Kind.NAME,
|
|
387
|
-
value: (_b = (_a = transformedSelection.alias) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : newName,
|
|
388
|
-
},
|
|
389
|
-
});
|
|
390
|
-
}
|
|
391
|
-
return {
|
|
392
|
-
...node,
|
|
393
|
-
selections: newSelections,
|
|
394
|
-
};
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
class TransformObjectFields {
|
|
399
|
-
constructor(objectFieldTransformer, fieldNodeTransformer) {
|
|
400
|
-
this.objectFieldTransformer = objectFieldTransformer;
|
|
401
|
-
this.fieldNodeTransformer = fieldNodeTransformer;
|
|
402
|
-
}
|
|
403
|
-
_getTransformer() {
|
|
404
|
-
const transformer = this.transformer;
|
|
405
|
-
if (transformer === undefined) {
|
|
406
|
-
throw new Error(`The TransformObjectFields transform's "transformRequest" and "transformResult" methods cannot be used without first calling "transformSchema".`);
|
|
407
|
-
}
|
|
408
|
-
return transformer;
|
|
409
|
-
}
|
|
410
|
-
transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
|
411
|
-
const compositeToObjectFieldTransformer = (typeName, fieldName, fieldConfig) => {
|
|
412
|
-
if (isObjectType(originalWrappingSchema.getType(typeName))) {
|
|
413
|
-
return this.objectFieldTransformer(typeName, fieldName, fieldConfig);
|
|
414
|
-
}
|
|
415
|
-
return undefined;
|
|
416
|
-
};
|
|
417
|
-
this.transformer = new TransformCompositeFields(compositeToObjectFieldTransformer, this.fieldNodeTransformer);
|
|
418
|
-
return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
|
419
|
-
}
|
|
420
|
-
transformRequest(originalRequest, delegationContext, transformationContext) {
|
|
421
|
-
return this._getTransformer().transformRequest(originalRequest, delegationContext, transformationContext);
|
|
422
|
-
}
|
|
423
|
-
transformResult(originalResult, delegationContext, transformationContext) {
|
|
424
|
-
return this._getTransformer().transformResult(originalResult, delegationContext, transformationContext);
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
class TransformRootFields {
|
|
429
|
-
constructor(rootFieldTransformer, fieldNodeTransformer) {
|
|
430
|
-
this.rootFieldTransformer = rootFieldTransformer;
|
|
431
|
-
this.fieldNodeTransformer = fieldNodeTransformer;
|
|
432
|
-
}
|
|
433
|
-
_getTransformer() {
|
|
434
|
-
const transformer = this.transformer;
|
|
435
|
-
if (transformer === undefined) {
|
|
436
|
-
throw new Error(`The TransformRootFields transform's "transformRequest" and "transformResult" methods cannot be used without first calling "transformSchema".`);
|
|
437
|
-
}
|
|
438
|
-
return transformer;
|
|
439
|
-
}
|
|
440
|
-
transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
|
441
|
-
const rootToObjectFieldTransformer = (typeName, fieldName, fieldConfig) => {
|
|
442
|
-
var _a, _b, _c;
|
|
443
|
-
if (typeName === ((_a = originalWrappingSchema.getQueryType()) === null || _a === void 0 ? void 0 : _a.name)) {
|
|
444
|
-
return this.rootFieldTransformer('Query', fieldName, fieldConfig);
|
|
445
|
-
}
|
|
446
|
-
if (typeName === ((_b = originalWrappingSchema.getMutationType()) === null || _b === void 0 ? void 0 : _b.name)) {
|
|
447
|
-
return this.rootFieldTransformer('Mutation', fieldName, fieldConfig);
|
|
448
|
-
}
|
|
449
|
-
if (typeName === ((_c = originalWrappingSchema.getSubscriptionType()) === null || _c === void 0 ? void 0 : _c.name)) {
|
|
450
|
-
return this.rootFieldTransformer('Subscription', fieldName, fieldConfig);
|
|
451
|
-
}
|
|
452
|
-
return undefined;
|
|
453
|
-
};
|
|
454
|
-
this.transformer = new TransformObjectFields(rootToObjectFieldTransformer, this.fieldNodeTransformer);
|
|
455
|
-
return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
|
456
|
-
}
|
|
457
|
-
transformRequest(originalRequest, delegationContext, transformationContext) {
|
|
458
|
-
return this._getTransformer().transformRequest(originalRequest, delegationContext, transformationContext);
|
|
459
|
-
}
|
|
460
|
-
transformResult(originalResult, delegationContext, transformationContext) {
|
|
461
|
-
return this._getTransformer().transformResult(originalResult, delegationContext, transformationContext);
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
class RenameRootFields {
|
|
466
|
-
constructor(renamer) {
|
|
467
|
-
this.transformer = new TransformRootFields((operation, fieldName, fieldConfig) => [renamer(operation, fieldName, fieldConfig), fieldConfig]);
|
|
468
|
-
}
|
|
469
|
-
transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
|
470
|
-
return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
|
471
|
-
}
|
|
472
|
-
transformRequest(originalRequest, delegationContext, transformationContext) {
|
|
473
|
-
return this.transformer.transformRequest(originalRequest, delegationContext, transformationContext);
|
|
474
|
-
}
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
class FilterRootFields {
|
|
478
|
-
constructor(filter) {
|
|
479
|
-
this.transformer = new TransformRootFields((operation, fieldName, fieldConfig) => {
|
|
480
|
-
if (filter(operation, fieldName, fieldConfig)) {
|
|
481
|
-
return undefined;
|
|
482
|
-
}
|
|
483
|
-
return null;
|
|
484
|
-
});
|
|
485
|
-
}
|
|
486
|
-
transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
|
487
|
-
return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
class RenameObjectFields {
|
|
492
|
-
constructor(renamer) {
|
|
493
|
-
this.transformer = new TransformObjectFields((typeName, fieldName, fieldConfig) => [
|
|
494
|
-
renamer(typeName, fieldName, fieldConfig),
|
|
495
|
-
fieldConfig,
|
|
496
|
-
]);
|
|
497
|
-
}
|
|
498
|
-
transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
|
499
|
-
return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
|
500
|
-
}
|
|
501
|
-
transformRequest(originalRequest, delegationContext, transformationContext) {
|
|
502
|
-
return this.transformer.transformRequest(originalRequest, delegationContext, transformationContext);
|
|
503
|
-
}
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
class RenameObjectFieldArguments {
|
|
507
|
-
constructor(renamer) {
|
|
508
|
-
this.renamer = renamer;
|
|
509
|
-
this.transformer = new TransformObjectFields((typeName, fieldName, fieldConfig) => {
|
|
510
|
-
const argsConfig = Object.fromEntries(Object.entries(fieldConfig.args || []).map(([argName, conf]) => {
|
|
511
|
-
const newName = renamer(typeName, fieldName, argName);
|
|
512
|
-
if (newName !== undefined && newName !== argName) {
|
|
513
|
-
if (newName != null) {
|
|
514
|
-
return [newName, conf];
|
|
515
|
-
}
|
|
516
|
-
}
|
|
517
|
-
return [argName, conf];
|
|
518
|
-
}));
|
|
519
|
-
return [fieldName, { ...fieldConfig, args: argsConfig }];
|
|
520
|
-
}, (typeName, fieldName, inputFieldNode) => {
|
|
521
|
-
if (!(typeName in this.reverseMap)) {
|
|
522
|
-
return inputFieldNode;
|
|
523
|
-
}
|
|
524
|
-
if (!(fieldName in this.reverseMap[typeName])) {
|
|
525
|
-
return inputFieldNode;
|
|
526
|
-
}
|
|
527
|
-
const fieldNameMap = this.reverseMap[typeName][fieldName];
|
|
528
|
-
return {
|
|
529
|
-
...inputFieldNode,
|
|
530
|
-
arguments: (inputFieldNode.arguments || []).map(argNode => {
|
|
531
|
-
return argNode.name.value in fieldNameMap
|
|
532
|
-
? {
|
|
533
|
-
...argNode,
|
|
534
|
-
name: {
|
|
535
|
-
...argNode.name,
|
|
536
|
-
value: fieldNameMap[argNode.name.value],
|
|
537
|
-
},
|
|
538
|
-
}
|
|
539
|
-
: argNode;
|
|
540
|
-
}),
|
|
541
|
-
};
|
|
542
|
-
});
|
|
543
|
-
this.reverseMap = Object.create(null);
|
|
544
|
-
}
|
|
545
|
-
transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
|
546
|
-
mapSchema(originalWrappingSchema, {
|
|
547
|
-
[MapperKind.OBJECT_FIELD]: (fieldConfig, fieldName, typeName) => {
|
|
548
|
-
Object.entries(fieldConfig.args || {}).forEach(([argName]) => {
|
|
549
|
-
const newName = this.renamer(typeName, fieldName, argName);
|
|
550
|
-
if (newName !== undefined && newName !== fieldName) {
|
|
551
|
-
if (this.reverseMap[typeName] == null) {
|
|
552
|
-
this.reverseMap[typeName] = Object.create(null);
|
|
553
|
-
}
|
|
554
|
-
if (this.reverseMap[typeName][fieldName] == null) {
|
|
555
|
-
this.reverseMap[typeName][fieldName] = Object.create(null);
|
|
556
|
-
}
|
|
557
|
-
this.reverseMap[typeName][fieldName][newName] = argName;
|
|
558
|
-
}
|
|
559
|
-
});
|
|
560
|
-
return undefined;
|
|
561
|
-
},
|
|
562
|
-
[MapperKind.ROOT_OBJECT]() {
|
|
563
|
-
return undefined;
|
|
564
|
-
},
|
|
565
|
-
});
|
|
566
|
-
return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
|
567
|
-
}
|
|
568
|
-
transformRequest(originalRequest, delegationContext, transformationContext) {
|
|
569
|
-
return this.transformer.transformRequest(originalRequest, delegationContext, transformationContext);
|
|
570
|
-
}
|
|
571
|
-
}
|
|
572
|
-
|
|
573
|
-
class FilterObjectFields {
|
|
574
|
-
constructor(filter) {
|
|
575
|
-
this.transformer = new TransformObjectFields((typeName, fieldName, fieldConfig) => filter(typeName, fieldName, fieldConfig) ? undefined : null);
|
|
576
|
-
}
|
|
577
|
-
transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
|
578
|
-
return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
|
579
|
-
}
|
|
580
|
-
}
|
|
581
|
-
|
|
582
|
-
class TransformInterfaceFields {
|
|
583
|
-
constructor(interfaceFieldTransformer, fieldNodeTransformer) {
|
|
584
|
-
this.interfaceFieldTransformer = interfaceFieldTransformer;
|
|
585
|
-
this.fieldNodeTransformer = fieldNodeTransformer;
|
|
586
|
-
}
|
|
587
|
-
_getTransformer() {
|
|
588
|
-
const transformer = this.transformer;
|
|
589
|
-
if (transformer === undefined) {
|
|
590
|
-
throw new Error(`The TransformInterfaceFields transform's "transformRequest" and "transformResult" methods cannot be used without first calling "transformSchema".`);
|
|
591
|
-
}
|
|
592
|
-
return transformer;
|
|
593
|
-
}
|
|
594
|
-
transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
|
595
|
-
const compositeToObjectFieldTransformer = (typeName, fieldName, fieldConfig) => {
|
|
596
|
-
if (isInterfaceType(originalWrappingSchema.getType(typeName))) {
|
|
597
|
-
return this.interfaceFieldTransformer(typeName, fieldName, fieldConfig);
|
|
598
|
-
}
|
|
599
|
-
return undefined;
|
|
600
|
-
};
|
|
601
|
-
this.transformer = new TransformCompositeFields(compositeToObjectFieldTransformer, this.fieldNodeTransformer);
|
|
602
|
-
return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
|
603
|
-
}
|
|
604
|
-
transformRequest(originalRequest, delegationContext, transformationContext) {
|
|
605
|
-
return this._getTransformer().transformRequest(originalRequest, delegationContext, transformationContext);
|
|
606
|
-
}
|
|
607
|
-
transformResult(originalResult, delegationContext, transformationContext) {
|
|
608
|
-
return this._getTransformer().transformResult(originalResult, delegationContext, transformationContext);
|
|
609
|
-
}
|
|
610
|
-
}
|
|
611
|
-
|
|
612
|
-
class RenameInterfaceFields {
|
|
613
|
-
constructor(renamer) {
|
|
614
|
-
this.transformer = new TransformInterfaceFields((typeName, fieldName, fieldConfig) => [
|
|
615
|
-
renamer(typeName, fieldName, fieldConfig),
|
|
616
|
-
fieldConfig,
|
|
617
|
-
]);
|
|
618
|
-
}
|
|
619
|
-
transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
|
620
|
-
return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
|
621
|
-
}
|
|
622
|
-
transformRequest(originalRequest, delegationContext, transformationContext) {
|
|
623
|
-
return this.transformer.transformRequest(originalRequest, delegationContext, transformationContext);
|
|
624
|
-
}
|
|
625
|
-
}
|
|
626
|
-
|
|
627
|
-
class FilterInterfaceFields {
|
|
628
|
-
constructor(filter) {
|
|
629
|
-
this.transformer = new TransformInterfaceFields((typeName, fieldName, fieldConfig) => filter(typeName, fieldName, fieldConfig) ? undefined : null);
|
|
630
|
-
}
|
|
631
|
-
transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
|
632
|
-
return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
|
633
|
-
}
|
|
634
|
-
}
|
|
635
|
-
|
|
636
|
-
class TransformInputObjectFields {
|
|
637
|
-
constructor(inputFieldTransformer, inputFieldNodeTransformer, inputObjectNodeTransformer) {
|
|
638
|
-
this.inputFieldTransformer = inputFieldTransformer;
|
|
639
|
-
this.inputFieldNodeTransformer = inputFieldNodeTransformer;
|
|
640
|
-
this.inputObjectNodeTransformer = inputObjectNodeTransformer;
|
|
641
|
-
this.mapping = {};
|
|
642
|
-
}
|
|
643
|
-
_getTransformedSchema() {
|
|
644
|
-
const transformedSchema = this.transformedSchema;
|
|
645
|
-
if (transformedSchema === undefined) {
|
|
646
|
-
throw new Error(`The TransformInputObjectFields transform's "transformRequest" and "transformResult" methods cannot be used without first calling "transformSchema".`);
|
|
647
|
-
}
|
|
648
|
-
return transformedSchema;
|
|
649
|
-
}
|
|
650
|
-
transformSchema(originalWrappingSchema, _subschemaConfig, _transformedSchema) {
|
|
651
|
-
this.transformedSchema = mapSchema(originalWrappingSchema, {
|
|
652
|
-
[MapperKind.INPUT_OBJECT_FIELD]: (inputFieldConfig, fieldName, typeName) => {
|
|
653
|
-
const transformedInputField = this.inputFieldTransformer(typeName, fieldName, inputFieldConfig);
|
|
654
|
-
if (Array.isArray(transformedInputField)) {
|
|
655
|
-
const newFieldName = transformedInputField[0];
|
|
656
|
-
if (newFieldName !== fieldName) {
|
|
657
|
-
if (!(typeName in this.mapping)) {
|
|
658
|
-
this.mapping[typeName] = {};
|
|
659
|
-
}
|
|
660
|
-
this.mapping[typeName][newFieldName] = fieldName;
|
|
661
|
-
}
|
|
662
|
-
}
|
|
663
|
-
return transformedInputField;
|
|
664
|
-
},
|
|
665
|
-
});
|
|
666
|
-
return this.transformedSchema;
|
|
667
|
-
}
|
|
668
|
-
transformRequest(originalRequest, delegationContext, _transformationContext) {
|
|
669
|
-
var _a;
|
|
670
|
-
const variableValues = (_a = originalRequest.variables) !== null && _a !== void 0 ? _a : {};
|
|
671
|
-
const fragments = Object.create(null);
|
|
672
|
-
const operations = [];
|
|
673
|
-
for (const def of originalRequest.document.definitions) {
|
|
674
|
-
if (def.kind === Kind.OPERATION_DEFINITION) {
|
|
675
|
-
operations.push(def);
|
|
676
|
-
}
|
|
677
|
-
else if (def.kind === Kind.FRAGMENT_DEFINITION) {
|
|
678
|
-
fragments[def.name.value] = def;
|
|
679
|
-
}
|
|
680
|
-
}
|
|
681
|
-
for (const def of operations) {
|
|
682
|
-
const variableDefs = def.variableDefinitions;
|
|
683
|
-
if (variableDefs != null) {
|
|
684
|
-
for (const variableDef of variableDefs) {
|
|
685
|
-
const varName = variableDef.variable.name.value;
|
|
686
|
-
// Cast to NamedTypeNode required until upcomming graphql releases will have TypeNode paramter
|
|
687
|
-
const varType = typeFromAST(delegationContext.transformedSchema, variableDef.type);
|
|
688
|
-
if (!isInputType(varType)) {
|
|
689
|
-
continue;
|
|
690
|
-
}
|
|
691
|
-
variableValues[varName] = transformInputValue(varType, variableValues[varName], undefined, (type, originalValue) => {
|
|
692
|
-
var _a;
|
|
693
|
-
const newValue = Object.create(null);
|
|
694
|
-
const fields = type.getFields();
|
|
695
|
-
for (const key in originalValue) {
|
|
696
|
-
const field = fields[key];
|
|
697
|
-
if (field != null) {
|
|
698
|
-
const newFieldName = (_a = this.mapping[type.name]) === null || _a === void 0 ? void 0 : _a[field.name];
|
|
699
|
-
if (newFieldName != null) {
|
|
700
|
-
newValue[newFieldName] = originalValue[field.name];
|
|
701
|
-
}
|
|
702
|
-
else {
|
|
703
|
-
newValue[field.name] = originalValue[field.name];
|
|
704
|
-
}
|
|
705
|
-
}
|
|
706
|
-
}
|
|
707
|
-
return newValue;
|
|
708
|
-
});
|
|
709
|
-
}
|
|
710
|
-
}
|
|
711
|
-
}
|
|
712
|
-
for (const def of originalRequest.document.definitions.filter(def => def.kind === Kind.FRAGMENT_DEFINITION)) {
|
|
713
|
-
fragments[def.name.value] = def;
|
|
714
|
-
}
|
|
715
|
-
const document = this.transformDocument(originalRequest.document, this.mapping, this.inputFieldNodeTransformer, this.inputObjectNodeTransformer, originalRequest, delegationContext);
|
|
716
|
-
return {
|
|
717
|
-
...originalRequest,
|
|
718
|
-
document,
|
|
719
|
-
variables: variableValues,
|
|
720
|
-
};
|
|
721
|
-
}
|
|
722
|
-
transformDocument(document, mapping, inputFieldNodeTransformer, inputObjectNodeTransformer, request, delegationContext) {
|
|
723
|
-
const typeInfo = new TypeInfo(this._getTransformedSchema());
|
|
724
|
-
const newDocument = visit(document, visitWithTypeInfo(typeInfo, {
|
|
725
|
-
[Kind.OBJECT]: {
|
|
726
|
-
leave: (node) => {
|
|
727
|
-
// The casting is kind of legit here as we are in a visitor
|
|
728
|
-
const parentType = typeInfo.getInputType();
|
|
729
|
-
if (parentType != null) {
|
|
730
|
-
const parentTypeName = parentType.name;
|
|
731
|
-
const newInputFields = [];
|
|
732
|
-
for (const inputField of node.fields) {
|
|
733
|
-
const newName = inputField.name.value;
|
|
734
|
-
const transformedInputField = inputFieldNodeTransformer != null
|
|
735
|
-
? inputFieldNodeTransformer(parentTypeName, newName, inputField, request, delegationContext)
|
|
736
|
-
: inputField;
|
|
737
|
-
if (Array.isArray(transformedInputField)) {
|
|
738
|
-
for (const individualTransformedInputField of transformedInputField) {
|
|
739
|
-
const typeMapping = mapping[parentTypeName];
|
|
740
|
-
if (typeMapping == null) {
|
|
741
|
-
newInputFields.push(individualTransformedInputField);
|
|
742
|
-
continue;
|
|
743
|
-
}
|
|
744
|
-
const oldName = typeMapping[newName];
|
|
745
|
-
if (oldName == null) {
|
|
746
|
-
newInputFields.push(individualTransformedInputField);
|
|
747
|
-
continue;
|
|
748
|
-
}
|
|
749
|
-
newInputFields.push({
|
|
750
|
-
...individualTransformedInputField,
|
|
751
|
-
name: {
|
|
752
|
-
...individualTransformedInputField.name,
|
|
753
|
-
value: oldName,
|
|
754
|
-
},
|
|
755
|
-
});
|
|
756
|
-
}
|
|
757
|
-
continue;
|
|
758
|
-
}
|
|
759
|
-
const typeMapping = mapping[parentTypeName];
|
|
760
|
-
if (typeMapping == null) {
|
|
761
|
-
newInputFields.push(transformedInputField);
|
|
762
|
-
continue;
|
|
763
|
-
}
|
|
764
|
-
const oldName = typeMapping[newName];
|
|
765
|
-
if (oldName == null) {
|
|
766
|
-
newInputFields.push(transformedInputField);
|
|
767
|
-
continue;
|
|
768
|
-
}
|
|
769
|
-
newInputFields.push({
|
|
770
|
-
...transformedInputField,
|
|
771
|
-
name: {
|
|
772
|
-
...transformedInputField.name,
|
|
773
|
-
value: oldName,
|
|
774
|
-
},
|
|
775
|
-
});
|
|
776
|
-
}
|
|
777
|
-
const newNode = {
|
|
778
|
-
...node,
|
|
779
|
-
fields: newInputFields,
|
|
780
|
-
};
|
|
781
|
-
return inputObjectNodeTransformer != null
|
|
782
|
-
? inputObjectNodeTransformer(parentTypeName, newNode, request, delegationContext)
|
|
783
|
-
: newNode;
|
|
784
|
-
}
|
|
785
|
-
},
|
|
786
|
-
},
|
|
787
|
-
}));
|
|
788
|
-
return newDocument;
|
|
789
|
-
}
|
|
790
|
-
}
|
|
791
|
-
|
|
792
|
-
class RenameInputObjectFields {
|
|
793
|
-
constructor(renamer) {
|
|
794
|
-
this.renamer = renamer;
|
|
795
|
-
this.transformer = new TransformInputObjectFields((typeName, inputFieldName, inputFieldConfig) => {
|
|
796
|
-
const newName = renamer(typeName, inputFieldName, inputFieldConfig);
|
|
797
|
-
if (newName !== undefined && newName !== inputFieldName) {
|
|
798
|
-
const value = renamer(typeName, inputFieldName, inputFieldConfig);
|
|
799
|
-
if (value != null) {
|
|
800
|
-
return [value, inputFieldConfig];
|
|
801
|
-
}
|
|
802
|
-
}
|
|
803
|
-
}, (typeName, inputFieldName, inputFieldNode) => {
|
|
804
|
-
if (!(typeName in this.reverseMap)) {
|
|
805
|
-
return inputFieldNode;
|
|
806
|
-
}
|
|
807
|
-
const inputFieldNameMap = this.reverseMap[typeName];
|
|
808
|
-
if (!(inputFieldName in inputFieldNameMap)) {
|
|
809
|
-
return inputFieldNode;
|
|
810
|
-
}
|
|
811
|
-
return {
|
|
812
|
-
...inputFieldNode,
|
|
813
|
-
name: {
|
|
814
|
-
...inputFieldNode.name,
|
|
815
|
-
value: inputFieldNameMap[inputFieldName],
|
|
816
|
-
},
|
|
817
|
-
};
|
|
818
|
-
});
|
|
819
|
-
this.reverseMap = Object.create(null);
|
|
820
|
-
}
|
|
821
|
-
transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
|
822
|
-
mapSchema(originalWrappingSchema, {
|
|
823
|
-
[MapperKind.INPUT_OBJECT_FIELD]: (inputFieldConfig, fieldName, typeName) => {
|
|
824
|
-
const newName = this.renamer(typeName, fieldName, inputFieldConfig);
|
|
825
|
-
if (newName !== undefined && newName !== fieldName) {
|
|
826
|
-
if (this.reverseMap[typeName] == null) {
|
|
827
|
-
this.reverseMap[typeName] = Object.create(null);
|
|
828
|
-
}
|
|
829
|
-
this.reverseMap[typeName][newName] = fieldName;
|
|
830
|
-
}
|
|
831
|
-
return undefined;
|
|
832
|
-
},
|
|
833
|
-
[MapperKind.ROOT_OBJECT]() {
|
|
834
|
-
return undefined;
|
|
835
|
-
},
|
|
836
|
-
});
|
|
837
|
-
return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
|
838
|
-
}
|
|
839
|
-
transformRequest(originalRequest, delegationContext, transformationContext) {
|
|
840
|
-
return this.transformer.transformRequest(originalRequest, delegationContext, transformationContext);
|
|
841
|
-
}
|
|
842
|
-
}
|
|
843
|
-
|
|
844
|
-
class FilterInputObjectFields {
|
|
845
|
-
constructor(filter, inputObjectNodeTransformer) {
|
|
846
|
-
this.transformer = new TransformInputObjectFields((typeName, fieldName, inputFieldConfig) => filter(typeName, fieldName, inputFieldConfig) ? undefined : null, undefined, inputObjectNodeTransformer);
|
|
847
|
-
}
|
|
848
|
-
transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
|
849
|
-
return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
|
850
|
-
}
|
|
851
|
-
transformRequest(originalRequest, delegationContext, transformationContext) {
|
|
852
|
-
return this.transformer.transformRequest(originalRequest, delegationContext, transformationContext);
|
|
853
|
-
}
|
|
854
|
-
}
|
|
855
|
-
|
|
856
|
-
class MapLeafValues {
|
|
857
|
-
constructor(inputValueTransformer, outputValueTransformer) {
|
|
858
|
-
this.inputValueTransformer = inputValueTransformer;
|
|
859
|
-
this.outputValueTransformer = outputValueTransformer;
|
|
860
|
-
this.resultVisitorMap = Object.create(null);
|
|
861
|
-
}
|
|
862
|
-
_getTypeInfo() {
|
|
863
|
-
const typeInfo = this.typeInfo;
|
|
864
|
-
if (typeInfo === undefined) {
|
|
865
|
-
throw new Error(`The MapLeafValues transform's "transformRequest" and "transformResult" methods cannot be used without first calling "transformSchema".`);
|
|
866
|
-
}
|
|
867
|
-
return typeInfo;
|
|
868
|
-
}
|
|
869
|
-
_getOriginalWrappingSchema() {
|
|
870
|
-
const originalWrappingSchema = this.originalWrappingSchema;
|
|
871
|
-
if (originalWrappingSchema === undefined) {
|
|
872
|
-
throw new Error(`The MapLeafValues transform's "transformRequest" and "transformResult" methods cannot be used without first calling "transformSchema".`);
|
|
873
|
-
}
|
|
874
|
-
return originalWrappingSchema;
|
|
875
|
-
}
|
|
876
|
-
transformSchema(originalWrappingSchema, _subschemaConfig, _transformedSchema) {
|
|
877
|
-
this.originalWrappingSchema = originalWrappingSchema;
|
|
878
|
-
const typeMap = originalWrappingSchema.getTypeMap();
|
|
879
|
-
for (const typeName in typeMap) {
|
|
880
|
-
const type = typeMap[typeName];
|
|
881
|
-
if (!typeName.startsWith('__')) {
|
|
882
|
-
if (isLeafType(type)) {
|
|
883
|
-
this.resultVisitorMap[typeName] = (value) => this.outputValueTransformer(typeName, value);
|
|
884
|
-
}
|
|
885
|
-
}
|
|
886
|
-
}
|
|
887
|
-
this.typeInfo = new TypeInfo(originalWrappingSchema);
|
|
888
|
-
return originalWrappingSchema;
|
|
889
|
-
}
|
|
890
|
-
transformRequest(originalRequest, _delegationContext, transformationContext) {
|
|
891
|
-
var _a;
|
|
892
|
-
const document = originalRequest.document;
|
|
893
|
-
const variableValues = (_a = originalRequest.variables) !== null && _a !== void 0 ? _a : {};
|
|
894
|
-
const operations = document.definitions.filter(def => def.kind === Kind.OPERATION_DEFINITION);
|
|
895
|
-
const fragments = document.definitions.filter(def => def.kind === Kind.FRAGMENT_DEFINITION);
|
|
896
|
-
const newOperations = this.transformOperations(operations, variableValues);
|
|
897
|
-
const transformedRequest = {
|
|
898
|
-
...originalRequest,
|
|
899
|
-
document: {
|
|
900
|
-
...document,
|
|
901
|
-
definitions: [...newOperations, ...fragments],
|
|
902
|
-
},
|
|
903
|
-
variables: variableValues,
|
|
904
|
-
};
|
|
905
|
-
transformationContext.transformedRequest = transformedRequest;
|
|
906
|
-
return transformedRequest;
|
|
907
|
-
}
|
|
908
|
-
transformResult(originalResult, _delegationContext, transformationContext) {
|
|
909
|
-
return visitResult(originalResult, transformationContext.transformedRequest, this._getOriginalWrappingSchema(), this.resultVisitorMap);
|
|
910
|
-
}
|
|
911
|
-
transformOperations(operations, variableValues) {
|
|
912
|
-
return operations.map((operation) => {
|
|
913
|
-
var _a;
|
|
914
|
-
const variableDefinitionMap = ((_a = operation.variableDefinitions) !== null && _a !== void 0 ? _a : []).reduce((prev, def) => ({
|
|
915
|
-
...prev,
|
|
916
|
-
[def.variable.name.value]: def,
|
|
917
|
-
}), {});
|
|
918
|
-
const newOperation = visit(operation, visitWithTypeInfo(this._getTypeInfo(), {
|
|
919
|
-
[Kind.FIELD]: node => this.transformFieldNode(node, variableDefinitionMap, variableValues),
|
|
920
|
-
}));
|
|
921
|
-
return {
|
|
922
|
-
...newOperation,
|
|
923
|
-
variableDefinitions: Object.values(variableDefinitionMap),
|
|
924
|
-
};
|
|
925
|
-
});
|
|
926
|
-
}
|
|
927
|
-
transformFieldNode(field, variableDefinitionMap, variableValues) {
|
|
928
|
-
const targetField = this._getTypeInfo().getFieldDef();
|
|
929
|
-
if (!targetField) {
|
|
930
|
-
return;
|
|
931
|
-
}
|
|
932
|
-
const generateVariableName = createVariableNameGenerator(variableDefinitionMap);
|
|
933
|
-
if (!targetField.name.startsWith('__')) {
|
|
934
|
-
const argumentNodes = field.arguments;
|
|
935
|
-
if (argumentNodes != null) {
|
|
936
|
-
const argumentNodeMap = argumentNodes.reduce((prev, argument) => ({
|
|
937
|
-
...prev,
|
|
938
|
-
[argument.name.value]: argument,
|
|
939
|
-
}), Object.create(null));
|
|
940
|
-
for (const argument of targetField.args) {
|
|
941
|
-
const argName = argument.name;
|
|
942
|
-
const argType = argument.type;
|
|
943
|
-
const argumentNode = argumentNodeMap[argName];
|
|
944
|
-
const argValue = argumentNode === null || argumentNode === void 0 ? void 0 : argumentNode.value;
|
|
945
|
-
let value;
|
|
946
|
-
if (argValue != null) {
|
|
947
|
-
value = valueFromAST(argValue, argType, variableValues);
|
|
948
|
-
}
|
|
949
|
-
updateArgument(argumentNodeMap, variableDefinitionMap, variableValues, argName, generateVariableName(argName), argType, transformInputValue(argType, value, (t, v) => {
|
|
950
|
-
const newValue = this.inputValueTransformer(t.name, v);
|
|
951
|
-
return newValue === undefined ? v : newValue;
|
|
952
|
-
}));
|
|
953
|
-
}
|
|
954
|
-
return {
|
|
955
|
-
...field,
|
|
956
|
-
arguments: Object.values(argumentNodeMap),
|
|
957
|
-
};
|
|
958
|
-
}
|
|
959
|
-
}
|
|
960
|
-
}
|
|
961
|
-
}
|
|
962
|
-
|
|
963
|
-
class TransformEnumValues {
|
|
964
|
-
constructor(enumValueTransformer, inputValueTransformer, outputValueTransformer) {
|
|
965
|
-
this.enumValueTransformer = enumValueTransformer;
|
|
966
|
-
this.mapping = Object.create(null);
|
|
967
|
-
this.reverseMapping = Object.create(null);
|
|
968
|
-
this.transformer = new MapLeafValues(generateValueTransformer(inputValueTransformer, this.reverseMapping), generateValueTransformer(outputValueTransformer, this.mapping));
|
|
969
|
-
}
|
|
970
|
-
transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
|
971
|
-
const mappingSchema = this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
|
972
|
-
this.transformedSchema = mapSchema(mappingSchema, {
|
|
973
|
-
[MapperKind.ENUM_VALUE]: (valueConfig, typeName, _schema, externalValue) => this.transformEnumValue(typeName, externalValue, valueConfig),
|
|
974
|
-
});
|
|
975
|
-
return this.transformedSchema;
|
|
976
|
-
}
|
|
977
|
-
transformRequest(originalRequest, delegationContext, transformationContext) {
|
|
978
|
-
return this.transformer.transformRequest(originalRequest, delegationContext, transformationContext);
|
|
979
|
-
}
|
|
980
|
-
transformResult(originalResult, delegationContext, transformationContext) {
|
|
981
|
-
return this.transformer.transformResult(originalResult, delegationContext, transformationContext);
|
|
982
|
-
}
|
|
983
|
-
transformEnumValue(typeName, externalValue, enumValueConfig) {
|
|
984
|
-
const transformedEnumValue = this.enumValueTransformer(typeName, externalValue, enumValueConfig);
|
|
985
|
-
if (Array.isArray(transformedEnumValue)) {
|
|
986
|
-
const newExternalValue = transformedEnumValue[0];
|
|
987
|
-
if (newExternalValue !== externalValue) {
|
|
988
|
-
if (!(typeName in this.mapping)) {
|
|
989
|
-
this.mapping[typeName] = Object.create(null);
|
|
990
|
-
this.reverseMapping[typeName] = Object.create(null);
|
|
991
|
-
}
|
|
992
|
-
this.mapping[typeName][externalValue] = newExternalValue;
|
|
993
|
-
this.reverseMapping[typeName][newExternalValue] = externalValue;
|
|
994
|
-
}
|
|
995
|
-
}
|
|
996
|
-
return transformedEnumValue;
|
|
997
|
-
}
|
|
998
|
-
}
|
|
999
|
-
function mapEnumValues(typeName, value, mapping) {
|
|
1000
|
-
var _a;
|
|
1001
|
-
const newExternalValue = (_a = mapping[typeName]) === null || _a === void 0 ? void 0 : _a[value];
|
|
1002
|
-
return newExternalValue != null ? newExternalValue : value;
|
|
1003
|
-
}
|
|
1004
|
-
function generateValueTransformer(valueTransformer, mapping) {
|
|
1005
|
-
if (valueTransformer == null) {
|
|
1006
|
-
return (typeName, value) => mapEnumValues(typeName, value, mapping);
|
|
1007
|
-
}
|
|
1008
|
-
else {
|
|
1009
|
-
return (typeName, value) => mapEnumValues(typeName, valueTransformer(typeName, value), mapping);
|
|
1010
|
-
}
|
|
1011
|
-
}
|
|
1012
|
-
|
|
1013
|
-
class TransformQuery {
|
|
1014
|
-
constructor({ path, queryTransformer, resultTransformer = result => result, errorPathTransformer = errorPath => [...errorPath], fragments = {}, }) {
|
|
1015
|
-
this.path = path;
|
|
1016
|
-
this.queryTransformer = queryTransformer;
|
|
1017
|
-
this.resultTransformer = resultTransformer;
|
|
1018
|
-
this.errorPathTransformer = errorPathTransformer;
|
|
1019
|
-
this.fragments = fragments;
|
|
1020
|
-
}
|
|
1021
|
-
transformRequest(originalRequest, delegationContext, transformationContext) {
|
|
1022
|
-
const pathLength = this.path.length;
|
|
1023
|
-
let index = 0;
|
|
1024
|
-
const document = visit(originalRequest.document, {
|
|
1025
|
-
[Kind.FIELD]: {
|
|
1026
|
-
enter: node => {
|
|
1027
|
-
if (index === pathLength || node.name.value !== this.path[index]) {
|
|
1028
|
-
return false;
|
|
1029
|
-
}
|
|
1030
|
-
index++;
|
|
1031
|
-
if (index === pathLength) {
|
|
1032
|
-
const selectionSet = this.queryTransformer(node.selectionSet, this.fragments, delegationContext, transformationContext);
|
|
1033
|
-
return {
|
|
1034
|
-
...node,
|
|
1035
|
-
selectionSet,
|
|
1036
|
-
};
|
|
1037
|
-
}
|
|
1038
|
-
},
|
|
1039
|
-
leave: () => {
|
|
1040
|
-
index--;
|
|
1041
|
-
},
|
|
1042
|
-
},
|
|
1043
|
-
});
|
|
1044
|
-
return {
|
|
1045
|
-
...originalRequest,
|
|
1046
|
-
document,
|
|
1047
|
-
};
|
|
1048
|
-
}
|
|
1049
|
-
transformResult(originalResult, delegationContext, transformationContext) {
|
|
1050
|
-
const data = this.transformData(originalResult.data, delegationContext, transformationContext);
|
|
1051
|
-
const errors = originalResult.errors;
|
|
1052
|
-
return {
|
|
1053
|
-
data,
|
|
1054
|
-
errors: errors != null ? this.transformErrors(errors) : undefined,
|
|
1055
|
-
};
|
|
1056
|
-
}
|
|
1057
|
-
transformData(data, delegationContext, transformationContext) {
|
|
1058
|
-
const leafIndex = this.path.length - 1;
|
|
1059
|
-
let index = 0;
|
|
1060
|
-
let newData = data;
|
|
1061
|
-
if (newData) {
|
|
1062
|
-
let next = this.path[index];
|
|
1063
|
-
while (index < leafIndex) {
|
|
1064
|
-
if (data[next]) {
|
|
1065
|
-
newData = newData[next];
|
|
1066
|
-
}
|
|
1067
|
-
else {
|
|
1068
|
-
break;
|
|
1069
|
-
}
|
|
1070
|
-
index++;
|
|
1071
|
-
next = this.path[index];
|
|
1072
|
-
}
|
|
1073
|
-
newData[next] = this.resultTransformer(newData[next], delegationContext, transformationContext);
|
|
1074
|
-
}
|
|
1075
|
-
return data;
|
|
1076
|
-
}
|
|
1077
|
-
transformErrors(errors) {
|
|
1078
|
-
return errors.map(error => {
|
|
1079
|
-
const path = error.path;
|
|
1080
|
-
if (path == null) {
|
|
1081
|
-
return error;
|
|
1082
|
-
}
|
|
1083
|
-
let match = true;
|
|
1084
|
-
let index = 0;
|
|
1085
|
-
while (index < this.path.length) {
|
|
1086
|
-
if (path[index] !== this.path[index]) {
|
|
1087
|
-
match = false;
|
|
1088
|
-
break;
|
|
1089
|
-
}
|
|
1090
|
-
index++;
|
|
1091
|
-
}
|
|
1092
|
-
const newPath = match ? path.slice(0, index).concat(this.errorPathTransformer(path.slice(index))) : path;
|
|
1093
|
-
return relocatedError(error, newPath);
|
|
1094
|
-
});
|
|
1095
|
-
}
|
|
1096
|
-
}
|
|
1097
|
-
|
|
1098
|
-
class FilterObjectFieldDirectives {
|
|
1099
|
-
constructor(filter) {
|
|
1100
|
-
this.filter = filter;
|
|
1101
|
-
}
|
|
1102
|
-
transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
|
1103
|
-
const transformer = new TransformObjectFields((_typeName, _fieldName, fieldConfig) => {
|
|
1104
|
-
var _a, _b, _c, _d;
|
|
1105
|
-
const keepDirectives = (_c = (_b = (_a = fieldConfig.astNode) === null || _a === void 0 ? void 0 : _a.directives) === null || _b === void 0 ? void 0 : _b.filter(dir => {
|
|
1106
|
-
const directiveDef = originalWrappingSchema.getDirective(dir.name.value);
|
|
1107
|
-
const directiveValue = directiveDef ? getArgumentValues(directiveDef, dir) : undefined;
|
|
1108
|
-
return this.filter(dir.name.value, directiveValue);
|
|
1109
|
-
})) !== null && _c !== void 0 ? _c : [];
|
|
1110
|
-
if (((_d = fieldConfig.astNode) === null || _d === void 0 ? void 0 : _d.directives) != null &&
|
|
1111
|
-
keepDirectives.length !== fieldConfig.astNode.directives.length) {
|
|
1112
|
-
fieldConfig = {
|
|
1113
|
-
...fieldConfig,
|
|
1114
|
-
astNode: {
|
|
1115
|
-
...fieldConfig.astNode,
|
|
1116
|
-
directives: keepDirectives,
|
|
1117
|
-
},
|
|
1118
|
-
};
|
|
1119
|
-
return fieldConfig;
|
|
1120
|
-
}
|
|
1121
|
-
});
|
|
1122
|
-
return transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
|
1123
|
-
}
|
|
1124
|
-
}
|
|
1125
|
-
|
|
1126
|
-
class RemoveObjectFieldDirectives {
|
|
1127
|
-
constructor(directiveName, args = {}) {
|
|
1128
|
-
this.transformer = new FilterObjectFieldDirectives((dirName, dirValue) => {
|
|
1129
|
-
return !(valueMatchesCriteria(dirName, directiveName) && valueMatchesCriteria(dirValue, args));
|
|
1130
|
-
});
|
|
1131
|
-
}
|
|
1132
|
-
transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
|
1133
|
-
return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
|
1134
|
-
}
|
|
1135
|
-
}
|
|
1136
|
-
|
|
1137
|
-
class RemoveObjectFieldsWithDirective {
|
|
1138
|
-
constructor(directiveName, args = {}) {
|
|
1139
|
-
this.directiveName = directiveName;
|
|
1140
|
-
this.args = args;
|
|
1141
|
-
}
|
|
1142
|
-
transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
|
1143
|
-
const transformer = new FilterObjectFields((_typeName, _fieldName, fieldConfig) => {
|
|
1144
|
-
const directives = getDirectives(originalWrappingSchema, fieldConfig);
|
|
1145
|
-
return !directives.some(directive => valueMatchesCriteria(directive.name, this.directiveName) && valueMatchesCriteria(directive.args, this.args));
|
|
1146
|
-
});
|
|
1147
|
-
return transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
|
1148
|
-
}
|
|
1149
|
-
}
|
|
1150
|
-
|
|
1151
|
-
class RemoveObjectFieldDeprecations {
|
|
1152
|
-
constructor(reason) {
|
|
1153
|
-
const args = { reason };
|
|
1154
|
-
this.removeDirectives = new FilterObjectFieldDirectives((dirName, dirValue) => {
|
|
1155
|
-
return !(dirName === 'deprecated' && valueMatchesCriteria(dirValue, args));
|
|
1156
|
-
});
|
|
1157
|
-
this.removeDeprecations = new TransformObjectFields((_typeName, _fieldName, fieldConfig) => {
|
|
1158
|
-
if (fieldConfig.deprecationReason && valueMatchesCriteria(fieldConfig.deprecationReason, reason)) {
|
|
1159
|
-
fieldConfig = { ...fieldConfig };
|
|
1160
|
-
delete fieldConfig.deprecationReason;
|
|
1161
|
-
}
|
|
1162
|
-
return fieldConfig;
|
|
1163
|
-
});
|
|
1164
|
-
}
|
|
1165
|
-
transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
|
1166
|
-
return this.removeDeprecations.transformSchema(this.removeDirectives.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema), subschemaConfig, transformedSchema);
|
|
1167
|
-
}
|
|
1168
|
-
}
|
|
1169
|
-
|
|
1170
|
-
class RemoveObjectFieldsWithDeprecation {
|
|
1171
|
-
constructor(reason) {
|
|
1172
|
-
this.transformer = new FilterObjectFields((_typeName, _fieldName, fieldConfig) => {
|
|
1173
|
-
if (fieldConfig.deprecationReason) {
|
|
1174
|
-
return !valueMatchesCriteria(fieldConfig.deprecationReason, reason);
|
|
1175
|
-
}
|
|
1176
|
-
return true;
|
|
1177
|
-
});
|
|
1178
|
-
}
|
|
1179
|
-
transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
|
1180
|
-
return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
|
1181
|
-
}
|
|
1182
|
-
}
|
|
1183
|
-
|
|
1184
|
-
class PruneTypes {
|
|
1185
|
-
constructor(options = {}) {
|
|
1186
|
-
this.options = options;
|
|
1187
|
-
}
|
|
1188
|
-
transformSchema(originalWrappingSchema, _subschemaConfig, _transformedSchema) {
|
|
1189
|
-
return pruneSchema(originalWrappingSchema, this.options);
|
|
1190
|
-
}
|
|
1191
|
-
}
|
|
1192
|
-
|
|
1193
|
-
class MapFields {
|
|
1194
|
-
constructor(fieldNodeTransformerMap, objectValueTransformerMap, errorsTransformer) {
|
|
1195
|
-
this.fieldNodeTransformerMap = fieldNodeTransformerMap;
|
|
1196
|
-
this.objectValueTransformerMap = objectValueTransformerMap;
|
|
1197
|
-
this.errorsTransformer = errorsTransformer;
|
|
1198
|
-
}
|
|
1199
|
-
_getTransformer() {
|
|
1200
|
-
const transformer = this.transformer;
|
|
1201
|
-
if (transformer === undefined) {
|
|
1202
|
-
throw new Error(`The MapFields transform's "transformRequest" and "transformResult" methods cannot be used without first calling "transformSchema".`);
|
|
1203
|
-
}
|
|
1204
|
-
return transformer;
|
|
1205
|
-
}
|
|
1206
|
-
transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
|
1207
|
-
var _a;
|
|
1208
|
-
const subscriptionTypeName = (_a = originalWrappingSchema.getSubscriptionType()) === null || _a === void 0 ? void 0 : _a.name;
|
|
1209
|
-
const objectValueTransformerMap = this.objectValueTransformerMap;
|
|
1210
|
-
this.transformer = new TransformCompositeFields(() => undefined, (typeName, fieldName, fieldNode, fragments, transformationContext) => {
|
|
1211
|
-
const typeTransformers = this.fieldNodeTransformerMap[typeName];
|
|
1212
|
-
if (typeTransformers == null) {
|
|
1213
|
-
return undefined;
|
|
1214
|
-
}
|
|
1215
|
-
const fieldNodeTransformer = typeTransformers[fieldName];
|
|
1216
|
-
if (fieldNodeTransformer == null) {
|
|
1217
|
-
return undefined;
|
|
1218
|
-
}
|
|
1219
|
-
return fieldNodeTransformer(fieldNode, fragments, transformationContext);
|
|
1220
|
-
}, objectValueTransformerMap != null
|
|
1221
|
-
? (data, transformationContext) => {
|
|
1222
|
-
if (data == null) {
|
|
1223
|
-
return data;
|
|
1224
|
-
}
|
|
1225
|
-
let typeName = data.__typename;
|
|
1226
|
-
if (typeName == null) {
|
|
1227
|
-
// see https://github.com/ardatan/graphql-tools/issues/2282
|
|
1228
|
-
typeName = subscriptionTypeName;
|
|
1229
|
-
if (typeName == null) {
|
|
1230
|
-
return data;
|
|
1231
|
-
}
|
|
1232
|
-
}
|
|
1233
|
-
const transformer = objectValueTransformerMap[typeName];
|
|
1234
|
-
if (transformer == null) {
|
|
1235
|
-
return data;
|
|
1236
|
-
}
|
|
1237
|
-
return transformer(data, transformationContext);
|
|
1238
|
-
}
|
|
1239
|
-
: undefined, this.errorsTransformer != null ? this.errorsTransformer : undefined);
|
|
1240
|
-
return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
|
1241
|
-
}
|
|
1242
|
-
transformRequest(originalRequest, delegationContext, transformationContext) {
|
|
1243
|
-
return this._getTransformer().transformRequest(originalRequest, delegationContext, transformationContext);
|
|
1244
|
-
}
|
|
1245
|
-
transformResult(originalResult, delegationContext, transformationContext) {
|
|
1246
|
-
return this._getTransformer().transformResult(originalResult, delegationContext, transformationContext);
|
|
1247
|
-
}
|
|
1248
|
-
}
|
|
1249
|
-
|
|
1250
|
-
class WrapFields {
|
|
1251
|
-
constructor(outerTypeName, wrappingFieldNames, wrappingTypeNames, fieldNames, prefix = 'gqtld') {
|
|
1252
|
-
this.outerTypeName = outerTypeName;
|
|
1253
|
-
this.wrappingFieldNames = wrappingFieldNames;
|
|
1254
|
-
this.wrappingTypeNames = wrappingTypeNames;
|
|
1255
|
-
this.numWraps = wrappingFieldNames.length;
|
|
1256
|
-
this.fieldNames = fieldNames;
|
|
1257
|
-
const remainingWrappingFieldNames = this.wrappingFieldNames.slice();
|
|
1258
|
-
const outerMostWrappingFieldName = remainingWrappingFieldNames.shift();
|
|
1259
|
-
if (outerMostWrappingFieldName == null) {
|
|
1260
|
-
throw new Error(`Cannot wrap fields, no wrapping field name provided.`);
|
|
1261
|
-
}
|
|
1262
|
-
this.transformer = new MapFields({
|
|
1263
|
-
[outerTypeName]: {
|
|
1264
|
-
[outerMostWrappingFieldName]: (fieldNode, fragments, transformationContext) => hoistFieldNodes({
|
|
1265
|
-
fieldNode,
|
|
1266
|
-
path: remainingWrappingFieldNames,
|
|
1267
|
-
fieldNames,
|
|
1268
|
-
fragments,
|
|
1269
|
-
transformationContext: transformationContext,
|
|
1270
|
-
prefix,
|
|
1271
|
-
}),
|
|
1272
|
-
},
|
|
1273
|
-
}, {
|
|
1274
|
-
[outerTypeName]: (value, context) => dehoistValue(value, context),
|
|
1275
|
-
}, (errors, context) => dehoistErrors(errors, context));
|
|
1276
|
-
}
|
|
1277
|
-
transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
|
1278
|
-
var _a, _b, _c, _d;
|
|
1279
|
-
const fieldNames = this.fieldNames;
|
|
1280
|
-
const targetFieldConfigMap = selectObjectFields(originalWrappingSchema, this.outerTypeName, !fieldNames ? () => true : fieldName => fieldNames.includes(fieldName));
|
|
1281
|
-
const newTargetFieldConfigMap = Object.create(null);
|
|
1282
|
-
for (const fieldName in targetFieldConfigMap) {
|
|
1283
|
-
const field = targetFieldConfigMap[fieldName];
|
|
1284
|
-
const newField = {
|
|
1285
|
-
...field,
|
|
1286
|
-
resolve: defaultMergedResolver,
|
|
1287
|
-
};
|
|
1288
|
-
newTargetFieldConfigMap[fieldName] = newField;
|
|
1289
|
-
}
|
|
1290
|
-
let wrapIndex = this.numWraps - 1;
|
|
1291
|
-
let wrappingTypeName = this.wrappingTypeNames[wrapIndex];
|
|
1292
|
-
let wrappingFieldName = this.wrappingFieldNames[wrapIndex];
|
|
1293
|
-
let newSchema = appendObjectFields(originalWrappingSchema, wrappingTypeName, newTargetFieldConfigMap);
|
|
1294
|
-
for (wrapIndex--; wrapIndex > -1; wrapIndex--) {
|
|
1295
|
-
const nextWrappingTypeName = this.wrappingTypeNames[wrapIndex];
|
|
1296
|
-
newSchema = appendObjectFields(newSchema, nextWrappingTypeName, {
|
|
1297
|
-
[wrappingFieldName]: {
|
|
1298
|
-
type: new GraphQLNonNull(newSchema.getType(wrappingTypeName)),
|
|
1299
|
-
resolve: defaultMergedResolver,
|
|
1300
|
-
},
|
|
1301
|
-
});
|
|
1302
|
-
wrappingTypeName = nextWrappingTypeName;
|
|
1303
|
-
wrappingFieldName = this.wrappingFieldNames[wrapIndex];
|
|
1304
|
-
}
|
|
1305
|
-
const wrappingRootField = this.outerTypeName === ((_a = originalWrappingSchema.getQueryType()) === null || _a === void 0 ? void 0 : _a.name) ||
|
|
1306
|
-
this.outerTypeName === ((_b = originalWrappingSchema.getMutationType()) === null || _b === void 0 ? void 0 : _b.name);
|
|
1307
|
-
let resolve;
|
|
1308
|
-
if (transformedSchema) {
|
|
1309
|
-
if (wrappingRootField) {
|
|
1310
|
-
const targetSchema = subschemaConfig.schema;
|
|
1311
|
-
const operation = this.outerTypeName === ((_c = targetSchema.getQueryType()) === null || _c === void 0 ? void 0 : _c.name) ? 'query' : 'mutation';
|
|
1312
|
-
const createProxyingResolver = (_d = subschemaConfig.createProxyingResolver) !== null && _d !== void 0 ? _d : defaultCreateProxyingResolver;
|
|
1313
|
-
resolve = createProxyingResolver({
|
|
1314
|
-
subschemaConfig,
|
|
1315
|
-
transformedSchema,
|
|
1316
|
-
operation: operation,
|
|
1317
|
-
fieldName: wrappingFieldName,
|
|
1318
|
-
});
|
|
1319
|
-
}
|
|
1320
|
-
else {
|
|
1321
|
-
resolve = defaultMergedResolver;
|
|
1322
|
-
}
|
|
1323
|
-
}
|
|
1324
|
-
[newSchema] = modifyObjectFields(newSchema, this.outerTypeName, fieldName => !!newTargetFieldConfigMap[fieldName], {
|
|
1325
|
-
[wrappingFieldName]: {
|
|
1326
|
-
type: new GraphQLNonNull(newSchema.getType(wrappingTypeName)),
|
|
1327
|
-
resolve,
|
|
1328
|
-
},
|
|
1329
|
-
});
|
|
1330
|
-
return this.transformer.transformSchema(newSchema, subschemaConfig, transformedSchema);
|
|
1331
|
-
}
|
|
1332
|
-
transformRequest(originalRequest, delegationContext, transformationContext) {
|
|
1333
|
-
transformationContext.nextIndex = 0;
|
|
1334
|
-
transformationContext.paths = Object.create(null);
|
|
1335
|
-
return this.transformer.transformRequest(originalRequest, delegationContext, transformationContext);
|
|
1336
|
-
}
|
|
1337
|
-
transformResult(originalResult, delegationContext, transformationContext) {
|
|
1338
|
-
return this.transformer.transformResult(originalResult, delegationContext, transformationContext);
|
|
1339
|
-
}
|
|
1340
|
-
}
|
|
1341
|
-
function collectFields(selectionSet, fragments, fields = [], visitedFragmentNames = {}) {
|
|
1342
|
-
if (selectionSet != null) {
|
|
1343
|
-
for (const selection of selectionSet.selections) {
|
|
1344
|
-
switch (selection.kind) {
|
|
1345
|
-
case Kind.FIELD:
|
|
1346
|
-
fields.push(selection);
|
|
1347
|
-
break;
|
|
1348
|
-
case Kind.INLINE_FRAGMENT:
|
|
1349
|
-
collectFields(selection.selectionSet, fragments, fields, visitedFragmentNames);
|
|
1350
|
-
break;
|
|
1351
|
-
case Kind.FRAGMENT_SPREAD: {
|
|
1352
|
-
const fragmentName = selection.name.value;
|
|
1353
|
-
if (!visitedFragmentNames[fragmentName]) {
|
|
1354
|
-
visitedFragmentNames[fragmentName] = true;
|
|
1355
|
-
collectFields(fragments[fragmentName].selectionSet, fragments, fields, visitedFragmentNames);
|
|
1356
|
-
}
|
|
1357
|
-
break;
|
|
1358
|
-
}
|
|
1359
|
-
}
|
|
1360
|
-
}
|
|
1361
|
-
}
|
|
1362
|
-
return fields;
|
|
1363
|
-
}
|
|
1364
|
-
function aliasFieldNode(fieldNode, str) {
|
|
1365
|
-
return {
|
|
1366
|
-
...fieldNode,
|
|
1367
|
-
alias: {
|
|
1368
|
-
kind: Kind.NAME,
|
|
1369
|
-
value: str,
|
|
1370
|
-
},
|
|
1371
|
-
};
|
|
1372
|
-
}
|
|
1373
|
-
function hoistFieldNodes({ fieldNode, fieldNames, path, fragments, transformationContext, prefix, index = 0, wrappingPath = [], }) {
|
|
1374
|
-
const alias = fieldNode.alias != null ? fieldNode.alias.value : fieldNode.name.value;
|
|
1375
|
-
let newFieldNodes = [];
|
|
1376
|
-
if (index < path.length) {
|
|
1377
|
-
const pathSegment = path[index];
|
|
1378
|
-
for (const possibleFieldNode of collectFields(fieldNode.selectionSet, fragments)) {
|
|
1379
|
-
if (possibleFieldNode.name.value === pathSegment) {
|
|
1380
|
-
const newWrappingPath = wrappingPath.concat([alias]);
|
|
1381
|
-
newFieldNodes = newFieldNodes.concat(hoistFieldNodes({
|
|
1382
|
-
fieldNode: possibleFieldNode,
|
|
1383
|
-
fieldNames,
|
|
1384
|
-
path,
|
|
1385
|
-
fragments,
|
|
1386
|
-
transformationContext,
|
|
1387
|
-
prefix,
|
|
1388
|
-
index: index + 1,
|
|
1389
|
-
wrappingPath: newWrappingPath,
|
|
1390
|
-
}));
|
|
1391
|
-
}
|
|
1392
|
-
}
|
|
1393
|
-
}
|
|
1394
|
-
else {
|
|
1395
|
-
for (const possibleFieldNode of collectFields(fieldNode.selectionSet, fragments)) {
|
|
1396
|
-
if (!fieldNames || fieldNames.includes(possibleFieldNode.name.value)) {
|
|
1397
|
-
const nextIndex = transformationContext.nextIndex;
|
|
1398
|
-
transformationContext.nextIndex++;
|
|
1399
|
-
const indexingAlias = `__${prefix}${nextIndex}__`;
|
|
1400
|
-
transformationContext.paths[indexingAlias] = {
|
|
1401
|
-
pathToField: wrappingPath.concat([alias]),
|
|
1402
|
-
alias: possibleFieldNode.alias != null ? possibleFieldNode.alias.value : possibleFieldNode.name.value,
|
|
1403
|
-
};
|
|
1404
|
-
newFieldNodes.push(aliasFieldNode(possibleFieldNode, indexingAlias));
|
|
1405
|
-
}
|
|
1406
|
-
}
|
|
1407
|
-
}
|
|
1408
|
-
return newFieldNodes;
|
|
1409
|
-
}
|
|
1410
|
-
function dehoistValue(originalValue, context) {
|
|
1411
|
-
if (originalValue == null) {
|
|
1412
|
-
return originalValue;
|
|
1413
|
-
}
|
|
1414
|
-
const newValue = Object.create(null);
|
|
1415
|
-
for (const alias in originalValue) {
|
|
1416
|
-
let obj = newValue;
|
|
1417
|
-
const path = context.paths[alias];
|
|
1418
|
-
if (path == null) {
|
|
1419
|
-
newValue[alias] = originalValue[alias];
|
|
1420
|
-
continue;
|
|
1421
|
-
}
|
|
1422
|
-
const pathToField = path.pathToField;
|
|
1423
|
-
const fieldAlias = path.alias;
|
|
1424
|
-
for (const key of pathToField) {
|
|
1425
|
-
obj = obj[key] = obj[key] || Object.create(null);
|
|
1426
|
-
}
|
|
1427
|
-
obj[fieldAlias] = originalValue[alias];
|
|
1428
|
-
}
|
|
1429
|
-
return newValue;
|
|
1430
|
-
}
|
|
1431
|
-
function dehoistErrors(errors, context) {
|
|
1432
|
-
if (errors === undefined) {
|
|
1433
|
-
return undefined;
|
|
1434
|
-
}
|
|
1435
|
-
return errors.map(error => {
|
|
1436
|
-
const originalPath = error.path;
|
|
1437
|
-
if (originalPath == null) {
|
|
1438
|
-
return error;
|
|
1439
|
-
}
|
|
1440
|
-
let newPath = [];
|
|
1441
|
-
for (const pathSegment of originalPath) {
|
|
1442
|
-
if (typeof pathSegment !== 'string') {
|
|
1443
|
-
newPath.push(pathSegment);
|
|
1444
|
-
continue;
|
|
1445
|
-
}
|
|
1446
|
-
const path = context.paths[pathSegment];
|
|
1447
|
-
if (path == null) {
|
|
1448
|
-
newPath.push(pathSegment);
|
|
1449
|
-
continue;
|
|
1450
|
-
}
|
|
1451
|
-
newPath = newPath.concat(path.pathToField, [path.alias]);
|
|
1452
|
-
}
|
|
1453
|
-
return relocatedError(error, newPath);
|
|
1454
|
-
});
|
|
1455
|
-
}
|
|
1456
|
-
|
|
1457
|
-
class WrapType {
|
|
1458
|
-
constructor(outerTypeName, innerTypeName, fieldName) {
|
|
1459
|
-
this.transformer = new WrapFields(outerTypeName, [fieldName], [innerTypeName]);
|
|
1460
|
-
}
|
|
1461
|
-
transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
|
1462
|
-
return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
|
1463
|
-
}
|
|
1464
|
-
transformRequest(originalRequest, delegationContext, transformationContext) {
|
|
1465
|
-
return this.transformer.transformRequest(originalRequest, delegationContext, transformationContext);
|
|
1466
|
-
}
|
|
1467
|
-
transformResult(originalResult, delegationContext, transformationContext) {
|
|
1468
|
-
return this.transformer.transformResult(originalResult, delegationContext, transformationContext);
|
|
1469
|
-
}
|
|
1470
|
-
}
|
|
1471
|
-
|
|
1472
|
-
class HoistField {
|
|
1473
|
-
constructor(typeName, pathConfig, newFieldName, alias = '__gqtlw__') {
|
|
1474
|
-
this.typeName = typeName;
|
|
1475
|
-
this.newFieldName = newFieldName;
|
|
1476
|
-
const path = pathConfig.map(segment => (typeof segment === 'string' ? segment : segment.fieldName));
|
|
1477
|
-
this.argFilters = pathConfig.map((segment, index) => {
|
|
1478
|
-
if (typeof segment === 'string' || segment.argFilter == null) {
|
|
1479
|
-
return index === pathConfig.length - 1 ? () => true : () => false;
|
|
1480
|
-
}
|
|
1481
|
-
return segment.argFilter;
|
|
1482
|
-
});
|
|
1483
|
-
const pathToField = path.slice();
|
|
1484
|
-
const oldFieldName = pathToField.pop();
|
|
1485
|
-
if (oldFieldName == null) {
|
|
1486
|
-
throw new Error(`Cannot hoist field to ${newFieldName} on type ${typeName}, no path provided.`);
|
|
1487
|
-
}
|
|
1488
|
-
this.oldFieldName = oldFieldName;
|
|
1489
|
-
this.pathToField = pathToField;
|
|
1490
|
-
const argLevels = Object.create(null);
|
|
1491
|
-
this.transformer = new MapFields({
|
|
1492
|
-
[typeName]: {
|
|
1493
|
-
[newFieldName]: fieldNode => wrapFieldNode(renameFieldNode(fieldNode, oldFieldName), pathToField, alias, argLevels),
|
|
1494
|
-
},
|
|
1495
|
-
}, {
|
|
1496
|
-
[typeName]: value => unwrapValue(value, alias),
|
|
1497
|
-
}, errors => (errors != null ? unwrapErrors(errors, alias) : undefined));
|
|
1498
|
-
this.argLevels = argLevels;
|
|
1499
|
-
}
|
|
1500
|
-
transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
|
1501
|
-
var _a, _b, _c, _d;
|
|
1502
|
-
const argsMap = Object.create(null);
|
|
1503
|
-
const innerType = this.pathToField.reduce((acc, pathSegment, index) => {
|
|
1504
|
-
const field = acc.getFields()[pathSegment];
|
|
1505
|
-
for (const arg of field.args) {
|
|
1506
|
-
if (this.argFilters[index](arg)) {
|
|
1507
|
-
argsMap[arg.name] = arg;
|
|
1508
|
-
this.argLevels[arg.name] = index;
|
|
1509
|
-
}
|
|
1510
|
-
}
|
|
1511
|
-
return getNullableType(field.type);
|
|
1512
|
-
}, originalWrappingSchema.getType(this.typeName));
|
|
1513
|
-
let [newSchema, targetFieldConfigMap] = removeObjectFields(originalWrappingSchema, innerType.name, fieldName => fieldName === this.oldFieldName);
|
|
1514
|
-
const targetField = targetFieldConfigMap[this.oldFieldName];
|
|
1515
|
-
let resolve;
|
|
1516
|
-
if (transformedSchema) {
|
|
1517
|
-
const hoistingToRootField = this.typeName === ((_a = originalWrappingSchema.getQueryType()) === null || _a === void 0 ? void 0 : _a.name) ||
|
|
1518
|
-
this.typeName === ((_b = originalWrappingSchema.getMutationType()) === null || _b === void 0 ? void 0 : _b.name);
|
|
1519
|
-
if (hoistingToRootField) {
|
|
1520
|
-
const targetSchema = subschemaConfig.schema;
|
|
1521
|
-
const operation = this.typeName === ((_c = targetSchema.getQueryType()) === null || _c === void 0 ? void 0 : _c.name) ? 'query' : 'mutation';
|
|
1522
|
-
const createProxyingResolver = (_d = subschemaConfig.createProxyingResolver) !== null && _d !== void 0 ? _d : defaultCreateProxyingResolver;
|
|
1523
|
-
resolve = createProxyingResolver({
|
|
1524
|
-
subschemaConfig,
|
|
1525
|
-
transformedSchema,
|
|
1526
|
-
operation: operation,
|
|
1527
|
-
fieldName: this.newFieldName,
|
|
1528
|
-
});
|
|
1529
|
-
}
|
|
1530
|
-
else {
|
|
1531
|
-
resolve = defaultMergedResolver;
|
|
1532
|
-
}
|
|
1533
|
-
}
|
|
1534
|
-
const newTargetField = {
|
|
1535
|
-
...targetField,
|
|
1536
|
-
resolve: resolve,
|
|
1537
|
-
};
|
|
1538
|
-
const level = this.pathToField.length;
|
|
1539
|
-
const args = targetField.args;
|
|
1540
|
-
if (args != null) {
|
|
1541
|
-
for (const argName in args) {
|
|
1542
|
-
const argConfig = args[argName];
|
|
1543
|
-
if (argConfig == null) {
|
|
1544
|
-
continue;
|
|
1545
|
-
}
|
|
1546
|
-
const arg = {
|
|
1547
|
-
...argConfig,
|
|
1548
|
-
name: argName,
|
|
1549
|
-
description: argConfig.description,
|
|
1550
|
-
defaultValue: argConfig.defaultValue,
|
|
1551
|
-
extensions: argConfig.extensions,
|
|
1552
|
-
astNode: argConfig.astNode,
|
|
1553
|
-
};
|
|
1554
|
-
if (this.argFilters[level](arg)) {
|
|
1555
|
-
argsMap[argName] = arg;
|
|
1556
|
-
this.argLevels[arg.name] = level;
|
|
1557
|
-
}
|
|
1558
|
-
}
|
|
1559
|
-
}
|
|
1560
|
-
newTargetField.args = argsMap;
|
|
1561
|
-
newSchema = appendObjectFields(newSchema, this.typeName, {
|
|
1562
|
-
[this.newFieldName]: newTargetField,
|
|
1563
|
-
});
|
|
1564
|
-
return this.transformer.transformSchema(newSchema, subschemaConfig, transformedSchema);
|
|
1565
|
-
}
|
|
1566
|
-
transformRequest(originalRequest, delegationContext, transformationContext) {
|
|
1567
|
-
return this.transformer.transformRequest(originalRequest, delegationContext, transformationContext);
|
|
1568
|
-
}
|
|
1569
|
-
transformResult(originalResult, delegationContext, transformationContext) {
|
|
1570
|
-
return this.transformer.transformResult(originalResult, delegationContext, transformationContext);
|
|
1571
|
-
}
|
|
1572
|
-
}
|
|
1573
|
-
function wrapFieldNode(fieldNode, path, alias, argLevels) {
|
|
1574
|
-
return path.reduceRight((acc, fieldName, index) => ({
|
|
1575
|
-
kind: Kind.FIELD,
|
|
1576
|
-
alias: {
|
|
1577
|
-
kind: Kind.NAME,
|
|
1578
|
-
value: alias,
|
|
1579
|
-
},
|
|
1580
|
-
name: {
|
|
1581
|
-
kind: Kind.NAME,
|
|
1582
|
-
value: fieldName,
|
|
1583
|
-
},
|
|
1584
|
-
selectionSet: {
|
|
1585
|
-
kind: Kind.SELECTION_SET,
|
|
1586
|
-
selections: [acc],
|
|
1587
|
-
},
|
|
1588
|
-
arguments: fieldNode.arguments != null
|
|
1589
|
-
? fieldNode.arguments.filter(arg => argLevels[arg.name.value] === index)
|
|
1590
|
-
: undefined,
|
|
1591
|
-
}), {
|
|
1592
|
-
...fieldNode,
|
|
1593
|
-
arguments: fieldNode.arguments != null
|
|
1594
|
-
? fieldNode.arguments.filter(arg => argLevels[arg.name.value] === path.length)
|
|
1595
|
-
: undefined,
|
|
1596
|
-
});
|
|
1597
|
-
}
|
|
1598
|
-
function renameFieldNode(fieldNode, name) {
|
|
1599
|
-
return {
|
|
1600
|
-
...fieldNode,
|
|
1601
|
-
alias: {
|
|
1602
|
-
kind: Kind.NAME,
|
|
1603
|
-
value: fieldNode.alias != null ? fieldNode.alias.value : fieldNode.name.value,
|
|
1604
|
-
},
|
|
1605
|
-
name: {
|
|
1606
|
-
kind: Kind.NAME,
|
|
1607
|
-
value: name,
|
|
1608
|
-
},
|
|
1609
|
-
};
|
|
1610
|
-
}
|
|
1611
|
-
function unwrapValue(originalValue, alias) {
|
|
1612
|
-
let newValue = originalValue;
|
|
1613
|
-
let object = newValue[alias];
|
|
1614
|
-
while (object != null) {
|
|
1615
|
-
newValue = object;
|
|
1616
|
-
object = newValue[alias];
|
|
1617
|
-
}
|
|
1618
|
-
delete originalValue[alias];
|
|
1619
|
-
Object.assign(originalValue, newValue);
|
|
1620
|
-
return originalValue;
|
|
1621
|
-
}
|
|
1622
|
-
function unwrapErrors(errors, alias) {
|
|
1623
|
-
if (errors === undefined) {
|
|
1624
|
-
return undefined;
|
|
1625
|
-
}
|
|
1626
|
-
return errors.map(error => {
|
|
1627
|
-
const originalPath = error.path;
|
|
1628
|
-
if (originalPath == null) {
|
|
1629
|
-
return error;
|
|
1630
|
-
}
|
|
1631
|
-
const newPath = originalPath.filter(pathSegment => pathSegment !== alias);
|
|
1632
|
-
return relocatedError(error, newPath);
|
|
1633
|
-
});
|
|
1634
|
-
}
|
|
1635
|
-
|
|
1636
|
-
class WrapQuery {
|
|
1637
|
-
constructor(path, wrapper, extractor) {
|
|
1638
|
-
this.path = path;
|
|
1639
|
-
this.wrapper = wrapper;
|
|
1640
|
-
this.extractor = extractor;
|
|
1641
|
-
}
|
|
1642
|
-
transformRequest(originalRequest, _delegationContext, _transformationContext) {
|
|
1643
|
-
const fieldPath = [];
|
|
1644
|
-
const ourPath = JSON.stringify(this.path);
|
|
1645
|
-
const document = visit(originalRequest.document, {
|
|
1646
|
-
[Kind.FIELD]: {
|
|
1647
|
-
enter: (node) => {
|
|
1648
|
-
fieldPath.push(node.name.value);
|
|
1649
|
-
if (node.selectionSet != null && ourPath === JSON.stringify(fieldPath)) {
|
|
1650
|
-
const wrapResult = this.wrapper(node.selectionSet);
|
|
1651
|
-
// Selection can be either a single selection or a selection set. If it's just one selection,
|
|
1652
|
-
// let's wrap it in a selection set. Otherwise, keep it as is.
|
|
1653
|
-
const selectionSet = wrapResult != null && wrapResult.kind === Kind.SELECTION_SET
|
|
1654
|
-
? wrapResult
|
|
1655
|
-
: {
|
|
1656
|
-
kind: Kind.SELECTION_SET,
|
|
1657
|
-
selections: [wrapResult],
|
|
1658
|
-
};
|
|
1659
|
-
return {
|
|
1660
|
-
...node,
|
|
1661
|
-
selectionSet,
|
|
1662
|
-
};
|
|
1663
|
-
}
|
|
1664
|
-
},
|
|
1665
|
-
leave: () => {
|
|
1666
|
-
fieldPath.pop();
|
|
1667
|
-
},
|
|
1668
|
-
},
|
|
1669
|
-
});
|
|
1670
|
-
return {
|
|
1671
|
-
...originalRequest,
|
|
1672
|
-
document,
|
|
1673
|
-
};
|
|
1674
|
-
}
|
|
1675
|
-
transformResult(originalResult, _delegationContext, _transformationContext) {
|
|
1676
|
-
const rootData = originalResult.data;
|
|
1677
|
-
if (rootData != null) {
|
|
1678
|
-
let data = rootData;
|
|
1679
|
-
const path = [...this.path];
|
|
1680
|
-
while (path.length > 1) {
|
|
1681
|
-
const next = path.shift();
|
|
1682
|
-
if (data[next]) {
|
|
1683
|
-
data = data[next];
|
|
1684
|
-
}
|
|
1685
|
-
}
|
|
1686
|
-
data[path[0]] = this.extractor(data[path[0]]);
|
|
1687
|
-
}
|
|
1688
|
-
return {
|
|
1689
|
-
data: rootData,
|
|
1690
|
-
errors: originalResult.errors,
|
|
1691
|
-
};
|
|
1692
|
-
}
|
|
1693
|
-
}
|
|
1694
|
-
|
|
1695
|
-
class ExtractField {
|
|
1696
|
-
constructor({ from, to }) {
|
|
1697
|
-
this.from = from;
|
|
1698
|
-
this.to = to;
|
|
1699
|
-
}
|
|
1700
|
-
transformRequest(originalRequest, _delegationContext, _transformationContext) {
|
|
1701
|
-
let fromSelection;
|
|
1702
|
-
const ourPathFrom = JSON.stringify(this.from);
|
|
1703
|
-
const ourPathTo = JSON.stringify(this.to);
|
|
1704
|
-
let fieldPath = [];
|
|
1705
|
-
visit(originalRequest.document, {
|
|
1706
|
-
[Kind.FIELD]: {
|
|
1707
|
-
enter: (node) => {
|
|
1708
|
-
fieldPath.push(node.name.value);
|
|
1709
|
-
if (ourPathFrom === JSON.stringify(fieldPath)) {
|
|
1710
|
-
fromSelection = node.selectionSet;
|
|
1711
|
-
return BREAK;
|
|
1712
|
-
}
|
|
1713
|
-
},
|
|
1714
|
-
leave: () => {
|
|
1715
|
-
fieldPath.pop();
|
|
1716
|
-
},
|
|
1717
|
-
},
|
|
1718
|
-
});
|
|
1719
|
-
fieldPath = [];
|
|
1720
|
-
const document = visit(originalRequest.document, {
|
|
1721
|
-
[Kind.FIELD]: {
|
|
1722
|
-
enter: (node) => {
|
|
1723
|
-
fieldPath.push(node.name.value);
|
|
1724
|
-
if (ourPathTo === JSON.stringify(fieldPath) && fromSelection != null) {
|
|
1725
|
-
return {
|
|
1726
|
-
...node,
|
|
1727
|
-
selectionSet: fromSelection,
|
|
1728
|
-
};
|
|
1729
|
-
}
|
|
1730
|
-
},
|
|
1731
|
-
leave: () => {
|
|
1732
|
-
fieldPath.pop();
|
|
1733
|
-
},
|
|
1734
|
-
},
|
|
1735
|
-
});
|
|
1736
|
-
return {
|
|
1737
|
-
...originalRequest,
|
|
1738
|
-
document,
|
|
1739
|
-
};
|
|
1740
|
-
}
|
|
1741
|
-
}
|
|
1742
|
-
|
|
1743
|
-
function getSchemaFromIntrospection(introspectionResult, options) {
|
|
1744
|
-
var _a;
|
|
1745
|
-
if ((_a = introspectionResult === null || introspectionResult === void 0 ? void 0 : introspectionResult.data) === null || _a === void 0 ? void 0 : _a.__schema) {
|
|
1746
|
-
return buildClientSchema(introspectionResult.data, options);
|
|
1747
|
-
}
|
|
1748
|
-
throw new Error('Could not obtain introspection result, received: ' + JSON.stringify(introspectionResult));
|
|
1749
|
-
}
|
|
1750
|
-
function introspectSchema(executor, context, options) {
|
|
1751
|
-
const parsedIntrospectionQuery = parse(getIntrospectionQuery(options), options);
|
|
1752
|
-
return new ValueOrPromise(() => executor({
|
|
1753
|
-
document: parsedIntrospectionQuery,
|
|
1754
|
-
context,
|
|
1755
|
-
}))
|
|
1756
|
-
.then(introspection => {
|
|
1757
|
-
if (isAsyncIterable(introspection)) {
|
|
1758
|
-
const iterator = introspection[Symbol.asyncIterator]();
|
|
1759
|
-
return iterator.next().then(({ value }) => value);
|
|
1760
|
-
}
|
|
1761
|
-
return introspection;
|
|
1762
|
-
})
|
|
1763
|
-
.then(introspection => getSchemaFromIntrospection(introspection, options))
|
|
1764
|
-
.resolve();
|
|
1765
|
-
}
|
|
1766
|
-
|
|
1767
|
-
export { ExtractField, FilterInputObjectFields, FilterInterfaceFields, FilterObjectFieldDirectives, FilterObjectFields, FilterRootFields, FilterTypes, HoistField, MapFields, MapLeafValues, PruneTypes as PruneSchema, RemoveObjectFieldDeprecations, RemoveObjectFieldDirectives, RemoveObjectFieldsWithDeprecation, RemoveObjectFieldsWithDirective, RenameInputObjectFields, RenameInterfaceFields, RenameObjectFieldArguments, RenameObjectFields, RenameRootFields, RenameRootTypes, RenameTypes, TransformCompositeFields, TransformEnumValues, TransformInputObjectFields, TransformInterfaceFields, TransformObjectFields, TransformQuery, TransformRootFields, WrapFields, WrapQuery, WrapType, defaultCreateProxyingResolver, generateProxyingResolvers, introspectSchema, wrapSchema };
|