如何使用 Kickstart Spring Boot GraphQL 在逻辑上拆分我的 GraphQL 模式和解析器 [英] How do I logically split up my GraphQL schema and Resolvers with Kickstart Spring Boot GraphQL
问题描述
目前,我所有的查询解析器都在一个查询类和一个查询模式/类型下:
At the moment, all my query resolvers are under a single Query class and a single Query schema / type as such:
schema {
query: Query #I'd prefer UserQueries and OrganisationQueries
mutation: UserMutations
mutation: OrganisationMutations
}
type Query {
fetchUser(email: String): User
listOrganisations(max: Int): [GenericListing]
}
...
以及我在一个班级中的所有查询:
And all my queries in one class:
@Component
public class Query implements GraphQLQueryResolver {
public List<GenericListing> listOrganisations (Integer max) {
...
}
public User fetchUser (String email) {
...
}
}
我已设法按用户和组织拆分并在逻辑上分离我的突变!
I've managed to split up and logically separate my mutations by User and Organisation!
@Component
public class UserMutations implements GraphQLMutationResolver {
public User createUser(String firstname, String lastname, String email, String msisdn, String password) {
...
}
}
我如何在逻辑上分离我的查询 - 或者至少不在 Query 类中包含我的所有查询.
How do I logically separate my queries - or at least not have all my queries in the Query class.
推荐答案
如果您想将用户相关查询与组织相关查询完全分开,则:
If you want to completely separate user-related queries from organization-related queries, then:
- 将单个 .graphqls 文件拆分为单独的文件:
user.graphqls:
schema {
query: Query
mutation: Mutation
}
type Query {
fetchUser(email: String): User
}
...
organization.graphqls:
schema {
query: Query
mutation: Mutation
}
type Query {
listOrganizations(max: Int): [GenericListing]
}
...
- 拆分解析器:
@Component
public class UserQuery implements GraphQLQueryResolver {
public User fetchUser(String email) {
...
}
}
@Component
public class OrganizationQuery implements GraphQLQueryResolver {
public List<GenericListing> listOrganisations(Integer max) {
...
}
}
<小时>
此外,您可以使用 graphql-java-codegen 插件根据您的模式自动生成接口和数据类.顺便说一句,它支持多个模式文件:
Also, you can use graphql-java-codegen plugin for auto-generating interfaces and data classes based on your schemas. By the way, it supports multiple schema files:
- Gradle 插件:graphql-java-codegen-gradle-plugin
- Maven 插件:grapqhl-java-codegen-maven-plugin
这篇关于如何使用 Kickstart Spring Boot GraphQL 在逻辑上拆分我的 GraphQL 模式和解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!