From 311d575c09162a25b12fea2d438b8215a9f412a9 Mon Sep 17 00:00:00 2001 From: Rushil Perera Date: Sat, 6 Dec 2025 08:26:16 -0500 Subject: [PATCH] feat: Refactor GraphQL context for user data and async creation --- src/graphql/context.ts | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/graphql/context.ts b/src/graphql/context.ts index e30cecb..5204e11 100644 --- a/src/graphql/context.ts +++ b/src/graphql/context.ts @@ -1,22 +1,26 @@ import type { Context as HonoContext } from "hono"; export interface GraphQLContext { - db: D1Database; - env: Env; - deviceId?: string; - aniListToken?: string; - honoContext: HonoContext; + db: D1Database; + deviceId?: string; + aniListToken?: string; + user: { id: number, name: string } | null; + honoContext: HonoContext; } -export function createGraphQLContext(c: HonoContext): GraphQLContext { - const deviceId = c.req.header("X-Device-ID"); - const aniListToken = c.req.header("X-AniList-Token"); +export async function createGraphQLContext(c: HonoContext): Promise { + const deviceId = c.req.header("X-Device-ID"); + const aniListToken = c.req.header("X-AniList-Token"); + const env = c.env as Env; - return { - db: c.env.DB, - env: c.env, - deviceId, - aniListToken, - honoContext: c, - }; + const stub = await env.ANILIST_DO.getByName("GLOBAL"); + const user = await stub.getUser(aniListToken!); + + return { + db: env.DB, + deviceId, + aniListToken, + user, + honoContext: c, + }; }