问题描述
我正在使用 @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
文件,并使用 codegen 生成相应的类型.
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!