我的 API 是用 TypeScript/Apollo 编写的,我可以在 http://localhost:4000/graphql 上运行查询/突变。我的前端是 Next.js 和 Apollo 客户端。我使用 GraphQL Codegenerator 来生成我需要的客户端代码。
我在用:
"@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",
我的codegen.yml
文件是
overwrite: true
schema: "http://localhost:4000/graphql"
documents: "src/graphql/**/*.graphql"
generates:
src/generated/graphql.tsx:
plugins:
- "typescript"
- "typescript-operations"
- "typescript-react-apollo"
我更改 API 解析器时运行的脚本是"gen": "graphql-codegen --config codegen.yml"
如果我确实运行此代码,则会收到错误消息:
✖ src/generated/graphql.tsx
Error:
Unable to find any GraphQL type definitions for the following pointers:
- src/graphql/**/*.graphql
但是如果codegen.yml
按如下方式设置我的文件它可以工作(并且我在其中一个组件中测试了一个查询,我可以访问数据):
overwrite: true
schema: "http://localhost:4000/graphql"
documents:
- "src/graphql/fragments/RecipeSnippet.graphql"
- "src/graphql/fragments/RegularError.graphql"
- "src/graphql/fragments/RegularUser.graphql"
- "src/graphql/fragments/RegularUserResponse.graphql"
- "src/graphql/mutations/createRecipe.graphql"
- "src/graphql/mutations/deleteRecipe.graphql"
- "src/graphql/mutations/login.graphql"
- "src/graphql/mutations/logout.graphql"
- "src/graphql/mutations/register.graphql"
- "src/graphql/mutations/updateRecipe.graphql"
- "src/graphql/queries/me.graphql"
- "src/graphql/queries/recipe.graphql"
- "src/graphql/queries/recipes.graphql"
generates:
src/generated/graphql.tsx:
plugins:
- "typescript"
- "typescript-operations"
- "typescript-react-apollo"
我不确定我到底做错了什么,更奇怪的是它曾经有效。我记得最近更新了我的部门。我看到并尝试了其他一些答案,例如这个或这个,但无济于事:(