UNPKG

4.05 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.filterSchema = void 0;
4const graphql_1 = require("graphql");
5const Interfaces_js_1 = require("./Interfaces.js");
6const mapSchema_js_1 = require("./mapSchema.js");
7function filterSchema({ schema, typeFilter = () => true, fieldFilter = undefined, rootFieldFilter = undefined, objectFieldFilter = undefined, interfaceFieldFilter = undefined, inputObjectFieldFilter = undefined, argumentFilter = undefined, directiveFilter = undefined, enumValueFilter = undefined, }) {
8 const filteredSchema = (0, mapSchema_js_1.mapSchema)(schema, {
9 [Interfaces_js_1.MapperKind.QUERY]: (type) => filterRootFields(type, 'Query', rootFieldFilter, argumentFilter),
10 [Interfaces_js_1.MapperKind.MUTATION]: (type) => filterRootFields(type, 'Mutation', rootFieldFilter, argumentFilter),
11 [Interfaces_js_1.MapperKind.SUBSCRIPTION]: (type) => filterRootFields(type, 'Subscription', rootFieldFilter, argumentFilter),
12 [Interfaces_js_1.MapperKind.OBJECT_TYPE]: (type) => typeFilter(type.name, type)
13 ? filterElementFields(graphql_1.GraphQLObjectType, type, objectFieldFilter || fieldFilter, argumentFilter)
14 : null,
15 [Interfaces_js_1.MapperKind.INTERFACE_TYPE]: (type) => typeFilter(type.name, type)
16 ? filterElementFields(graphql_1.GraphQLInterfaceType, type, interfaceFieldFilter || fieldFilter, argumentFilter)
17 : null,
18 [Interfaces_js_1.MapperKind.INPUT_OBJECT_TYPE]: (type) => typeFilter(type.name, type)
19 ? filterElementFields(graphql_1.GraphQLInputObjectType, type, inputObjectFieldFilter || fieldFilter)
20 : null,
21 [Interfaces_js_1.MapperKind.UNION_TYPE]: (type) => typeFilter(type.name, type) ? undefined : null,
22 [Interfaces_js_1.MapperKind.ENUM_TYPE]: (type) => typeFilter(type.name, type) ? undefined : null,
23 [Interfaces_js_1.MapperKind.SCALAR_TYPE]: (type) => typeFilter(type.name, type) ? undefined : null,
24 [Interfaces_js_1.MapperKind.DIRECTIVE]: directive => directiveFilter && !directiveFilter(directive.name, directive) ? null : undefined,
25 [Interfaces_js_1.MapperKind.ENUM_VALUE]: (valueConfig, typeName, _schema, externalValue) => enumValueFilter && !enumValueFilter(typeName, externalValue, valueConfig) ? null : undefined,
26 });
27 return filteredSchema;
28}
29exports.filterSchema = filterSchema;
30function filterRootFields(type, operation, rootFieldFilter, argumentFilter) {
31 if (rootFieldFilter || argumentFilter) {
32 const config = type.toConfig();
33 for (const fieldName in config.fields) {
34 const field = config.fields[fieldName];
35 if (rootFieldFilter && !rootFieldFilter(operation, fieldName, config.fields[fieldName])) {
36 delete config.fields[fieldName];
37 }
38 else if (argumentFilter && field.args) {
39 for (const argName in field.args) {
40 if (!argumentFilter(type.name, fieldName, argName, field.args[argName])) {
41 delete field.args[argName];
42 }
43 }
44 }
45 }
46 return new graphql_1.GraphQLObjectType(config);
47 }
48 return type;
49}
50function filterElementFields(ElementConstructor, type, fieldFilter, argumentFilter) {
51 if (fieldFilter || argumentFilter) {
52 const config = type.toConfig();
53 for (const fieldName in config.fields) {
54 const field = config.fields[fieldName];
55 if (fieldFilter && !fieldFilter(type.name, fieldName, config.fields[fieldName])) {
56 delete config.fields[fieldName];
57 }
58 else if (argumentFilter && 'args' in field) {
59 for (const argName in field.args) {
60 if (!argumentFilter(type.name, fieldName, argName, field.args[argName])) {
61 delete field.args[argName];
62 }
63 }
64 }
65 }
66 return new ElementConstructor(config);
67 }
68}