UNPKG

fuse

Version:

The magical GraphQL framework

79 lines (76 loc) 2.25 kB
// src/adapters/cloudflare.ts import { createYoga } from "graphql-yoga"; import { builder } from "../builder"; // src/utils/yoga-helpers.ts import { blockFieldSuggestionsPlugin } from "@escape.tech/graphql-armor-block-field-suggestions"; import { useDeferStream } from "@graphql-yoga/plugin-defer-stream"; import { useDisableIntrospection } from "@graphql-yoga/plugin-disable-introspection"; import { createStellateLoggerPlugin } from "stellate/graphql-yoga"; var getYogaPlugins = (stellate) => { return [ useDeferStream(), process.env.NODE_ENV === "production" && useDisableIntrospection(), process.env.NODE_ENV === "production" && blockFieldSuggestionsPlugin(), Boolean(process.env.NODE_ENV === "production" && stellate) && createStellateLoggerPlugin({ serviceName: stellate.serviceName, token: stellate.loggingToken, fetch }) ].filter(Boolean); }; var wrappedContext = (context) => { return async (ct) => { const baseContext = { request: ct.request, headers: ct.request.headers, params: ct.params }; if (typeof context === "function") { const userCtx = context(baseContext); if (userCtx.then) { const result = await userCtx; return { ...baseContext, ...result }; } return { ...baseContext, ...userCtx }; } else if (typeof context === "object") { return { ...baseContext, ...context }; } return baseContext; }; }; // src/adapters/cloudflare.ts function fetch2(request) { let ctx; import.meta.glob("/types/**/*.ts", { eager: true }); const context = import.meta.glob("/_context.ts", { eager: true }); if (context["/_context.ts"]) { const mod = context["/_context.ts"]; if (mod.getContext) { ctx = mod.getContext; } } const completedSchema = builder.toSchema({}); const yoga = createYoga({ graphiql: false, maskedErrors: true, schema: completedSchema, // We allow batching by default batching: true, context: wrappedContext(ctx), plugins: getYogaPlugins() }); return yoga.fetch(request, ctx); } var cloudflare_default = { fetch: fetch2 }; export { cloudflare_default as default };