You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NextJS中GraphQL+Apollo Server无法找到context中的Prisma Client

解决方案

问题根源

TypeGraphQL的Prisma生成器已配置contextPrismaKey = "customPrisma",要求GraphQL Context必须通过customPrisma键提供Prisma Client实例,但你的Apollo Server Context返回的是{ p: prisma },键名不匹配导致系统找不到所需的Prisma实例。

修复步骤

  1. 修改Apollo Server的Context配置,将Prisma Client的键名改为customPrisma:

    // 示例:NextJS的API路由文件(如pages/api/graphql.ts)
    import { ApolloServer } from '@apollo/server';
    import { startServerAndCreateNextHandler } from '@as-integrations/next';
    import { prisma } from '../../prisma/client';
    import { schema } from '../../generated/type-graphql';
    
    const server = new ApolloServer({ schema });
    
    export default startServerAndCreateNextHandler(server, {
      context: async () => ({
        customPrisma: prisma, // 替换原来的`p`为`customPrisma`
      }),
    });
    
  2. 重启开发服务器,确保代码变更生效。

  3. 重新测试数据提交的mutation,确认错误是否解决。

注意事项

如果之后需要调整Context中的键名,必须同步修改Prisma Schema里的contextPrismaKey配置,确保两者完全一致:

generator typegraphql {
  provider           = "typegraphql-prisma"
  contextPrismaKey   = "customPrisma" // 与Apollo Context的键名保持统一
}

内容的提问来源于stack exchange,提问作者Felipe Olaya Ospina

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 04:01:13