Apollo Server生产模式下用graphql-codegen为React生成GraphQL类型的方法
生产模式下graphql-codegen执行报错场景
后端服务NODE_ENV设为development开发模式时,所有功能可正常运行;切换至生产模式后,执行graphql-codegen会触发两类报错:
- 本地运行报错:Apollo Server生产模式默认禁止GraphQL内省操作,拦截了包含
__schema或__type字段的请求,提示需在初始化ApolloServer时传入introspection: true配置,才能在生产环境启用内省能力 - 生产端点访问报错:从GraphQL端点
https://example.com/graphql加载Schema失败,根因为证书链校验不通过;报错提示同时列明graphql-codegen支持的Schema来源类型:ES模块/CommonJS模块导出的schema对象、内省JSON文件、GraphQL端点URL、支持glob表达式的多类型定义文件、配置文件内嵌字符串
项目现有配置明细
- 前端
codegen.yml配置内容:
schema: ${REACT_APP_GRAPHQL_URL} documents: - './src/GraphQL/queries/query.ts' - './src/GraphQL/mutations/mutation.ts' overwrite: true generates: ./src/generated/graphql.tsx: plugins: - typescript - typescript-operations - typescript-react-apollo config: skipTypename: false withHooks: true withHOC: false withComponent: false
- 前端graphql-codegen相关开发依赖版本:
{ "@graphql-codegen/cli": "^1.20.1", "@graphql-codegen/typescript": "^1.20.2", "@graphql-codegen/typescript-operations": "^1.17.14", "@graphql-codegen/typescript-react-apollo": "^2.2.1", }
- npm执行脚本配置:
{ "generate": "graphql-codegen -r dotenv/config --watch --config codegen.yml", "prebuild": "graphql-codegen -r dotenv/config --config codegen.yml" }
- 版本控制规则:已将
./src/generated/目录加入.gitignore忽略列表
内容的提问来源于stack exchange,提问作者Eugene Zalivadnyi
相关产品推荐
相关产品推荐

