This repository has been archived by the owner on Sep 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
schema-transformer.spec.ts
338 lines (310 loc) · 11.3 KB
/
schema-transformer.spec.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import {
buildASTSchema,
GraphQLFieldConfigMap, GraphQLID, GraphQLInt, GraphQLInterfaceType, GraphQLObjectType, GraphQLObjectTypeConfig,
GraphQLScalarType,
GraphQLSchema,
GraphQLString,
GraphQLUnionType, parse
} from 'graphql';
import {
FieldsTransformationContext, GraphQLNamedFieldConfig, transformSchema
} from '../src/schema-transformer';
import { walkFields } from '../src/schema-utils';
import gql from 'graphql-tag';
import { removeUnusedTypesFromSchema } from '../src/remove-unused-types';
describe('schema-transformer', () => {
it('can copy types', () => {
const type1 = new GraphQLObjectType({
name: 'Type1',
fields: {
scalar: {
type: GraphQLString
}
}
});
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
field1: {
type: type1
}
}
})
});
const newSchema = transformSchema(schema, {
transformFields(config: GraphQLFieldConfigMap<any, any>, context: FieldsTransformationContext) {
if (context.oldOuterType.name == 'Query') {
const field1 = config['field1'];
const type2 = context.copyType(field1.type, {
transformObjectType(typeConfig: GraphQLObjectTypeConfig<any, any>) {
return {
...typeConfig,
name: 'Type2'
};
},
transformFields(fieldConfig: GraphQLFieldConfigMap<any, any>) {
return {
...fieldConfig,
clone: {
type: GraphQLInt,
resolve: () => 42
}
};
}
});
return {
...config,
field2: {
type: type2
}
};
}
return config;
}
});
expect(walkFields(newSchema.getQueryType()!, ['field1', 'scalar'])).toBeDefined('type1.scalar is missing');
expect(walkFields(newSchema.getQueryType()!, ['field2', 'scalar'])).toBeDefined('type2.scalar is missing');
expect(walkFields(newSchema.getQueryType()!, ['field1', 'clone'])).toBeUndefined('type2.clone should not be there');
expect(walkFields(newSchema.getQueryType()!, ['field2', 'clone'])).toBeDefined('type2.clone is missing');
});
it('supports union types', () => {
const option1 = new GraphQLObjectType({
name: 'Option1',
fields: {option1: {type: GraphQLInt}},
isTypeOf: (obj) => { return 'option1' in obj; }
});
const option2 = new GraphQLObjectType({
name: 'Option2',
fields: {option2: {type: GraphQLInt}},
isTypeOf: (obj) => { return 'option1' in obj; }
});
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
union: {
type: new GraphQLUnionType({
name: 'Union',
types: [ option1, option2 ]
})
}
}
})
});
const newSchema = transformSchema(schema, {
transformField(config: GraphQLNamedFieldConfig<any, any>) {
return {
...config,
name: config.name + '_'
};
}
});
const unionType = newSchema.getQueryType()!.getFields()['union_'].type;
expect(unionType instanceof GraphQLUnionType).toBeTruthy();
expect((unionType as GraphQLUnionType).getTypes().length).toBe(2);
});
it('supports the README case', () => {
const myType = new GraphQLObjectType({
name: 'MyType',
fields: {
name: {
type: GraphQLString
}
}
});
const originalSchema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
myField: {
type: myType
}
}
})
});
const transformedSchema = transformSchema(originalSchema, {
transformField(field: GraphQLNamedFieldConfig<any, any>, context) {
// Rename a field in a type
if (context.oldOuterType.name == 'MyType') {
return {
...field,
name: field.name + 'ButCooler'
}
}
return field;
},
transformObjectType(type: GraphQLObjectTypeConfig<any, any>) {
if (type.name == 'MyType') {
return {
...type,
name: 'MyCoolType'
};
}
return type;
},
transformFields(fields: GraphQLFieldConfigMap<any, any>, context) {
// You can even copy types on the fly and transform the copies
const type2 = context.copyType(context.oldOuterType, {
transformObjectType(typeConfig: GraphQLObjectTypeConfig<any, any>) {
return {
...typeConfig,
name: typeConfig.name + '2'
};
}
});
// This just adds a reflexive field "self" to all types, but its type does not have
// the "self" field (because it is a copy from the original type, see above)
// it also won't have the "cool" rename applied because the top-level transformers are not applied
return {
...fields,
self: {
type: type2,
resolve: (source: any) => source
}
}
}
});
const myTypeRes = transformedSchema.getQueryType()!.getFields()['myField'].type as GraphQLObjectType;
expect(myTypeRes).toBeDefined();
expect(myTypeRes.getFields()['nameButCooler']).toBeDefined();
expect(myTypeRes.getFields()['self']).toBeDefined();
const reflexiveTypeRes = myTypeRes.getFields()['self'].type as GraphQLObjectType;
expect(reflexiveTypeRes.name).toBe('MyType2');
expect(reflexiveTypeRes.getFields()['self']).not.toBeDefined();
expect(reflexiveTypeRes.getFields()['name']).toBeDefined();
});
it('keeps ast nodes', () => {
const schema = buildASTSchema(parse(`schema { query: Query } type Query { echo(in: String): String }`));
expect((schema.getType('Query') as any).getFields()['echo'].astNode).toBeDefined();
const transformed = transformSchema(schema, {
transformField(config) {
if (config.name == 'echo') {
return {
...config,
name: 'echo2'
}
}
return config;
}
});
expect((transformed.getType('Query') as any).getFields()['echo2'].astNode).toBeDefined();
});
it('removes type that were used and now are unused', () => {
const iface = new GraphQLInterfaceType({
name: 'Interface',
fields: {
test: {
type: GraphQLID
}
},
resolveType: () => ''
});
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
hello: {
type: iface
}
}
}),
types: [new GraphQLObjectType({
name: 'Unused',
interfaces: [ iface ],
fields: {
test: {
type: GraphQLID
}
}
})]
});
expect(schema.getTypeMap()['Unused']).toBeDefined(); // sanity check that GraphQL does not remove this
// now remove the only reference to the interface
const transformedSchema = transformSchema(schema, {
transformField(config) {
if (config.name == 'hello') {
return {
...config,
type: GraphQLString
}
}
return config;
}
});
expect(transformedSchema.getTypeMap()['Unused']).toBeUndefined();
});
it('removes unused types without causing collisions with new types of same name', () => {
const myType = new GraphQLObjectType({
name: 'MyType',
fields: {
field: {
type: GraphQLInt
}
}
});
const myTypeReplacement = new GraphQLObjectType({
name: 'MyType',
fields: {
field: {
type: GraphQLInt
}
}
});
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
hello: {
type: myType
}
}
})
});
const transformedSchema = transformSchema(schema, {
transformField(config) {
if (config.name == 'hello') {
return {
...config,
type: myTypeReplacement
}
}
return config;
}
});
expect(transformedSchema.getTypeMap()['MyType']).toBe(myTypeReplacement);
});
it('keeps interface implementations if they are still used indirectly', () => {
const schema = buildASTSchema(gql`
schema {
query: Query
}
interface Interface {
test: ID
}
interface WrappingInterface {
test: Interface
}
type Query {
hello: WrappingInterface
}
type Impl implements Interface {
test: ID
}
`);
const condensedSchema = transformSchema(schema, {
transformFields(config, context) {
if (context.oldOuterType.name == 'Query') {
return {
...config,
newField: {
type: GraphQLInt
}
}
}
return config;
}
});
expect(condensedSchema.getTypeMap()['Impl']).toBeDefined();
});
});