/
TransformCompositeFields.ts
190 lines (165 loc) · 6.15 KB
/
TransformCompositeFields.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
import {
GraphQLSchema,
GraphQLType,
DocumentNode,
TypeInfo,
visit,
visitWithTypeInfo,
Kind,
SelectionSetNode,
SelectionNode,
FragmentDefinitionNode,
GraphQLInterfaceType,
isObjectType,
isInterfaceType,
GraphQLObjectType,
} from 'graphql';
import { Transform } from '@graphql-tools/delegate';
import { Request, MapperKind, mapSchema } from '@graphql-tools/utils';
import { FieldTransformer, FieldNodeTransformer } from '../types';
export default class TransformCompositeFields implements Transform {
private readonly fieldTransformer: FieldTransformer;
private readonly fieldNodeTransformer: FieldNodeTransformer;
private transformedSchema: GraphQLSchema;
private mapping: Record<string, Record<string, string>>;
constructor(fieldTransformer: FieldTransformer, fieldNodeTransformer?: FieldNodeTransformer) {
this.fieldTransformer = fieldTransformer;
this.fieldNodeTransformer = fieldNodeTransformer;
this.mapping = {};
}
public transformSchema(originalSchema: GraphQLSchema): GraphQLSchema {
this.transformedSchema = mapSchema(originalSchema, {
[MapperKind.OBJECT_TYPE]: (type: GraphQLObjectType) => this.transformFields(type, this.fieldTransformer),
[MapperKind.INTERFACE_TYPE]: (type: GraphQLInterfaceType) => this.transformFields(type, this.fieldTransformer),
});
return this.transformedSchema;
}
public transformRequest(originalRequest: Request, transformationContext: Record<string, any>): Request {
const fragments = Object.create(null);
originalRequest.document.definitions
.filter(def => def.kind === Kind.FRAGMENT_DEFINITION)
.forEach(def => {
fragments[(def as FragmentDefinitionNode).name.value] = def;
});
const document = this.transformDocument(
originalRequest.document,
transformationContext.transformedSchema,
this.mapping,
this.fieldNodeTransformer,
fragments
);
return {
...originalRequest,
document,
};
}
private transformFields(type: GraphQLObjectType, fieldTransformer: FieldTransformer): GraphQLObjectType;
private transformFields(type: GraphQLInterfaceType, fieldTransformer: FieldTransformer): GraphQLInterfaceType;
private transformFields(type: GraphQLObjectType | GraphQLInterfaceType, fieldTransformer: FieldTransformer): any {
const config = type.toConfig();
const originalFieldConfigMap = config.fields;
const newFieldConfigMap = {};
Object.keys(originalFieldConfigMap).forEach(fieldName => {
const originalfieldConfig = originalFieldConfigMap[fieldName];
const transformedField = fieldTransformer(type.name, fieldName, originalfieldConfig);
if (transformedField === undefined) {
newFieldConfigMap[fieldName] = originalfieldConfig;
} else if (Array.isArray(transformedField)) {
const newFieldName = transformedField[0];
const newFieldConfig = transformedField[1];
newFieldConfigMap[newFieldName] = newFieldConfig;
if (newFieldName !== fieldName) {
const typeName = type.name;
if (!(typeName in this.mapping)) {
this.mapping[typeName] = {};
}
this.mapping[typeName][newFieldName] = fieldName;
}
} else if (transformedField != null) {
newFieldConfigMap[fieldName] = transformedField;
}
});
if (!Object.keys(newFieldConfigMap).length) {
return null;
}
if (isObjectType(type)) {
return new GraphQLObjectType({
...type.toConfig(),
fields: newFieldConfigMap,
});
} else if (isInterfaceType(type)) {
return new GraphQLInterfaceType({
...type.toConfig(),
fields: newFieldConfigMap,
});
}
}
private transformDocument(
document: DocumentNode,
transformedSchema: GraphQLSchema,
mapping: Record<string, Record<string, string>>,
fieldNodeTransformer?: FieldNodeTransformer,
fragments: Record<string, FragmentDefinitionNode> = {}
): DocumentNode {
const typeInfo = new TypeInfo(transformedSchema);
const newDocument: DocumentNode = visit(
document,
visitWithTypeInfo(typeInfo, {
leave: {
[Kind.SELECTION_SET]: (node: SelectionSetNode): SelectionSetNode => {
const parentType: GraphQLType = typeInfo.getParentType();
if (parentType != null) {
const parentTypeName = parentType.name;
let newSelections: Array<SelectionNode> = [];
node.selections.forEach(selection => {
if (selection.kind !== Kind.FIELD) {
newSelections.push(selection);
return;
}
const newName = selection.name.value;
const transformedSelection =
fieldNodeTransformer != null
? fieldNodeTransformer(parentTypeName, newName, selection, fragments)
: selection;
if (Array.isArray(transformedSelection)) {
newSelections = newSelections.concat(transformedSelection);
return;
}
if (transformedSelection.kind !== Kind.FIELD) {
newSelections.push(transformedSelection);
return;
}
const typeMapping = mapping[parentTypeName];
if (typeMapping == null) {
newSelections.push(transformedSelection);
return;
}
const oldName = mapping[parentTypeName][newName];
if (oldName == null) {
newSelections.push(transformedSelection);
return;
}
newSelections.push({
...transformedSelection,
name: {
kind: Kind.NAME,
value: oldName,
},
alias: {
kind: Kind.NAME,
value: newName,
},
});
});
return {
...node,
selections: newSelections,
};
}
},
},
})
);
return newDocument;
}
}