@graphql-tools/wrap 8.4.21 → 8.5.0-alpha-6c480b2d.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/index.d.ts +0 -5
- package/index.js +0 -1806
- package/index.mjs +0 -1767
- package/transforms/index.d.ts +0 -32
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { typeFromAST, TypeInfo, visit, visitWithTypeInfo, Kind, isInputType, } from 'graphql';
|
|
2
|
+
import { MapperKind, mapSchema, transformInputValue } from '@graphql-tools/utils';
|
|
3
|
+
export default class TransformInputObjectFields {
|
|
4
|
+
constructor(inputFieldTransformer, inputFieldNodeTransformer, inputObjectNodeTransformer) {
|
|
5
|
+
this.inputFieldTransformer = inputFieldTransformer;
|
|
6
|
+
this.inputFieldNodeTransformer = inputFieldNodeTransformer;
|
|
7
|
+
this.inputObjectNodeTransformer = inputObjectNodeTransformer;
|
|
8
|
+
this.mapping = {};
|
|
9
|
+
}
|
|
10
|
+
_getTransformedSchema() {
|
|
11
|
+
const transformedSchema = this.transformedSchema;
|
|
12
|
+
if (transformedSchema === undefined) {
|
|
13
|
+
throw new Error(`The TransformInputObjectFields transform's "transformRequest" and "transformResult" methods cannot be used without first calling "transformSchema".`);
|
|
14
|
+
}
|
|
15
|
+
return transformedSchema;
|
|
16
|
+
}
|
|
17
|
+
transformSchema(originalWrappingSchema, _subschemaConfig, _transformedSchema) {
|
|
18
|
+
this.transformedSchema = mapSchema(originalWrappingSchema, {
|
|
19
|
+
[MapperKind.INPUT_OBJECT_FIELD]: (inputFieldConfig, fieldName, typeName) => {
|
|
20
|
+
const transformedInputField = this.inputFieldTransformer(typeName, fieldName, inputFieldConfig);
|
|
21
|
+
if (Array.isArray(transformedInputField)) {
|
|
22
|
+
const newFieldName = transformedInputField[0];
|
|
23
|
+
if (newFieldName !== fieldName) {
|
|
24
|
+
if (!(typeName in this.mapping)) {
|
|
25
|
+
this.mapping[typeName] = {};
|
|
26
|
+
}
|
|
27
|
+
this.mapping[typeName][newFieldName] = fieldName;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return transformedInputField;
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
return this.transformedSchema;
|
|
34
|
+
}
|
|
35
|
+
transformRequest(originalRequest, delegationContext, _transformationContext) {
|
|
36
|
+
var _a;
|
|
37
|
+
const variableValues = (_a = originalRequest.variables) !== null && _a !== void 0 ? _a : {};
|
|
38
|
+
const fragments = Object.create(null);
|
|
39
|
+
const operations = [];
|
|
40
|
+
for (const def of originalRequest.document.definitions) {
|
|
41
|
+
if (def.kind === Kind.OPERATION_DEFINITION) {
|
|
42
|
+
operations.push(def);
|
|
43
|
+
}
|
|
44
|
+
else if (def.kind === Kind.FRAGMENT_DEFINITION) {
|
|
45
|
+
fragments[def.name.value] = def;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
for (const def of operations) {
|
|
49
|
+
const variableDefs = def.variableDefinitions;
|
|
50
|
+
if (variableDefs != null) {
|
|
51
|
+
for (const variableDef of variableDefs) {
|
|
52
|
+
const varName = variableDef.variable.name.value;
|
|
53
|
+
// Cast to NamedTypeNode required until upcomming graphql releases will have TypeNode paramter
|
|
54
|
+
const varType = typeFromAST(delegationContext.transformedSchema, variableDef.type);
|
|
55
|
+
if (!isInputType(varType)) {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
variableValues[varName] = transformInputValue(varType, variableValues[varName], undefined, (type, originalValue) => {
|
|
59
|
+
var _a;
|
|
60
|
+
const newValue = Object.create(null);
|
|
61
|
+
const fields = type.getFields();
|
|
62
|
+
for (const key in originalValue) {
|
|
63
|
+
const field = fields[key];
|
|
64
|
+
if (field != null) {
|
|
65
|
+
const newFieldName = (_a = this.mapping[type.name]) === null || _a === void 0 ? void 0 : _a[field.name];
|
|
66
|
+
if (newFieldName != null) {
|
|
67
|
+
newValue[newFieldName] = originalValue[field.name];
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
newValue[field.name] = originalValue[field.name];
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return newValue;
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
for (const def of originalRequest.document.definitions.filter(def => def.kind === Kind.FRAGMENT_DEFINITION)) {
|
|
80
|
+
fragments[def.name.value] = def;
|
|
81
|
+
}
|
|
82
|
+
const document = this.transformDocument(originalRequest.document, this.mapping, this.inputFieldNodeTransformer, this.inputObjectNodeTransformer, originalRequest, delegationContext);
|
|
83
|
+
return {
|
|
84
|
+
...originalRequest,
|
|
85
|
+
document,
|
|
86
|
+
variables: variableValues,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
transformDocument(document, mapping, inputFieldNodeTransformer, inputObjectNodeTransformer, request, delegationContext) {
|
|
90
|
+
const typeInfo = new TypeInfo(this._getTransformedSchema());
|
|
91
|
+
const newDocument = visit(document, visitWithTypeInfo(typeInfo, {
|
|
92
|
+
[Kind.OBJECT]: {
|
|
93
|
+
leave: (node) => {
|
|
94
|
+
// The casting is kind of legit here as we are in a visitor
|
|
95
|
+
const parentType = typeInfo.getInputType();
|
|
96
|
+
if (parentType != null) {
|
|
97
|
+
const parentTypeName = parentType.name;
|
|
98
|
+
const newInputFields = [];
|
|
99
|
+
for (const inputField of node.fields) {
|
|
100
|
+
const newName = inputField.name.value;
|
|
101
|
+
const transformedInputField = inputFieldNodeTransformer != null
|
|
102
|
+
? inputFieldNodeTransformer(parentTypeName, newName, inputField, request, delegationContext)
|
|
103
|
+
: inputField;
|
|
104
|
+
if (Array.isArray(transformedInputField)) {
|
|
105
|
+
for (const individualTransformedInputField of transformedInputField) {
|
|
106
|
+
const typeMapping = mapping[parentTypeName];
|
|
107
|
+
if (typeMapping == null) {
|
|
108
|
+
newInputFields.push(individualTransformedInputField);
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
const oldName = typeMapping[newName];
|
|
112
|
+
if (oldName == null) {
|
|
113
|
+
newInputFields.push(individualTransformedInputField);
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
newInputFields.push({
|
|
117
|
+
...individualTransformedInputField,
|
|
118
|
+
name: {
|
|
119
|
+
...individualTransformedInputField.name,
|
|
120
|
+
value: oldName,
|
|
121
|
+
},
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
const typeMapping = mapping[parentTypeName];
|
|
127
|
+
if (typeMapping == null) {
|
|
128
|
+
newInputFields.push(transformedInputField);
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
const oldName = typeMapping[newName];
|
|
132
|
+
if (oldName == null) {
|
|
133
|
+
newInputFields.push(transformedInputField);
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
newInputFields.push({
|
|
137
|
+
...transformedInputField,
|
|
138
|
+
name: {
|
|
139
|
+
...transformedInputField.name,
|
|
140
|
+
value: oldName,
|
|
141
|
+
},
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
const newNode = {
|
|
145
|
+
...node,
|
|
146
|
+
fields: newInputFields,
|
|
147
|
+
};
|
|
148
|
+
return inputObjectNodeTransformer != null
|
|
149
|
+
? inputObjectNodeTransformer(parentTypeName, newNode, request, delegationContext)
|
|
150
|
+
: newNode;
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
},
|
|
154
|
+
}));
|
|
155
|
+
return newDocument;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { isInterfaceType } from 'graphql';
|
|
2
|
+
import TransformCompositeFields from './TransformCompositeFields.js';
|
|
3
|
+
export default class TransformInterfaceFields {
|
|
4
|
+
constructor(interfaceFieldTransformer, fieldNodeTransformer) {
|
|
5
|
+
this.interfaceFieldTransformer = interfaceFieldTransformer;
|
|
6
|
+
this.fieldNodeTransformer = fieldNodeTransformer;
|
|
7
|
+
}
|
|
8
|
+
_getTransformer() {
|
|
9
|
+
const transformer = this.transformer;
|
|
10
|
+
if (transformer === undefined) {
|
|
11
|
+
throw new Error(`The TransformInterfaceFields transform's "transformRequest" and "transformResult" methods cannot be used without first calling "transformSchema".`);
|
|
12
|
+
}
|
|
13
|
+
return transformer;
|
|
14
|
+
}
|
|
15
|
+
transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
|
16
|
+
const compositeToObjectFieldTransformer = (typeName, fieldName, fieldConfig) => {
|
|
17
|
+
if (isInterfaceType(originalWrappingSchema.getType(typeName))) {
|
|
18
|
+
return this.interfaceFieldTransformer(typeName, fieldName, fieldConfig);
|
|
19
|
+
}
|
|
20
|
+
return undefined;
|
|
21
|
+
};
|
|
22
|
+
this.transformer = new TransformCompositeFields(compositeToObjectFieldTransformer, this.fieldNodeTransformer);
|
|
23
|
+
return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
|
24
|
+
}
|
|
25
|
+
transformRequest(originalRequest, delegationContext, transformationContext) {
|
|
26
|
+
return this._getTransformer().transformRequest(originalRequest, delegationContext, transformationContext);
|
|
27
|
+
}
|
|
28
|
+
transformResult(originalResult, delegationContext, transformationContext) {
|
|
29
|
+
return this._getTransformer().transformResult(originalResult, delegationContext, transformationContext);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { isObjectType } from 'graphql';
|
|
2
|
+
import TransformCompositeFields from './TransformCompositeFields.js';
|
|
3
|
+
export default class TransformObjectFields {
|
|
4
|
+
constructor(objectFieldTransformer, fieldNodeTransformer) {
|
|
5
|
+
this.objectFieldTransformer = objectFieldTransformer;
|
|
6
|
+
this.fieldNodeTransformer = fieldNodeTransformer;
|
|
7
|
+
}
|
|
8
|
+
_getTransformer() {
|
|
9
|
+
const transformer = this.transformer;
|
|
10
|
+
if (transformer === undefined) {
|
|
11
|
+
throw new Error(`The TransformObjectFields transform's "transformRequest" and "transformResult" methods cannot be used without first calling "transformSchema".`);
|
|
12
|
+
}
|
|
13
|
+
return transformer;
|
|
14
|
+
}
|
|
15
|
+
transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
|
16
|
+
const compositeToObjectFieldTransformer = (typeName, fieldName, fieldConfig) => {
|
|
17
|
+
if (isObjectType(originalWrappingSchema.getType(typeName))) {
|
|
18
|
+
return this.objectFieldTransformer(typeName, fieldName, fieldConfig);
|
|
19
|
+
}
|
|
20
|
+
return undefined;
|
|
21
|
+
};
|
|
22
|
+
this.transformer = new TransformCompositeFields(compositeToObjectFieldTransformer, this.fieldNodeTransformer);
|
|
23
|
+
return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
|
24
|
+
}
|
|
25
|
+
transformRequest(originalRequest, delegationContext, transformationContext) {
|
|
26
|
+
return this._getTransformer().transformRequest(originalRequest, delegationContext, transformationContext);
|
|
27
|
+
}
|
|
28
|
+
transformResult(originalResult, delegationContext, transformationContext) {
|
|
29
|
+
return this._getTransformer().transformResult(originalResult, delegationContext, transformationContext);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { visit, Kind } from 'graphql';
|
|
2
|
+
import { relocatedError } from '@graphql-tools/utils';
|
|
3
|
+
export default class TransformQuery {
|
|
4
|
+
constructor({ path, queryTransformer, resultTransformer = result => result, errorPathTransformer = errorPath => [...errorPath], fragments = {}, }) {
|
|
5
|
+
this.path = path;
|
|
6
|
+
this.queryTransformer = queryTransformer;
|
|
7
|
+
this.resultTransformer = resultTransformer;
|
|
8
|
+
this.errorPathTransformer = errorPathTransformer;
|
|
9
|
+
this.fragments = fragments;
|
|
10
|
+
}
|
|
11
|
+
transformRequest(originalRequest, delegationContext, transformationContext) {
|
|
12
|
+
const pathLength = this.path.length;
|
|
13
|
+
let index = 0;
|
|
14
|
+
const document = visit(originalRequest.document, {
|
|
15
|
+
[Kind.FIELD]: {
|
|
16
|
+
enter: node => {
|
|
17
|
+
if (index === pathLength || node.name.value !== this.path[index]) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
index++;
|
|
21
|
+
if (index === pathLength) {
|
|
22
|
+
const selectionSet = this.queryTransformer(node.selectionSet, this.fragments, delegationContext, transformationContext);
|
|
23
|
+
return {
|
|
24
|
+
...node,
|
|
25
|
+
selectionSet,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
leave: () => {
|
|
30
|
+
index--;
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
return {
|
|
35
|
+
...originalRequest,
|
|
36
|
+
document,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
transformResult(originalResult, delegationContext, transformationContext) {
|
|
40
|
+
const data = this.transformData(originalResult.data, delegationContext, transformationContext);
|
|
41
|
+
const errors = originalResult.errors;
|
|
42
|
+
return {
|
|
43
|
+
data,
|
|
44
|
+
errors: errors != null ? this.transformErrors(errors) : undefined,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
transformData(data, delegationContext, transformationContext) {
|
|
48
|
+
const leafIndex = this.path.length - 1;
|
|
49
|
+
let index = 0;
|
|
50
|
+
let newData = data;
|
|
51
|
+
if (newData) {
|
|
52
|
+
let next = this.path[index];
|
|
53
|
+
while (index < leafIndex) {
|
|
54
|
+
if (data[next]) {
|
|
55
|
+
newData = newData[next];
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
index++;
|
|
61
|
+
next = this.path[index];
|
|
62
|
+
}
|
|
63
|
+
newData[next] = this.resultTransformer(newData[next], delegationContext, transformationContext);
|
|
64
|
+
}
|
|
65
|
+
return data;
|
|
66
|
+
}
|
|
67
|
+
transformErrors(errors) {
|
|
68
|
+
return errors.map(error => {
|
|
69
|
+
const path = error.path;
|
|
70
|
+
if (path == null) {
|
|
71
|
+
return error;
|
|
72
|
+
}
|
|
73
|
+
let match = true;
|
|
74
|
+
let index = 0;
|
|
75
|
+
while (index < this.path.length) {
|
|
76
|
+
if (path[index] !== this.path[index]) {
|
|
77
|
+
match = false;
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
index++;
|
|
81
|
+
}
|
|
82
|
+
const newPath = match ? path.slice(0, index).concat(this.errorPathTransformer(path.slice(index))) : path;
|
|
83
|
+
return relocatedError(error, newPath);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import TransformObjectFields from './TransformObjectFields.js';
|
|
2
|
+
export default class TransformRootFields {
|
|
3
|
+
constructor(rootFieldTransformer, fieldNodeTransformer) {
|
|
4
|
+
this.rootFieldTransformer = rootFieldTransformer;
|
|
5
|
+
this.fieldNodeTransformer = fieldNodeTransformer;
|
|
6
|
+
}
|
|
7
|
+
_getTransformer() {
|
|
8
|
+
const transformer = this.transformer;
|
|
9
|
+
if (transformer === undefined) {
|
|
10
|
+
throw new Error(`The TransformRootFields transform's "transformRequest" and "transformResult" methods cannot be used without first calling "transformSchema".`);
|
|
11
|
+
}
|
|
12
|
+
return transformer;
|
|
13
|
+
}
|
|
14
|
+
transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
|
15
|
+
const rootToObjectFieldTransformer = (typeName, fieldName, fieldConfig) => {
|
|
16
|
+
var _a, _b, _c;
|
|
17
|
+
if (typeName === ((_a = originalWrappingSchema.getQueryType()) === null || _a === void 0 ? void 0 : _a.name)) {
|
|
18
|
+
return this.rootFieldTransformer('Query', fieldName, fieldConfig);
|
|
19
|
+
}
|
|
20
|
+
if (typeName === ((_b = originalWrappingSchema.getMutationType()) === null || _b === void 0 ? void 0 : _b.name)) {
|
|
21
|
+
return this.rootFieldTransformer('Mutation', fieldName, fieldConfig);
|
|
22
|
+
}
|
|
23
|
+
if (typeName === ((_c = originalWrappingSchema.getSubscriptionType()) === null || _c === void 0 ? void 0 : _c.name)) {
|
|
24
|
+
return this.rootFieldTransformer('Subscription', fieldName, fieldConfig);
|
|
25
|
+
}
|
|
26
|
+
return undefined;
|
|
27
|
+
};
|
|
28
|
+
this.transformer = new TransformObjectFields(rootToObjectFieldTransformer, this.fieldNodeTransformer);
|
|
29
|
+
return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
|
30
|
+
}
|
|
31
|
+
transformRequest(originalRequest, delegationContext, transformationContext) {
|
|
32
|
+
return this._getTransformer().transformRequest(originalRequest, delegationContext, transformationContext);
|
|
33
|
+
}
|
|
34
|
+
transformResult(originalResult, delegationContext, transformationContext) {
|
|
35
|
+
return this._getTransformer().transformResult(originalResult, delegationContext, transformationContext);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { Kind, GraphQLNonNull, } from 'graphql';
|
|
2
|
+
import { appendObjectFields, selectObjectFields, modifyObjectFields, relocatedError, } from '@graphql-tools/utils';
|
|
3
|
+
import { defaultMergedResolver } from '@graphql-tools/delegate';
|
|
4
|
+
import MapFields from './MapFields.js';
|
|
5
|
+
import { defaultCreateProxyingResolver } from '../generateProxyingResolvers.js';
|
|
6
|
+
export default class WrapFields {
|
|
7
|
+
constructor(outerTypeName, wrappingFieldNames, wrappingTypeNames, fieldNames, prefix = 'gqtld') {
|
|
8
|
+
this.outerTypeName = outerTypeName;
|
|
9
|
+
this.wrappingFieldNames = wrappingFieldNames;
|
|
10
|
+
this.wrappingTypeNames = wrappingTypeNames;
|
|
11
|
+
this.numWraps = wrappingFieldNames.length;
|
|
12
|
+
this.fieldNames = fieldNames;
|
|
13
|
+
const remainingWrappingFieldNames = this.wrappingFieldNames.slice();
|
|
14
|
+
const outerMostWrappingFieldName = remainingWrappingFieldNames.shift();
|
|
15
|
+
if (outerMostWrappingFieldName == null) {
|
|
16
|
+
throw new Error(`Cannot wrap fields, no wrapping field name provided.`);
|
|
17
|
+
}
|
|
18
|
+
this.transformer = new MapFields({
|
|
19
|
+
[outerTypeName]: {
|
|
20
|
+
[outerMostWrappingFieldName]: (fieldNode, fragments, transformationContext) => hoistFieldNodes({
|
|
21
|
+
fieldNode,
|
|
22
|
+
path: remainingWrappingFieldNames,
|
|
23
|
+
fieldNames,
|
|
24
|
+
fragments,
|
|
25
|
+
transformationContext: transformationContext,
|
|
26
|
+
prefix,
|
|
27
|
+
}),
|
|
28
|
+
},
|
|
29
|
+
}, {
|
|
30
|
+
[outerTypeName]: (value, context) => dehoistValue(value, context),
|
|
31
|
+
}, (errors, context) => dehoistErrors(errors, context));
|
|
32
|
+
}
|
|
33
|
+
transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
|
34
|
+
var _a, _b, _c, _d;
|
|
35
|
+
const fieldNames = this.fieldNames;
|
|
36
|
+
const targetFieldConfigMap = selectObjectFields(originalWrappingSchema, this.outerTypeName, !fieldNames ? () => true : fieldName => fieldNames.includes(fieldName));
|
|
37
|
+
const newTargetFieldConfigMap = Object.create(null);
|
|
38
|
+
for (const fieldName in targetFieldConfigMap) {
|
|
39
|
+
const field = targetFieldConfigMap[fieldName];
|
|
40
|
+
const newField = {
|
|
41
|
+
...field,
|
|
42
|
+
resolve: defaultMergedResolver,
|
|
43
|
+
};
|
|
44
|
+
newTargetFieldConfigMap[fieldName] = newField;
|
|
45
|
+
}
|
|
46
|
+
let wrapIndex = this.numWraps - 1;
|
|
47
|
+
let wrappingTypeName = this.wrappingTypeNames[wrapIndex];
|
|
48
|
+
let wrappingFieldName = this.wrappingFieldNames[wrapIndex];
|
|
49
|
+
let newSchema = appendObjectFields(originalWrappingSchema, wrappingTypeName, newTargetFieldConfigMap);
|
|
50
|
+
for (wrapIndex--; wrapIndex > -1; wrapIndex--) {
|
|
51
|
+
const nextWrappingTypeName = this.wrappingTypeNames[wrapIndex];
|
|
52
|
+
newSchema = appendObjectFields(newSchema, nextWrappingTypeName, {
|
|
53
|
+
[wrappingFieldName]: {
|
|
54
|
+
type: new GraphQLNonNull(newSchema.getType(wrappingTypeName)),
|
|
55
|
+
resolve: defaultMergedResolver,
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
wrappingTypeName = nextWrappingTypeName;
|
|
59
|
+
wrappingFieldName = this.wrappingFieldNames[wrapIndex];
|
|
60
|
+
}
|
|
61
|
+
const wrappingRootField = this.outerTypeName === ((_a = originalWrappingSchema.getQueryType()) === null || _a === void 0 ? void 0 : _a.name) ||
|
|
62
|
+
this.outerTypeName === ((_b = originalWrappingSchema.getMutationType()) === null || _b === void 0 ? void 0 : _b.name);
|
|
63
|
+
let resolve;
|
|
64
|
+
if (transformedSchema) {
|
|
65
|
+
if (wrappingRootField) {
|
|
66
|
+
const targetSchema = subschemaConfig.schema;
|
|
67
|
+
const operation = this.outerTypeName === ((_c = targetSchema.getQueryType()) === null || _c === void 0 ? void 0 : _c.name) ? 'query' : 'mutation';
|
|
68
|
+
const createProxyingResolver = (_d = subschemaConfig.createProxyingResolver) !== null && _d !== void 0 ? _d : defaultCreateProxyingResolver;
|
|
69
|
+
resolve = createProxyingResolver({
|
|
70
|
+
subschemaConfig,
|
|
71
|
+
transformedSchema,
|
|
72
|
+
operation: operation,
|
|
73
|
+
fieldName: wrappingFieldName,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
resolve = defaultMergedResolver;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
[newSchema] = modifyObjectFields(newSchema, this.outerTypeName, fieldName => !!newTargetFieldConfigMap[fieldName], {
|
|
81
|
+
[wrappingFieldName]: {
|
|
82
|
+
type: new GraphQLNonNull(newSchema.getType(wrappingTypeName)),
|
|
83
|
+
resolve,
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
return this.transformer.transformSchema(newSchema, subschemaConfig, transformedSchema);
|
|
87
|
+
}
|
|
88
|
+
transformRequest(originalRequest, delegationContext, transformationContext) {
|
|
89
|
+
transformationContext.nextIndex = 0;
|
|
90
|
+
transformationContext.paths = Object.create(null);
|
|
91
|
+
return this.transformer.transformRequest(originalRequest, delegationContext, transformationContext);
|
|
92
|
+
}
|
|
93
|
+
transformResult(originalResult, delegationContext, transformationContext) {
|
|
94
|
+
return this.transformer.transformResult(originalResult, delegationContext, transformationContext);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
function collectFields(selectionSet, fragments, fields = [], visitedFragmentNames = {}) {
|
|
98
|
+
if (selectionSet != null) {
|
|
99
|
+
for (const selection of selectionSet.selections) {
|
|
100
|
+
switch (selection.kind) {
|
|
101
|
+
case Kind.FIELD:
|
|
102
|
+
fields.push(selection);
|
|
103
|
+
break;
|
|
104
|
+
case Kind.INLINE_FRAGMENT:
|
|
105
|
+
collectFields(selection.selectionSet, fragments, fields, visitedFragmentNames);
|
|
106
|
+
break;
|
|
107
|
+
case Kind.FRAGMENT_SPREAD: {
|
|
108
|
+
const fragmentName = selection.name.value;
|
|
109
|
+
if (!visitedFragmentNames[fragmentName]) {
|
|
110
|
+
visitedFragmentNames[fragmentName] = true;
|
|
111
|
+
collectFields(fragments[fragmentName].selectionSet, fragments, fields, visitedFragmentNames);
|
|
112
|
+
}
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
default:
|
|
116
|
+
// unreachable
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return fields;
|
|
122
|
+
}
|
|
123
|
+
function aliasFieldNode(fieldNode, str) {
|
|
124
|
+
return {
|
|
125
|
+
...fieldNode,
|
|
126
|
+
alias: {
|
|
127
|
+
kind: Kind.NAME,
|
|
128
|
+
value: str,
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
function hoistFieldNodes({ fieldNode, fieldNames, path, fragments, transformationContext, prefix, index = 0, wrappingPath = [], }) {
|
|
133
|
+
const alias = fieldNode.alias != null ? fieldNode.alias.value : fieldNode.name.value;
|
|
134
|
+
let newFieldNodes = [];
|
|
135
|
+
if (index < path.length) {
|
|
136
|
+
const pathSegment = path[index];
|
|
137
|
+
for (const possibleFieldNode of collectFields(fieldNode.selectionSet, fragments)) {
|
|
138
|
+
if (possibleFieldNode.name.value === pathSegment) {
|
|
139
|
+
const newWrappingPath = wrappingPath.concat([alias]);
|
|
140
|
+
newFieldNodes = newFieldNodes.concat(hoistFieldNodes({
|
|
141
|
+
fieldNode: possibleFieldNode,
|
|
142
|
+
fieldNames,
|
|
143
|
+
path,
|
|
144
|
+
fragments,
|
|
145
|
+
transformationContext,
|
|
146
|
+
prefix,
|
|
147
|
+
index: index + 1,
|
|
148
|
+
wrappingPath: newWrappingPath,
|
|
149
|
+
}));
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
for (const possibleFieldNode of collectFields(fieldNode.selectionSet, fragments)) {
|
|
155
|
+
if (!fieldNames || fieldNames.includes(possibleFieldNode.name.value)) {
|
|
156
|
+
const nextIndex = transformationContext.nextIndex;
|
|
157
|
+
transformationContext.nextIndex++;
|
|
158
|
+
const indexingAlias = `__${prefix}${nextIndex}__`;
|
|
159
|
+
transformationContext.paths[indexingAlias] = {
|
|
160
|
+
pathToField: wrappingPath.concat([alias]),
|
|
161
|
+
alias: possibleFieldNode.alias != null ? possibleFieldNode.alias.value : possibleFieldNode.name.value,
|
|
162
|
+
};
|
|
163
|
+
newFieldNodes.push(aliasFieldNode(possibleFieldNode, indexingAlias));
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return newFieldNodes;
|
|
168
|
+
}
|
|
169
|
+
export function dehoistValue(originalValue, context) {
|
|
170
|
+
if (originalValue == null) {
|
|
171
|
+
return originalValue;
|
|
172
|
+
}
|
|
173
|
+
const newValue = Object.create(null);
|
|
174
|
+
for (const alias in originalValue) {
|
|
175
|
+
let obj = newValue;
|
|
176
|
+
const path = context.paths[alias];
|
|
177
|
+
if (path == null) {
|
|
178
|
+
newValue[alias] = originalValue[alias];
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
const pathToField = path.pathToField;
|
|
182
|
+
const fieldAlias = path.alias;
|
|
183
|
+
for (const key of pathToField) {
|
|
184
|
+
obj = obj[key] = obj[key] || Object.create(null);
|
|
185
|
+
}
|
|
186
|
+
obj[fieldAlias] = originalValue[alias];
|
|
187
|
+
}
|
|
188
|
+
return newValue;
|
|
189
|
+
}
|
|
190
|
+
function dehoistErrors(errors, context) {
|
|
191
|
+
if (errors === undefined) {
|
|
192
|
+
return undefined;
|
|
193
|
+
}
|
|
194
|
+
return errors.map(error => {
|
|
195
|
+
const originalPath = error.path;
|
|
196
|
+
if (originalPath == null) {
|
|
197
|
+
return error;
|
|
198
|
+
}
|
|
199
|
+
let newPath = [];
|
|
200
|
+
for (const pathSegment of originalPath) {
|
|
201
|
+
if (typeof pathSegment !== 'string') {
|
|
202
|
+
newPath.push(pathSegment);
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
const path = context.paths[pathSegment];
|
|
206
|
+
if (path == null) {
|
|
207
|
+
newPath.push(pathSegment);
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
newPath = newPath.concat(path.pathToField, [path.alias]);
|
|
211
|
+
}
|
|
212
|
+
return relocatedError(error, newPath);
|
|
213
|
+
});
|
|
214
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { visit, Kind } from 'graphql';
|
|
2
|
+
export default class WrapQuery {
|
|
3
|
+
constructor(path, wrapper, extractor) {
|
|
4
|
+
this.path = path;
|
|
5
|
+
this.wrapper = wrapper;
|
|
6
|
+
this.extractor = extractor;
|
|
7
|
+
}
|
|
8
|
+
transformRequest(originalRequest, _delegationContext, _transformationContext) {
|
|
9
|
+
const fieldPath = [];
|
|
10
|
+
const ourPath = JSON.stringify(this.path);
|
|
11
|
+
const document = visit(originalRequest.document, {
|
|
12
|
+
[Kind.FIELD]: {
|
|
13
|
+
enter: (node) => {
|
|
14
|
+
fieldPath.push(node.name.value);
|
|
15
|
+
if (node.selectionSet != null && ourPath === JSON.stringify(fieldPath)) {
|
|
16
|
+
const wrapResult = this.wrapper(node.selectionSet);
|
|
17
|
+
// Selection can be either a single selection or a selection set. If it's just one selection,
|
|
18
|
+
// let's wrap it in a selection set. Otherwise, keep it as is.
|
|
19
|
+
const selectionSet = wrapResult != null && wrapResult.kind === Kind.SELECTION_SET
|
|
20
|
+
? wrapResult
|
|
21
|
+
: {
|
|
22
|
+
kind: Kind.SELECTION_SET,
|
|
23
|
+
selections: [wrapResult],
|
|
24
|
+
};
|
|
25
|
+
return {
|
|
26
|
+
...node,
|
|
27
|
+
selectionSet,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
leave: () => {
|
|
32
|
+
fieldPath.pop();
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
return {
|
|
37
|
+
...originalRequest,
|
|
38
|
+
document,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
transformResult(originalResult, _delegationContext, _transformationContext) {
|
|
42
|
+
const rootData = originalResult.data;
|
|
43
|
+
if (rootData != null) {
|
|
44
|
+
let data = rootData;
|
|
45
|
+
const path = [...this.path];
|
|
46
|
+
while (path.length > 1) {
|
|
47
|
+
const next = path.shift();
|
|
48
|
+
if (data[next]) {
|
|
49
|
+
data = data[next];
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
data[path[0]] = this.extractor(data[path[0]]);
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
data: rootData,
|
|
56
|
+
errors: originalResult.errors,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import WrapFields from './WrapFields.js';
|
|
2
|
+
export default class WrapType {
|
|
3
|
+
constructor(outerTypeName, innerTypeName, fieldName) {
|
|
4
|
+
this.transformer = new WrapFields(outerTypeName, [fieldName], [innerTypeName]);
|
|
5
|
+
}
|
|
6
|
+
transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
|
7
|
+
return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
|
|
8
|
+
}
|
|
9
|
+
transformRequest(originalRequest, delegationContext, transformationContext) {
|
|
10
|
+
return this.transformer.transformRequest(originalRequest, delegationContext, transformationContext);
|
|
11
|
+
}
|
|
12
|
+
transformResult(originalResult, delegationContext, transformationContext) {
|
|
13
|
+
return this.transformer.transformResult(originalResult, delegationContext, transformationContext);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export { default as RenameTypes } from './RenameTypes.js';
|
|
2
|
+
export { default as FilterTypes } from './FilterTypes.js';
|
|
3
|
+
export { default as RenameRootTypes } from './RenameRootTypes.js';
|
|
4
|
+
export { default as TransformCompositeFields } from './TransformCompositeFields.js';
|
|
5
|
+
export { default as TransformRootFields } from './TransformRootFields.js';
|
|
6
|
+
export { default as RenameRootFields } from './RenameRootFields.js';
|
|
7
|
+
export { default as FilterRootFields } from './FilterRootFields.js';
|
|
8
|
+
export { default as TransformObjectFields } from './TransformObjectFields.js';
|
|
9
|
+
export { default as RenameObjectFields } from './RenameObjectFields.js';
|
|
10
|
+
export { default as RenameObjectFieldArguments } from './RenameObjectFieldArguments.js';
|
|
11
|
+
export { default as FilterObjectFields } from './FilterObjectFields.js';
|
|
12
|
+
export { default as TransformInterfaceFields } from './TransformInterfaceFields.js';
|
|
13
|
+
export { default as RenameInterfaceFields } from './RenameInterfaceFields.js';
|
|
14
|
+
export { default as FilterInterfaceFields } from './FilterInterfaceFields.js';
|
|
15
|
+
export { default as TransformInputObjectFields } from './TransformInputObjectFields.js';
|
|
16
|
+
export { default as RenameInputObjectFields } from './RenameInputObjectFields.js';
|
|
17
|
+
export { default as FilterInputObjectFields } from './FilterInputObjectFields.js';
|
|
18
|
+
export { default as MapLeafValues } from './MapLeafValues.js';
|
|
19
|
+
export { default as TransformEnumValues } from './TransformEnumValues.js';
|
|
20
|
+
export { default as TransformQuery } from './TransformQuery.js';
|
|
21
|
+
export { default as FilterObjectFieldDirectives } from './FilterObjectFieldDirectives.js';
|
|
22
|
+
export { default as RemoveObjectFieldDirectives } from './RemoveObjectFieldDirectives.js';
|
|
23
|
+
export { default as RemoveObjectFieldsWithDirective } from './RemoveObjectFieldsWithDirective.js';
|
|
24
|
+
export { default as RemoveObjectFieldDeprecations } from './RemoveObjectFieldDeprecations.js';
|
|
25
|
+
export { default as RemoveObjectFieldsWithDeprecation } from './RemoveObjectFieldsWithDeprecation.js';
|
|
26
|
+
export { default as PruneSchema } from './PruneSchema.js';
|
|
27
|
+
export { default as WrapType } from './WrapType.js';
|
|
28
|
+
export { default as WrapFields } from './WrapFields.js';
|
|
29
|
+
export { default as HoistField } from './HoistField.js';
|
|
30
|
+
export { default as MapFields } from './MapFields.js';
|
|
31
|
+
// superseded by TransformQuery
|
|
32
|
+
export { default as WrapQuery } from './WrapQuery.js';
|
|
33
|
+
export { default as ExtractField } from './ExtractField.js';
|
package/esm/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|