fix: resolve GraphQL request hang in Fastify integration
- 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>
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
import Fastify from "fastify";
|
||||
import Fastify, { FastifyRequest, FastifyReply } from "fastify";
|
||||
import fastifyCookie from "@fastify/cookie";
|
||||
import fastifyCors from "@fastify/cors";
|
||||
import fastifyMultipart from "@fastify/multipart";
|
||||
import fastifyStatic from "@fastify/static";
|
||||
import { createYoga } from "graphql-yoga";
|
||||
import { Readable } from "stream";
|
||||
import path from "path";
|
||||
import { schema } from "./graphql/index";
|
||||
import { buildContext } from "./graphql/context";
|
||||
@@ -44,34 +43,24 @@ async function main() {
|
||||
decorateReply: false,
|
||||
});
|
||||
|
||||
const yoga = createYoga({
|
||||
const yoga = createYoga<{ req: FastifyRequest; reply: FastifyReply; db: typeof db; redis: typeof redis }>({
|
||||
schema,
|
||||
context: buildContext,
|
||||
graphqlEndpoint: "/graphql",
|
||||
healthCheckEndpoint: "/health",
|
||||
logging: {
|
||||
debug: (msg: string) => fastify.log.debug(msg),
|
||||
info: (msg: string) => fastify.log.info(msg),
|
||||
warn: (msg: string) => fastify.log.warn(msg),
|
||||
error: (msg: string) => fastify.log.error(msg),
|
||||
debug: (...args) => args.forEach((arg) => fastify.log.debug(arg)),
|
||||
info: (...args) => args.forEach((arg) => fastify.log.info(arg)),
|
||||
warn: (...args) => args.forEach((arg) => fastify.log.warn(arg)),
|
||||
error: (...args) => args.forEach((arg) => fastify.log.error(arg)),
|
||||
},
|
||||
});
|
||||
|
||||
fastify.route({
|
||||
url: "/graphql",
|
||||
method: ["GET", "POST", "OPTIONS"],
|
||||
handler: async (request, reply) => {
|
||||
const response = await yoga.handleNodeRequestAndResponse(
|
||||
request.raw,
|
||||
reply.raw,
|
||||
{ reply, db, redis },
|
||||
);
|
||||
reply.status(response.status);
|
||||
for (const [key, value] of response.headers.entries()) {
|
||||
reply.header(key, value);
|
||||
}
|
||||
return reply.send(Readable.from(response.body));
|
||||
},
|
||||
handler: (req, reply) =>
|
||||
yoga.handleNodeRequestAndResponse(req, reply, { req, reply, db, redis }),
|
||||
});
|
||||
|
||||
fastify.get("/health", async (_request, reply) => {
|
||||
|
||||
Reference in New Issue
Block a user