import { All, Controller, Inject, Req, Res } from "@nestjs/common";
import type { Request, Response } from "express";
import type { YogaInstance } from "../graphql/yoga.js";

/**
 * GraphQL Yoga 端点 Controller（对齐 02-architecture-design.md §1.1）。
 *
 * - POST /graphql：执行 GraphQL query / mutation
 * - GET /graphql：开发环境返回 GraphiQL playground（生产关闭）
 *
 * 由 NestJS 路由到 yoga.handleNodeRequestAndResponse，
 * Yoga 内部负责解析 body / 构建 context（含 ParentSession + DataLoaders）/ 执行 schema / 返回结果。
 */
@Controller("graphql")
export class GraphqlController {
  private readonly yoga: YogaInstance;

  constructor(@Inject("YOGA_INSTANCE") yoga: YogaInstance) {
    this.yoga = yoga;
  }

  @All()
  async handle(@Req() req: Request, @Res() res: Response): Promise<void> {
    await this.yoga.handleNodeRequestAndResponse(req, res, { req, res });
  }
}
