我从angular graphql code generator开始了解here。我试图在我的项目中使用它来生成graphql类型和Query
服务的角度。
我的codegen.yml文件如下所示。
overwrite: true
schema: "http://localhost:9010/myGraphQL"
documents: "apps/myGraphQLApp/src/gqls/**/*.graphql"
generates:
apps/myGraphQLApp/src/app/generated/graphql.ts:
plugins:
- "typescript"
- "typescript-operations"
- "typescript-apollo-angular"
我有一个带有以下内容的graphql文件apps\myGraphQLApp\src\gqls\myBooks.graphql
query GetMyBooks {
books{
id
name
}
}
我正在运行npx graphql-codegen --config codegen.yml
,但失败并出现以下错误。
√ Parse configuration
> Generate outputs
> Generate apps/myGraphQLApp/src/app/generated/graphql.ts
√ Load GraphQL schemas
√ Load GraphQL documents
× Generate
→ Query root type must be provided.
Found 1 error
× apps/myGraphQLApp/src/app/generated/graphql.ts
Error: Query root type must be provided.
at assertValidSchema (....\node_modules\graphql\type\validate.js:71:11)
...
我的graphql服务器肯定公开了查询根,我可以用其他客户端调用它。
schema {
query: MyGraphQLQueryType
}
我不确定自己在做什么错。任何帮助将不胜感激。
答案 0 :(得分:0)
我在1年前使用graphql generator进行了示例,所以也许它已经过时了新版本,但是如果想要尝试的话,我可以
overwrite: true
schema: schema.graphql
documents: src/app/graphql/*.graphql
generates:
./src/app/generated/graphql.ts:
plugins:
- typescript-common
- typescript-client
- typescript-apollo-angular
./schema.json:
plugins:
- introspection
我认为您的问题是您的架构,您应该有这样的东西
type Query {
login(email: String!, password: String!): AuthData!
user: User!
pets(page: Int, name: String, type: String): PetData!
pet(id: ID!): Pet!
}
type Mutation {
createUser(userInput: UserInputData): User!
deletePet(id: ID!): Boolean
}
type User {
id: ID!
name: String!
email: String!
password: String!
status: String!
pets: [Pet!]!
}
type AuthData {
token: String!
userId: String!
}
input UserInputData {
email: String!
name: String!
password: String!
}
type Pet {
id: ID!
name: String!
type: String!
imageUrls: [File!]!
creator: UserPet!
createdAt: String!
updatedAt: String!
}
type PetData {
pets: [Pet!]!
totalPets: Int!
filterData: FilterDataPet!
}
type File {
id: String!
path: String!
filename: String!
mimetype: String!
}
type UserPet {
id: ID!
name: String!
email: String!
pets: [Pet!]!
}
type FilterDataPet {
isFilterData: Boolean!
currentPage: Int!
queryParams: QueryParamsPet
}
type QueryParamsPet {
name: String
type: String
}
您可以在https://github.com/anthowm/graphqlpetapi-frontend/blob/master/codegen.yml
处查看完整的示例