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:
@@ -4,6 +4,8 @@ services:
|
|||||||
image: postgres:16-alpine
|
image: postgres:16-alpine
|
||||||
container_name: sexy_postgres
|
container_name: sexy_postgres
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
ports:
|
||||||
|
- "5432:5432"
|
||||||
volumes:
|
volumes:
|
||||||
- postgres_data:/var/lib/postgresql/data
|
- postgres_data:/var/lib/postgresql/data
|
||||||
environment:
|
environment:
|
||||||
@@ -19,6 +21,8 @@ services:
|
|||||||
image: redis:7-alpine
|
image: redis:7-alpine
|
||||||
container_name: sexy_redis
|
container_name: sexy_redis
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
ports:
|
||||||
|
- "6379:6379"
|
||||||
volumes:
|
volumes:
|
||||||
- redis_data:/data
|
- redis_data:/data
|
||||||
command: redis-server --appendonly yes
|
command: redis-server --appendonly yes
|
||||||
|
|||||||
@@ -1,10 +1,18 @@
|
|||||||
import type { YogaInitialContext } from "graphql-yoga";
|
import type { YogaInitialContext } from "graphql-yoga";
|
||||||
|
import type { FastifyRequest, FastifyReply } from "fastify";
|
||||||
import type { Context } from "./builder";
|
import type { Context } from "./builder";
|
||||||
import { getSession } from "../lib/auth";
|
import { getSession } from "../lib/auth";
|
||||||
import { db } from "../db/connection";
|
import { db } from "../db/connection";
|
||||||
import { redis } from "../lib/auth";
|
import { redis } from "../lib/auth";
|
||||||
|
|
||||||
export async function buildContext(ctx: YogaInitialContext & { request: Request; reply: unknown; db: typeof db; redis: typeof redis }): Promise<Context> {
|
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 request = ctx.request;
|
||||||
const cookieHeader = request.headers.get("cookie") || "";
|
const cookieHeader = request.headers.get("cookie") || "";
|
||||||
|
|
||||||
|
|||||||
@@ -129,7 +129,11 @@ builder.mutationField("register", (t) =>
|
|||||||
email_verified: false,
|
email_verified: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
await sendVerification(args.email, verifyToken);
|
await sendVerification(args.email, verifyToken);
|
||||||
|
} catch (e) {
|
||||||
|
console.warn("Failed to send verification email:", (e as Error).message);
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
@@ -184,7 +188,11 @@ builder.mutationField("requestPasswordReset", (t) =>
|
|||||||
.set({ password_reset_token: token, password_reset_expiry: expiry })
|
.set({ password_reset_token: token, password_reset_expiry: expiry })
|
||||||
.where(eq(users.id, user[0].id));
|
.where(eq(users.id, user[0].id));
|
||||||
|
|
||||||
|
try {
|
||||||
await sendPasswordReset(args.email, token);
|
await sendPasswordReset(args.email, token);
|
||||||
|
} catch (e) {
|
||||||
|
console.warn("Failed to send password reset email:", (e as Error).message);
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
import Fastify from "fastify";
|
import Fastify, { FastifyRequest, FastifyReply } from "fastify";
|
||||||
import fastifyCookie from "@fastify/cookie";
|
import fastifyCookie from "@fastify/cookie";
|
||||||
import fastifyCors from "@fastify/cors";
|
import fastifyCors from "@fastify/cors";
|
||||||
import fastifyMultipart from "@fastify/multipart";
|
import fastifyMultipart from "@fastify/multipart";
|
||||||
import fastifyStatic from "@fastify/static";
|
import fastifyStatic from "@fastify/static";
|
||||||
import { createYoga } from "graphql-yoga";
|
import { createYoga } from "graphql-yoga";
|
||||||
import { Readable } from "stream";
|
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import { schema } from "./graphql/index";
|
import { schema } from "./graphql/index";
|
||||||
import { buildContext } from "./graphql/context";
|
import { buildContext } from "./graphql/context";
|
||||||
@@ -44,34 +43,24 @@ async function main() {
|
|||||||
decorateReply: false,
|
decorateReply: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
const yoga = createYoga({
|
const yoga = createYoga<{ req: FastifyRequest; reply: FastifyReply; db: typeof db; redis: typeof redis }>({
|
||||||
schema,
|
schema,
|
||||||
context: buildContext,
|
context: buildContext,
|
||||||
graphqlEndpoint: "/graphql",
|
graphqlEndpoint: "/graphql",
|
||||||
healthCheckEndpoint: "/health",
|
healthCheckEndpoint: "/health",
|
||||||
logging: {
|
logging: {
|
||||||
debug: (msg: string) => fastify.log.debug(msg),
|
debug: (...args) => args.forEach((arg) => fastify.log.debug(arg)),
|
||||||
info: (msg: string) => fastify.log.info(msg),
|
info: (...args) => args.forEach((arg) => fastify.log.info(arg)),
|
||||||
warn: (msg: string) => fastify.log.warn(msg),
|
warn: (...args) => args.forEach((arg) => fastify.log.warn(arg)),
|
||||||
error: (msg: string) => fastify.log.error(msg),
|
error: (...args) => args.forEach((arg) => fastify.log.error(arg)),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
fastify.route({
|
fastify.route({
|
||||||
url: "/graphql",
|
url: "/graphql",
|
||||||
method: ["GET", "POST", "OPTIONS"],
|
method: ["GET", "POST", "OPTIONS"],
|
||||||
handler: async (request, reply) => {
|
handler: (req, reply) =>
|
||||||
const response = await yoga.handleNodeRequestAndResponse(
|
yoga.handleNodeRequestAndResponse(req, reply, { req, reply, db, redis }),
|
||||||
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));
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
fastify.get("/health", async (_request, reply) => {
|
fastify.get("/health", async (_request, reply) => {
|
||||||
|
|||||||
@@ -11,7 +11,11 @@ const db = drizzle(pool);
|
|||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
console.log("Running schema migrations...");
|
console.log("Running schema migrations...");
|
||||||
const migrationsFolder = path.join(__dirname, "../../migrations");
|
// In dev (tsx): __dirname = src/scripts → migrations are at src/migrations
|
||||||
|
// In prod (node dist): __dirname = dist/scripts → migrations are at ../../migrations (package root)
|
||||||
|
const migrationsFolder = __dirname.includes("/src/")
|
||||||
|
? path.join(__dirname, "../migrations")
|
||||||
|
: path.join(__dirname, "../../migrations");
|
||||||
await migrate(db, { migrationsFolder });
|
await migrate(db, { migrationsFolder });
|
||||||
console.log("Schema migrations complete.");
|
console.log("Schema migrations complete.");
|
||||||
await pool.end();
|
await pool.end();
|
||||||
|
|||||||
Reference in New Issue
Block a user