UNPKG

@graphql-mesh/serve-runtime

Version:
50 lines (49 loc) 2.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.handleUnifiedGraphSchema = exports.handleUnifiedGraphConfig = void 0; /* eslint-disable import/no-extraneous-dependencies */ const graphql_1 = require("graphql"); const utils_1 = require("@graphql-mesh/utils"); const utils_2 = require("@graphql-tools/utils"); function handleUnifiedGraphConfig(config, configContext) { const config$ = typeof config === 'function' ? config() : config; if ((0, utils_2.isPromise)(config$)) { return config$.then(schema => handleUnifiedGraphSchema(schema, configContext)); } return handleUnifiedGraphSchema(config$, configContext); } exports.handleUnifiedGraphConfig = handleUnifiedGraphConfig; function handleUnifiedGraphSchema(schema, configContext) { if ((0, graphql_1.isSchema)(schema)) { return schema; } if ((0, utils_2.isDocumentNode)(schema)) { return (0, graphql_1.buildASTSchema)(schema, { assumeValid: true, assumeValidSDL: true, }); } if (typeof schema === 'string') { if ((0, utils_2.isValidPath)(schema) || (0, utils_1.isUrl)(schema)) { return (0, utils_1.readFileOrUrl)(schema, { fetch: configContext.fetch, cwd: configContext.cwd, logger: configContext.logger, allowUnknownExtensions: true, importFn: utils_1.defaultImportFn, }).then(sdl => handleUnifiedGraphSchema(sdl, configContext)); } try { return (0, graphql_1.buildSchema)(schema, { assumeValid: true, assumeValidSDL: true, }); } catch (e) { configContext.logger.error(`Failed to build UnifiedGraphSchema from "${schema}"`); throw e; } } throw new Error(`Invalid UnifiedGraphSchema "${schema}". It can be an SDL string, an instance of GraphQLSchema or DocumentNode, or a function that returns/resolves any of these.`); } exports.handleUnifiedGraphSchema = handleUnifiedGraphSchema;