(
schema: GraphQLSchema,
options: PruneSchemaOptions = {},
)
| 23 | * @param options Additional options for removing unused types from the schema |
| 24 | */ |
| 25 | export function pruneSchema( |
| 26 | schema: GraphQLSchema, |
| 27 | options: PruneSchemaOptions = {}, |
| 28 | ): GraphQLSchema { |
| 29 | const { |
| 30 | skipEmptyCompositeTypePruning, |
| 31 | skipEmptyUnionPruning, |
| 32 | skipPruning, |
| 33 | skipUnimplementedInterfacesPruning, |
| 34 | skipUnusedTypesPruning, |
| 35 | } = options; |
| 36 | let prunedTypes: string[] = []; // Pruned types during mapping |
| 37 | let prunedSchema: GraphQLSchema = schema; |
| 38 | |
| 39 | do { |
| 40 | let visited = visitSchema(prunedSchema); |
| 41 | |
| 42 | // Custom pruning was defined, so we need to pre-emptively revisit the schema accounting for this |
| 43 | if (skipPruning) { |
| 44 | const revisit: string[] = []; |
| 45 | |
| 46 | for (const typeName in prunedSchema.getTypeMap()) { |
| 47 | if (typeName.startsWith('__')) { |
| 48 | continue; |
| 49 | } |
| 50 | |
| 51 | const type = prunedSchema.getType(typeName); |
| 52 | |
| 53 | // if we want to skip pruning for this type, add it to the list of types to revisit |
| 54 | if (type && skipPruning(type)) { |
| 55 | revisit.push(typeName); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | visited = visitQueue(revisit, prunedSchema, visited); // visit again |
| 60 | } |
| 61 | |
| 62 | prunedTypes = []; |
| 63 | |
| 64 | prunedSchema = mapSchema(prunedSchema, { |
| 65 | [MapperKind.TYPE]: type => { |
| 66 | if (!visited.has(type.name) && !isSpecifiedScalarType(type)) { |
| 67 | if ( |
| 68 | isUnionType(type) || |
| 69 | isInputObjectType(type) || |
| 70 | isInterfaceType(type) || |
| 71 | isObjectType(type) || |
| 72 | isScalarType(type) |
| 73 | ) { |
| 74 | // skipUnusedTypesPruning: skip pruning unused types |
| 75 | if (skipUnusedTypesPruning) { |
| 76 | return type; |
| 77 | } |
| 78 | // skipEmptyUnionPruning: skip pruning empty unions |
| 79 | if ( |
| 80 | isUnionType(type) && |
| 81 | skipEmptyUnionPruning && |
| 82 | !Object.keys(type.getTypes()).length |
no test coverage detected
searching dependent graphs…