feat: Integrate asynchronous GraphQL context in Hono handler

This commit is contained in:
2025-12-06 08:26:16 -05:00
parent 311d575c09
commit a2702db794

View File

@@ -6,36 +6,36 @@ import { resolvers } from "./resolvers";
import { typeDefs } from "./schema"; import { typeDefs } from "./schema";
const schema = createSchema({ const schema = createSchema({
typeDefs, typeDefs,
resolvers, resolvers,
}); });
const yoga = createYoga({ const yoga = createYoga({
schema, schema,
graphqlEndpoint: "/graphql", graphqlEndpoint: "/graphql",
landingPage: false, // Disable landing page for production landingPage: false, // Disable landing page for production
graphiql: { graphiql: {
title: "Aniplay GraphQL API", title: "Aniplay GraphQL API",
}, },
context: ({ request }) => { context: ({ request }) => {
// Extract Hono context from the request // Extract Hono context from the request
// graphql-yoga passes the raw request, but we need Hono context // graphql-yoga passes the raw request, but we need Hono context
// This will be provided when we integrate with Hono // This will be provided when we integrate with Hono
return request as any; return request as any;
}, },
}); });
const app = new Hono<Cloudflare.Env>(); const app = new Hono<Cloudflare.Env>();
app.all("/", async (c) => { app.all("/", async (c) => {
const graphqlContext = createGraphQLContext(c); const graphqlContext = await createGraphQLContext(c);
// Create a custom request object that includes our GraphQL context // Create a custom request object that includes our GraphQL context
const request = c.req.raw.clone(); const request = c.req.raw.clone();
(request as any).graphqlContext = graphqlContext; (request as any).graphqlContext = graphqlContext;
const response = await yoga.fetch(request, graphqlContext); const response = await yoga.fetch(request, graphqlContext);
return response; return response;
}); });
export default app; export default app;