UNPKG

@graphql-mesh/serve-runtime

Version:
45 lines (44 loc) 1.81 kB
/* eslint-disable import/no-extraneous-dependencies */ import { buildASTSchema, buildSchema, isSchema } from 'graphql'; import { defaultImportFn, isUrl, readFileOrUrl } from '@graphql-mesh/utils'; import { isDocumentNode, isPromise, isValidPath } from '@graphql-tools/utils'; export function handleUnifiedGraphConfig(config, configContext) { const config$ = typeof config === 'function' ? config() : config; if (isPromise(config$)) { return config$.then(schema => handleUnifiedGraphSchema(schema, configContext)); } return handleUnifiedGraphSchema(config$, configContext); } export function handleUnifiedGraphSchema(schema, configContext) { if (isSchema(schema)) { return schema; } if (isDocumentNode(schema)) { return buildASTSchema(schema, { assumeValid: true, assumeValidSDL: true, }); } if (typeof schema === 'string') { if (isValidPath(schema) || isUrl(schema)) { return readFileOrUrl(schema, { fetch: configContext.fetch, cwd: configContext.cwd, logger: configContext.logger, allowUnknownExtensions: true, importFn: defaultImportFn, }).then(sdl => handleUnifiedGraphSchema(sdl, configContext)); } try { return 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.`); }