无法找到以下指针的任何 GraphQL 类型定义:src/**/*.graphql

The*_*oul 12 codegen typescript graphql graphql-codegen

我正在使用该@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"
Run Code Online (Sandbox Code Playgroud)

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

"schema": "graphql-codegen --config codegen.yml"
Run Code Online (Sandbox Code Playgroud)

所有这些都是通过执行 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!
}
Run Code Online (Sandbox Code Playgroud)

Fel*_*ipe 14

您共享的文件是您的架构(由您的服务器生成),但您似乎尚未在其上创建任何查询或更改。这将是代码生成器无法正常工作的原因。

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

query GetUsers {
  user {
    _id
    __typename
    name
    surname
    email
    phone
  }
}
Run Code Online (Sandbox Code Playgroud)

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

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

  • 为什么我必须手动创建这些查询和突变?生成器不能自动为我拾取它们吗? (4认同)
  • 这个解决方案对我不起作用。我正在开始一个新问题。 (3认同)
  • @Felipe 你能进一步解释一下吗?为什么需要在专用文件中声明查询?生成器无法使用内省功能创建查询? (2认同)