Apollo Client:codegen未生成GraphQL Mutation输入类型问题求助
解决Apollo Client Codegen未生成Mutation输入类型的问题
我之前碰到过一模一样的情况,咱们一步步排查解决:
1. 确保Codegen拉取到完整的远程Schema
你的命令直接通过--endpoint生成类型,但有时候Apollo CLI可能没有完整拉取到远程Schema的所有定义(包括输入类型)。建议先单独下载Schema,再基于本地Schema生成类型:
第一步:添加下载Schema的脚本
在package.json里新增一个脚本,把远程Schema下载到本地:
"download-schema:auth-api": "apollo client:download-schema --endpoint=\"https://frontend-engineer-onboarding-api-thxaa.ondigitalocean.app/graphql\" src/graphql/auth-api/schema.graphql"
第二步:修改Codegen命令指向本地Schema
把原来的codegen:auth-api脚本改成基于本地Schema生成:
"codegen:auth-api": "apollo client:codegen --target typescript --includes=\"./src/graphql/auth-api/**/*.ts\" --outputFlat --schema src/graphql/auth-api/schema.graphql \"src/types\""
先执行npm run download-schema:auth-api,再运行codegen命令,这样能确保Codegen基于完整的Schema生成类型。
2. 检查Apollo CLI版本
旧版本的Apollo CLI可能存在输入类型生成的bug,建议更新到最新稳定版:
npm install @apollo/client@latest apollo@latest
3. 尝试移除--outputFlat参数
--outputFlat会将所有类型合并到同一目录,但有时候会干扰全局类型的生成逻辑。可以先暂时移除这个参数,看看globalTypes.ts是否能正确生成SignUpInput,之后再根据需求调整输出结构。
4. 使用配置文件替代命令行参数
如果上述方法都无效,建议创建apollo.config.js配置文件,让配置更清晰可控:
module.exports = { client: { service: { name: 'auth-api', url: 'https://frontend-engineer-onboarding-api-thxaa.ondigitalocean.app/graphql', }, includes: ['./src/graphql/auth-api/**/*.ts'], target: 'typescript', outputFlat: true, outputPath: 'src/types/', }, };
然后把package.json里的脚本简化为:
"codegen:auth-api": "apollo client:codegen"
关键排查点
你可以先通过apollo client:download-schema下载的schema.graphql文件,确认里面是否包含SignUpInput的定义。如果文件里没有这个类型,那问题出在远程API的Schema暴露上;如果有,那就是Codegen的配置或版本问题。
内容的提问来源于stack exchange,提问作者JP Calvo
相关产品推荐
相关产品推荐

