当 Apollo Server 处于生产模式时,如何借助 graphql-codegen 为 React 生成 Graphql 类型?

问题描述

当后端模式设置为 NODE_ENV: development 时一切正常,但在生产模式下 graphql-codegen 失败并出现错误:

本地网络服务器错误:

Apollo Server 不允许 GraphQL 自省,但查询 包含 _schema 或 _type。要启用内省,请通过 内省:生产中的 ApolloServer 是真实的

生产网络服务器错误:

无法从 https://example.com/graphql 加载架构,原因:无法 验证第一个证书。 GraphQL 代码生成器支持:

  • ES 模块和 CommonJS 导出(默认导出或命名导出“模式”)
  • 内省 JSON 文件
  • GraphQL 端点的 URL
  • 具有类型定义(全局表达式)的多个文件
  • 配置文件中的字符串

前端 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/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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)