feat: Refactor GraphQL context for user data and async creation

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

View File

@@ -2,21 +2,25 @@ import type { Context as HonoContext } from "hono";
export interface GraphQLContext { export interface GraphQLContext {
db: D1Database; db: D1Database;
env: Env;
deviceId?: string; deviceId?: string;
aniListToken?: string; aniListToken?: string;
user: { id: number, name: string } | null;
honoContext: HonoContext; honoContext: HonoContext;
} }
export function createGraphQLContext(c: HonoContext): GraphQLContext { export async function createGraphQLContext(c: HonoContext<Env>): Promise<GraphQLContext> {
const deviceId = c.req.header("X-Device-ID"); const deviceId = c.req.header("X-Device-ID");
const aniListToken = c.req.header("X-AniList-Token"); const aniListToken = c.req.header("X-AniList-Token");
const env = c.env as Env;
const stub = await env.ANILIST_DO.getByName("GLOBAL");
const user = await stub.getUser(aniListToken!);
return { return {
db: c.env.DB, db: env.DB,
env: c.env,
deviceId, deviceId,
aniListToken, aniListToken,
user,
honoContext: c, honoContext: c,
}; };
} }