UNPKG

fuse

Version:

The magical GraphQL framework

98 lines (95 loc) 2.75 kB
// src/adapters/lambda.ts import { createYoga } from "graphql-yoga"; import { builder } from "fuse"; // 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/lambda.ts async function fetch2(event, lambdaContext) { 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() }); const response = await yoga.fetch( event.path + "?" + new URLSearchParams( event.queryStringParameters || {} ).toString(), { method: event.httpMethod, headers: event.headers, body: event.body ? Buffer.from(event.body, event.isBase64Encoded ? "base64" : "utf8") : void 0 }, { event, lambdaContext } ); const responseHeaders = Object.fromEntries(response.headers.entries()); return { statusCode: response.status, headers: responseHeaders, body: await response.text(), isBase64Encoded: false }; } export { fetch2 as fetch };