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, + }; }