【问题标题】:Unable to find any GraphQL type definitions for the following pointers: src/**/*.graphql找不到以下指针的任何 GraphQL 类型定义:src/**/*.graphql
【发布时间】:2020-03-13 05:21:03
【问题描述】:

我正在使用 @graphql-codegen/cli 工具从我的 graphql 服务器生成打字稿类型。 这是我的codegen.yml 内容:

overwrite: true
schema: "http://localhost:3001/graphql"
documents: "src/**/*.graphql"
generates:
  src/generated/graphql.tsx:
    plugins:
      - "typescript"
      - "typescript-operations"
      - "typescript-react-apollo"
  ./graphql.schema.json:
    plugins:
      - "introspection"

这是我用来生成类型的package.json 脚​​本 (yarn schema):

"schema": "graphql-codegen --config codegen.yml"

所有这些都是通过执行cli向导yarn codegen init自动生成的。

但是当我运行yarn schema 时,我得到了以下错误:

(服务器在http://localhost:3001/graphql 上运行并公开了图形模式。

感谢您的帮助和建议

这是托管在我的服务器中的 .graphql 文件 (http://localhost:3001/graphql

# -----------------------------------------------
# !!! THIS FILE WAS GENERATED BY TYPE-GRAPHQL !!!
# !!!   DO NOT MODIFY THIS FILE BY YOURSELF   !!!
# -----------------------------------------------

"""Date custom scalar type"""
scalar Date

type Mutation {
  create_user(user: UserInput!): User!
  create_pofficer(pofficer: POfficerCreateInput!): POfficer!
  create_incident(incident: TIncidentInput!): TIncident!
  add_incident_type(incident_type: TIncidentTypeInput!): TIncidentType!
}

type POfficer {
  _id: ID!
  userid: ID!
  user: User!
}

input POfficerCreateInput {
  name: String!
  surname: String!
  phone: String!
}

type Query {
  users: [User!]!
  pofficers: [POfficer!]!
  incidents: [TIncident!]!
  incident_types: [TIncidentType!]!
}

type TIncident {
  _id: ID!
  createdAt: Date!
  incidenttype_id: ID!
  pofficer_id: ID!
  toffender_id: ID
  toffender_phone: String!
  carnumber: String!
  incident_status: String!
  pofficer: POfficer!
  toffender: User!
  incident_type: TIncidentType!
}

input TIncidentInput {
  incidenttype_id: ID!
  pofficer_id: ID!
  toffender_phone: String!
  carnumber: String!
}

type TIncidentType {
  _id: ID!
  name: String!
  description: String
}

input TIncidentTypeInput {
  name: String!
  description: String
}

type User {
  _id: ID!
  name: String!
  surname: String!
  email: String
  phone: String!
}

input UserInput {
  name: String!
  surname: String!
  email: String!
  phone: String!
}

【问题讨论】:

  • 您是否创建了任何带有查询或突变的.graphql 文件?你能把它们贴在这里吗?
  • @Felipe 我已经编辑了我的问题,并添加了由我的服务器在http://localhost:3001/graphql 托管和公开的.graphql 文件

标签: typescript graphql codegen graphql-codegen


【解决方案1】:

您共享的文件是您的架构(由您的服务器生成),但您似乎没有在其上创建任何查询或突变。这将是 codegen 无法正常工作的原因。

我建议您使用简单的查询创建一个新文件,例如:get-users.query.graphql

query GetUsers {
  user {
    _id
    __typename
    name
    surname
    email
    phone
  }
}

并将其添加到您的src 文件夹中(因为您的代码生成配置为在您的src 文件夹中查找所有.graphql 文件)。然后重新运行codegen,看看是否正常。

之后,您可以使用您的查询和突变生成各种.graphql 文件,并使用代码生成器生成相应的类型。

【讨论】:

  • 谢谢@Felipe。我已将您的建议标记为我的问题的解决方案
  • 为什么我必须手动创建这些查询和突变?生成器不能自动为我捡起来吗?
  • 是的,这些是您要在服务器上执行的查询和突变。您的服务器提供的可用查询和突变的定义由 codegen 自动生成。
【解决方案2】:

在我的例子中,我将所有查询重构到一个新文件夹中的单个文件中:lib/queries.tsx

然后我需要做的是将该文件路径添加到codegen.yml:

documents:
  - "./lib/queries.tsx"

【讨论】:

    【解决方案3】:

    请检查您的操作文件的后缀并根据该文件更改此文件,可能您的文件后缀是 .ts 或 .js 并且默认情况下在 codegen.yml 文档中是

    documents: "src/**/*.graphql"    
    

    将 .graphql 替换为您的操作文件名的后缀,例如 .ts 或 .js

    那是我的问题,或者文件地址错误

    【讨论】:

      猜你喜欢
      • 2020-11-05
      • 2021-11-20
      • 1970-01-01
      • 2020-06-01
      • 2018-01-17
      • 2021-09-12
      • 2020-11-11
      相关资源
      最近更新 更多