/
prune.test.ts
408 lines (337 loc) · 9.67 KB
/
prune.test.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import { buildSchema, GraphQLNamedType } from 'graphql';
import { pruneSchema } from '../src/prune';
import { PruneSchemaFilter } from '../src';
describe('pruneSchema', () => {
test('can handle recursive input types', () => {
const schema = buildSchema(/* GraphQL */ `
input Input {
moreInput: Input
}
type Query {
someQuery(input: Input): Boolean
}
`);
pruneSchema(schema);
});
test('removes unused enums', () => {
const schema = buildSchema(/* GraphQL */ `
enum Unused {
VALUE
}
type Query {
foo: Boolean
}
`);
const result = pruneSchema(schema);
expect(result.getType('Unused')).toBeUndefined();
});
test('removes unused objects', () => {
const schema = buildSchema(/* GraphQL */ `
type Unused {
value: String
}
type Query {
foo: Boolean
}
`);
const result = pruneSchema(schema);
expect(result.getType('Unused')).toBeUndefined();
});
test('does not remove unused objects when skipUnusedTypesPruning is true', () => {
const schema = buildSchema(/* GraphQL */ `
type Unused {
value: String
}
type Query {
foo: Boolean
}
`);
const result = pruneSchema(schema, {
skipUnusedTypesPruning: true,
});
expect(result.getType('Unused')).toBeDefined();
});
test('removes unused custom scalar', () => {
const schema = buildSchema(/* GraphQL */ `
scalar CustomScalar
type Query {
foo: Boolean
}
`);
const result = pruneSchema(schema);
expect(result.getType('CustomScalar')).toBeUndefined();
});
test('does not remove unused custom scalar when skipUnusedTypesPruning set to true', () => {
const schema = buildSchema(/* GraphQL */ `
scalar CustomScalar
type Query {
foo: Boolean
}
`);
const result = pruneSchema(schema, {
skipUnusedTypesPruning: true,
});
expect(result.getType('CustomScalar')).toBeDefined();
});
test('does not remove used input objects', () => {
const schema = buildSchema(/* GraphQL */ `
input UsedInput {
value: String
}
type Query {
foo(input: UsedInput): Boolean
}
`);
const result = pruneSchema(schema);
expect(result.getType('UsedInput')).toBeDefined();
});
test('removes unused input objects', () => {
const schema = buildSchema(/* GraphQL */ `
input Unused {
value: String
}
type Query {
foo: Boolean
}
`);
const result = pruneSchema(schema);
expect(result.getType('Unused')).toBeUndefined();
});
test('does not remove unused input objects when skipUnusedTypesPruning is true', () => {
const schema = buildSchema(/* GraphQL */ `
input Unused {
value: String
}
type Query {
foo: Boolean
}
`);
const result = pruneSchema(schema, {
skipUnusedTypesPruning: true,
});
expect(result.getType('Unused')).toBeDefined();
});
test('removes unused unions', () => {
const schema = buildSchema(/* GraphQL */ `
union Unused
type Query {
foo: Boolean
}
`);
const result = pruneSchema(schema);
expect(result.getType('Unused')).toBeUndefined();
});
test('does not remove unused unions when skipEmptyUnionPruning is true', () => {
const schema = buildSchema(/* GraphQL */ `
union Unused
type Query {
foo: Boolean
}
`);
const result = pruneSchema(schema, {
skipEmptyUnionPruning: true,
skipUnusedTypesPruning: true,
});
expect(result.getType('Unused')).toBeDefined();
});
test('removes unused interfaces', () => {
const schema = buildSchema(/* GraphQL */ `
interface Unused {
value: String
}
type Query {
foo: Boolean
}
`);
const result = pruneSchema(schema);
expect(result.getType('Unused')).toBeUndefined();
});
test('does not remove unused interfaces when skipUnimplementedInterfacesPruning is true', () => {
const schema = buildSchema(/* GraphQL */ `
interface Unused {
value: String
}
type Query {
foo: Boolean
}
`);
const result = pruneSchema(schema, {
skipUnimplementedInterfacesPruning: true,
skipUnusedTypesPruning: true,
});
expect(result.getType('Unused')).toBeDefined();
});
test('removes unused implementations of interfaces', () => {
const schema = buildSchema(/* GraphQL */ `
type Query {
operation: SomeType
}
interface SomeInterface {
field: String
}
type SomeType implements SomeInterface {
field: String
}
type ShouldPrune implements SomeInterface {
field: String
}
`);
const result = pruneSchema(schema);
expect(result.getType('ShouldPrune')).toBeUndefined();
});
test('does not remove implementations of interfaces used as return', () => {
const schema = buildSchema(/* GraphQL */ `
type Query {
operation: SomeType
}
type SomeType {
# SomeInterface is inline and should have all its implementations kept
field: SomeInterface
}
interface SomeInterface {
field: String
}
type KeepMe implements SomeInterface {
field: String
}
`);
const result = pruneSchema(schema);
expect(result.getType('KeepMe')).toBeDefined();
});
test('does not remove interfaces despite first pass', () => {
const schema = buildSchema(/* GraphQL */ `
type Query {
operation: SomeType
}
type SomeType {
# This will be processed last
afield: AsReturnType
# This will be processed first
bField: AsInterfaceType
}
# SomeInterface is declared as an interface so it should be not a return type but still visited
type AsInterfaceType implements SomeInterface {
field: String
}
# SomeInterface is a return type and should have all its implementations kept
type AsReturnType {
field: SomeInterface
}
interface SomeInterface {
field: String
}
type KeepMe implements SomeInterface {
field: String
}
`);
const result = pruneSchema(schema);
expect(result.getType('KeepMe')).toBeDefined();
});
test('removes top level objects with no fields', () => {
const schema = buildSchema(/* GraphQL */ `
type Query {
foo: Boolean
}
type Mutation
`);
const result = pruneSchema(schema);
expect(result.getMutationType()).toBeUndefined();
});
test('does not removes objects with no fields when skipEmptyCompositeTypePruning is true', () => {
const schema = buildSchema(/* GraphQL */ `
type Query {
foo: Boolean
}
type Foo
`);
const result = pruneSchema(schema, {
skipUnusedTypesPruning: true,
skipEmptyCompositeTypePruning: true,
});
expect(result.getType('Foo')).toBeDefined();
});
test('removes unused interfaces when implementations are unused', () => {
const schema = buildSchema(/* GraphQL */ `
interface UnusedInterface {
value: String
}
type UnusedType implements UnusedInterface {
value: String
}
type Query {
foo: Boolean
}
`);
const result = pruneSchema(schema);
expect(result.getType('UnusedType')).toBeUndefined();
expect(result.getType('UnusedInterface')).toBeUndefined();
});
test('removes unused unions when implementations are unused', () => {
const schema = buildSchema(/* GraphQL */ `
union UnusedUnion = UnusedType
type UnusedType {
value: String
}
type Query {
foo: Boolean
}
`);
const result = pruneSchema(schema);
expect(result.getType('UnusedType')).toBeUndefined();
expect(result.getType('UnusedUnion')).toBeUndefined();
});
test('does not throw on pruning unimplemented interfaces', () => {
const schema = buildSchema(/* GraphQL */ `
interface UnimplementedInterface {
value: String
}
type Query {
foo: UnimplementedInterface
}
`);
const result = pruneSchema(schema);
expect(result.getType('UnimplementedInterface')).toBeDefined();
});
test('does not prune types that match the filter', () => {
const schema = buildSchema(/* GraphQL */ `
directive @bar on OBJECT
type CustomType @bar {
value: String
}
type Query {
foo: Boolean
}
`);
// Do not prune any type that has the @bar directive
const doNotPruneTypeWithBar: PruneSchemaFilter = (type: GraphQLNamedType) => {
return !!type.astNode?.directives?.find(it => it.name.value === 'bar');
};
const result = pruneSchema(schema, {
skipPruning: doNotPruneTypeWithBar,
});
expect(result.getType('CustomType')).toBeDefined();
});
test('does not prune types or its leaves that match the filter', () => {
const schema = buildSchema(/* GraphQL */ `
directive @bar on OBJECT
type SomeInterface {
value: String
}
type CustomType implements SomeInterface @bar {
value: String
}
type Query {
foo: Boolean
}
`);
// Do not prune any type that has the @bar directive
const doNotPruneTypeWithBar: PruneSchemaFilter = (type: GraphQLNamedType) => {
return !!type.astNode?.directives?.find(it => it.name.value === 'bar');
};
const result = pruneSchema(schema, {
skipPruning: doNotPruneTypeWithBar,
});
expect(result.getType('CustomType')).toBeDefined();
expect(result.getType('SomeInterface')).toBeDefined();
});
});