-
-
Notifications
You must be signed in to change notification settings - Fork 819
/
TransformInputObjectFields.ts
237 lines (212 loc) · 8.26 KB
/
TransformInputObjectFields.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import {
GraphQLSchema,
DocumentNode,
typeFromAST,
TypeInfo,
visit,
visitWithTypeInfo,
Kind,
FragmentDefinitionNode,
GraphQLInputObjectType,
ObjectValueNode,
ObjectFieldNode,
OperationDefinitionNode,
isInputType,
NamedTypeNode,
} from 'graphql';
import { Maybe, ExecutionRequest, MapperKind, mapSchema, transformInputValue } from '@graphql-tools/utils';
import { Transform, DelegationContext, SubschemaConfig } from '@graphql-tools/delegate';
import { InputFieldTransformer, InputFieldNodeTransformer, InputObjectNodeTransformer } from '../types';
export default class TransformInputObjectFields implements Transform {
private readonly inputFieldTransformer: InputFieldTransformer;
private readonly inputFieldNodeTransformer: InputFieldNodeTransformer | undefined;
private readonly inputObjectNodeTransformer: InputObjectNodeTransformer | undefined;
private transformedSchema: GraphQLSchema | undefined;
private mapping: Record<string, Record<string, string>>;
constructor(
inputFieldTransformer: InputFieldTransformer,
inputFieldNodeTransformer?: InputFieldNodeTransformer,
inputObjectNodeTransformer?: InputObjectNodeTransformer
) {
this.inputFieldTransformer = inputFieldTransformer;
this.inputFieldNodeTransformer = inputFieldNodeTransformer;
this.inputObjectNodeTransformer = inputObjectNodeTransformer;
this.mapping = {};
}
private _getTransformedSchema() {
const transformedSchema = this.transformedSchema;
if (transformedSchema === undefined) {
throw new Error(
`The TransformInputObjectFields transform's "transformRequest" and "transformResult" methods cannot be used without first calling "transformSchema".`
);
}
return transformedSchema;
}
public transformSchema(
originalWrappingSchema: GraphQLSchema,
_subschemaConfig: SubschemaConfig,
_transformedSchema?: GraphQLSchema
): GraphQLSchema {
this.transformedSchema = mapSchema(originalWrappingSchema, {
[MapperKind.INPUT_OBJECT_FIELD]: (inputFieldConfig, fieldName, typeName) => {
const transformedInputField = this.inputFieldTransformer(typeName, fieldName, inputFieldConfig);
if (Array.isArray(transformedInputField)) {
const newFieldName = transformedInputField[0];
if (newFieldName !== fieldName) {
if (!(typeName in this.mapping)) {
this.mapping[typeName] = {};
}
this.mapping[typeName][newFieldName] = fieldName;
}
}
return transformedInputField;
},
});
return this.transformedSchema;
}
public transformRequest(
originalRequest: ExecutionRequest,
delegationContext: DelegationContext,
_transformationContext: Record<string, any>
): ExecutionRequest {
const variableValues = originalRequest.variables ?? {};
const fragments = Object.create(null);
const operations: Array<OperationDefinitionNode> = [];
for (const def of originalRequest.document.definitions) {
if (def.kind === Kind.OPERATION_DEFINITION) {
operations.push(def);
} else if (def.kind === Kind.FRAGMENT_DEFINITION) {
fragments[def.name.value] = def;
}
}
for (const def of operations) {
const variableDefs = def.variableDefinitions;
if (variableDefs != null) {
for (const variableDef of variableDefs) {
const varName = variableDef.variable.name.value;
// Cast to NamedTypeNode required until upcomming graphql releases will have TypeNode paramter
const varType = typeFromAST(delegationContext.transformedSchema, variableDef.type as NamedTypeNode);
if (!isInputType(varType)) {
throw new Error(`Expected ${varType} to be an input type`);
}
variableValues[varName] = transformInputValue(
varType,
variableValues[varName],
undefined,
(type, originalValue) => {
const newValue = Object.create(null);
const fields = type.getFields();
for (const key in originalValue) {
const field = fields[key];
if (field != null) {
const newFieldName = this.mapping[type.name]?.[field.name];
if (newFieldName != null) {
newValue[newFieldName] = originalValue[field.name];
} else {
newValue[field.name] = originalValue[field.name];
}
}
}
return newValue;
}
);
}
}
}
for (const def of originalRequest.document.definitions.filter(def => def.kind === Kind.FRAGMENT_DEFINITION)) {
fragments[(def as FragmentDefinitionNode).name.value] = def;
}
const document = this.transformDocument(
originalRequest.document,
this.mapping,
this.inputFieldNodeTransformer,
this.inputObjectNodeTransformer,
originalRequest,
delegationContext
);
return {
...originalRequest,
document,
variables: variableValues,
};
}
private transformDocument(
document: DocumentNode,
mapping: Record<string, Record<string, string>>,
inputFieldNodeTransformer: InputFieldNodeTransformer | undefined,
inputObjectNodeTransformer: InputObjectNodeTransformer | undefined,
request: ExecutionRequest,
delegationContext?: DelegationContext
): DocumentNode {
const typeInfo = new TypeInfo(this._getTransformedSchema());
const newDocument: DocumentNode = visit(
document,
visitWithTypeInfo(typeInfo, {
[Kind.OBJECT]: {
leave: (node: ObjectValueNode): ObjectValueNode | undefined => {
// The casting is kind of legit here as we are in a visitor
const parentType = typeInfo.getInputType() as Maybe<GraphQLInputObjectType>;
if (parentType != null) {
const parentTypeName = parentType.name;
const newInputFields: Array<ObjectFieldNode> = [];
for (const inputField of node.fields) {
const newName = inputField.name.value;
const transformedInputField =
inputFieldNodeTransformer != null
? inputFieldNodeTransformer(parentTypeName, newName, inputField, request, delegationContext)
: inputField;
if (Array.isArray(transformedInputField)) {
for (const individualTransformedInputField of transformedInputField) {
const typeMapping = mapping[parentTypeName];
if (typeMapping == null) {
newInputFields.push(individualTransformedInputField);
continue;
}
const oldName = typeMapping[newName];
if (oldName == null) {
newInputFields.push(individualTransformedInputField);
continue;
}
newInputFields.push({
...individualTransformedInputField,
name: {
...individualTransformedInputField.name,
value: oldName,
},
});
}
continue;
}
const typeMapping = mapping[parentTypeName];
if (typeMapping == null) {
newInputFields.push(transformedInputField);
continue;
}
const oldName = typeMapping[newName];
if (oldName == null) {
newInputFields.push(transformedInputField);
continue;
}
newInputFields.push({
...transformedInputField,
name: {
...transformedInputField.name,
value: oldName,
},
});
}
const newNode = {
...node,
fields: newInputFields,
};
return inputObjectNodeTransformer != null
? inputObjectNodeTransformer(parentTypeName, newNode, request, delegationContext)
: newNode;
}
},
},
})
);
return newDocument;
}
}