fuse
Version:
The magical GraphQL framework
82 lines (79 loc) • 2.27 kB
JavaScript
// src/adapters/bun.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/bun.ts
async function main() {
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()
});
Bun.serve(
// @ts-ignore this is a typing bug, it works. https://github.com/dotansimha/graphql-yoga/issues/3003
yoga
);
}
main();
export {
main
};