UNPKG

@graphql-tools/utils

Version:

Common package containing utils and types for GraphQL tools

72 lines (71 loc) 3.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getResolversFromSchema = getResolversFromSchema; const graphql_1 = require("graphql"); function getResolversFromSchema(schema, // Include default merged resolvers includeDefaultMergedResolver) { const resolvers = Object.create(null); const typeMap = schema.getTypeMap(); for (const typeName in typeMap) { if (!typeName.startsWith('__')) { const type = typeMap[typeName]; if ((0, graphql_1.isScalarType)(type)) { if (!(0, graphql_1.isSpecifiedScalarType)(type)) { const config = type.toConfig(); delete config.astNode; // avoid AST duplication elsewhere resolvers[typeName] = new graphql_1.GraphQLScalarType(config); } } else if ((0, graphql_1.isEnumType)(type)) { resolvers[typeName] = {}; const values = type.getValues(); for (const value of values) { resolvers[typeName][value.name] = value.value; } } else if ((0, graphql_1.isInterfaceType)(type)) { if (type.resolveType != null) { resolvers[typeName] = { __resolveType: type.resolveType, }; } } else if ((0, graphql_1.isUnionType)(type)) { if (type.resolveType != null) { resolvers[typeName] = { __resolveType: type.resolveType, }; } } else if ((0, graphql_1.isObjectType)(type)) { resolvers[typeName] = {}; if (type.isTypeOf != null) { resolvers[typeName].__isTypeOf = type.isTypeOf; } const fields = type.getFields(); for (const fieldName in fields) { const field = fields[fieldName]; if (field.subscribe != null) { resolvers[typeName][fieldName] = resolvers[typeName][fieldName] || {}; resolvers[typeName][fieldName].subscribe = field.subscribe; } if (field.resolve != null && field.resolve?.name !== 'defaultFieldResolver') { switch (field.resolve?.name) { case 'defaultMergedResolver': if (!includeDefaultMergedResolver) { continue; } break; case 'defaultFieldResolver': continue; } resolvers[typeName][fieldName] = resolvers[typeName][fieldName] || {}; resolvers[typeName][fieldName].resolve = field.resolve; } } } } } return resolvers; }