- Pass FastifyRequest/FastifyReply directly to yoga.handleNodeRequestAndResponse per the official graphql-yoga Fastify integration docs. Yoga v5 uses req.body (already parsed by Fastify) when available, avoiding the dead raw stream issue. - Add proper TypeScript generics for server context including db and redis - Wrap sendVerification/sendPasswordReset in try/catch so missing SMTP does not crash register/requestPasswordReset mutations - Fix migrate.ts path resolution to work with both tsx (src/) and compiled (dist/) - Expose postgres:5432 and redis:6379 ports in compose.yml for local dev Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
38 lines
1021 B
TypeScript
38 lines
1021 B
TypeScript
import type { YogaInitialContext } from "graphql-yoga";
|
|
import type { FastifyRequest, FastifyReply } from "fastify";
|
|
import type { Context } from "./builder";
|
|
import { getSession } from "../lib/auth";
|
|
import { db } from "../db/connection";
|
|
import { redis } from "../lib/auth";
|
|
|
|
type ServerContext = {
|
|
req: FastifyRequest;
|
|
reply: FastifyReply;
|
|
db: typeof db;
|
|
redis: typeof redis;
|
|
};
|
|
|
|
export async function buildContext(ctx: YogaInitialContext & ServerContext): Promise<Context> {
|
|
const request = ctx.request;
|
|
const cookieHeader = request.headers.get("cookie") || "";
|
|
|
|
// Parse session_token from cookies
|
|
const cookies = Object.fromEntries(
|
|
cookieHeader.split(";").map((c) => {
|
|
const [k, ...v] = c.trim().split("=");
|
|
return [k.trim(), v.join("=")];
|
|
}),
|
|
);
|
|
|
|
const token = cookies["session_token"];
|
|
const currentUser = token ? await getSession(token) : null;
|
|
|
|
return {
|
|
db: ctx.db || db,
|
|
redis: ctx.redis || redis,
|
|
currentUser,
|
|
request,
|
|
reply: ctx.reply,
|
|
};
|
|
}
|