UNPKG

graphql-yoga

Version:
82 lines (81 loc) • 3.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useSchema = void 0; const promise_helpers_1 = require("@whatwg-node/promise-helpers"); function isGraphQLSchema(schemaDef) { // @ts-expect-error - Symbol.toStringTag exists return schemaDef?.[Symbol.toStringTag] === 'GraphQLSchema'; } const useSchema = (schemaDef) => { if (schemaDef == null) { return {}; } if (isGraphQLSchema(schemaDef)) { return { onPluginInit({ setSchema }) { setSchema(schemaDef); }, }; } if ('then' in schemaDef) { let schema; return { onRequestParse() { return { onRequestParseDone() { if (!schema) { return (0, promise_helpers_1.handleMaybePromise)(() => schemaDef, schemaDef => { schema = schemaDef; }); } }, }; }, onEnveloped({ setSchema }) { if (schema == null) { throw new Error(`You provide a promise of a schema but it hasn't been resolved yet. Make sure you use this plugin with GraphQL Yoga.`); } if (!isGraphQLSchema(schema)) { throw new Error(`The resolved schema is not a valid GraphQLSchema instance.`); } setSchema(schema); }, }; } if (typeof schemaDef === 'function') { const schemaByRequest = new WeakMap(); return { onRequestParse({ request, serverContext }) { return { onRequestParseDone() { return (0, promise_helpers_1.handleMaybePromise)(() => schemaDef({ ...serverContext, request, }), schemaDef => { if (!isGraphQLSchema(schemaDef)) { throw new Error('The factory function did not return a valid GraphQLSchema.'); } schemaByRequest.set(request, schemaDef); }); }, }; }, onEnveloped({ setSchema, context }) { if (context?.request == null) { throw new Error('Request object is not available in the context. Make sure you use this plugin with GraphQL Yoga, and pass a Fetch API Request as `request` when you call getEnveloped yourself (e.g. for graphql-ws).'); } // a request that skipped onRequestParse (e.g. one built for graphql-ws) gets its schema here const schema = schemaByRequest.get(context.request) ?? schemaDef(context); if (!isGraphQLSchema(schema)) { if ((0, promise_helpers_1.isPromise)(schema)) { schema.then(undefined, () => { }); } throw new Error('No schema found for this request. When getEnveloped is called outside of the request pipeline (e.g. by graphql-ws), the factory function must return a GraphQLSchema synchronously.'); } setSchema(schema); }, }; } throw new Error(`Invalid schema definition provided, expected a schema, promise or function.`); }; exports.useSchema = useSchema;