找不到以下指针的任何GraphQL类型定义:src/**/*.graphql [英] Unable to find any GraphQL type definitions for the following pointers: src/**/*.graphql

查看:74
本文介绍了找不到以下指针的任何GraphQL类型定义:src/**/*.graphql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using the @graphql-codegen/cli tool to generate typescript types out of my graphql server. Here is my codegen.yml content:

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 ):

Here is the package.json script I use to generate my types (yarn schema):

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

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

All these have been automatically generated by executing the cli wizard yarn codegen init.

但是当我运行 yarn schema 时,这些是我得到的错误:

But when I run yarn schema, these are the errors I get:

(服务器肯定在 http://localhost:3001/graphql 上运行,并公开了图模式.

(server is positively running at http://localhost:3001/graphql and exposes the graph schema.

感谢您的帮助和建议

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

Here is the .graphql file hosted in my server (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!
}

推荐答案

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

The file your shared is your schema (as generated by your server), but it seems that you haven't created any queries or mutations on top of it. This would be a reason why the codegen is not working properly.

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

I suggest thay you create a new file with a simple query, such as: get-users.query.graphql

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

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

And add it to your src folder (since your codegen is configured to find all .graphql files inside your src folder). Then re-run the codegen and see if it works well.

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

Aftewards you can generated all kinds of .graphql files with your queries and mutations and use the codegen to generate the corresponding types.

这篇关于找不到以下指针的任何GraphQL类型定义:src/**/*.graphql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆