[英]How to generate Graphql types for React with help of graphql-codegen when Apollo Server is in production mode?
When backend mode set to NODE_ENV: development
all works perfectly, but in production mode graphql-codegen fails with error:当后端模式设置为
NODE_ENV: development
一切正常,但在生产模式下 graphql-codegen 失败并出现错误:
Local web-server error:本地网络服务器错误:
GraphQL introspection is not allowed by Apollo Server, but the query contained _schema or _type.
Apollo Server 不允许 GraphQL 自省,但查询包含 _schema 或 _type。 To enable introspection, pass introspection: true to ApolloServer in production
要启用自省,请将 introspection: true 传递给生产环境中的 ApolloServer
Production web-server error:生产网络服务器错误:
Failed to load schema from https://example.com/graphql , reason: unable to verify the first certificate.
无法从https://example.com/graphql加载架构,原因:无法验证第一个证书。 GraphQL Code Generator supports:
GraphQL 代码生成器支持:
- ES Modules and CommonJS exports (export as default or named export "schema")
ES 模块和 CommonJS 导出(默认导出或命名导出“模式”)
- Introspection JSON File
自省 JSON 文件
- URL of GraphQL endpoint
GraphQL 端点的 URL
- Multiple files with type definitions (glob expressions)
具有类型定义的多个文件(glob 表达式)
- String in config file
配置文件中的字符串
Front-end codegen.yml:前端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
Front-end devDependencies:前端开发依赖:
{
"@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 scripts: npm 脚本:
{
"generate": "graphql-codegen -r dotenv/config --watch --config codegen.yml",
"prebuild": "graphql-codegen -r dotenv/config --config codegen.yml"
}
./src/generated/ directory added to.gitignore ./src/generated/目录添加到.gitignore
My solution was in add introspection plugin and separate codegen.yml file into:我的解决方案是添加自省插件并将 codegen.yml 文件分离到:
devDependencies: {"@graphql-codegen/introspection": "^1.18.1"}
codegen-generate-typescript.yml: codegen-generate-typescript.yml:
schema: graphql.schema.json
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
codegen-generate-schema-json.yml: codegen-generate-schema-json.yml:
schema: ${REACT_APP_GRAPHQL_URL}
overwrite: true
generates:
./graphql.schema.json:
plugins:
- introspection
So ./graphql.schema.json file need to be commited and used as schema source instead of URI.所以./graphql.schema.json文件需要被提交并用作模式源而不是 URI。
Maybe you suggest the better solution?也许您建议更好的解决方案?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.