当后端模式设置为NODE_ENV: development
all 时完美运行,但在生产模式下 graphql-codegen 失败并出现错误:
本地网络服务器错误:
Apollo Server 不允许 GraphQL 自省,但查询包含 _schema 或 _type。要启用自省,请将 introspection: true 传递给生产环境中的 ApolloServer
生产网络服务器错误:
无法从https://example.com/graphql加载架构,原因:无法验证第一个证书。GraphQL 代码生成器支持:
- ES 模块和 CommonJS 导出(默认导出或命名导出“模式”)
- 自省 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/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