如何使用Kickstart Spring Boot GraphQL在逻辑上拆分GraphQL模式和解析器 [英] How do I logically split up my GraphQL schema and Resolvers with Kickstart Spring Boot GraphQL

查看:98
本文介绍了如何使用Kickstart Spring Boot GraphQL在逻辑上拆分GraphQL模式和解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此刻,我所有的查询解析器都在单个Query类和单个Query模式/类型下,例如:

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:

  1. 将单个.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]
    }
    ...

  1. 拆分分解器:

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

这篇关于如何使用Kickstart Spring Boot GraphQL在逻辑上拆分GraphQL模式和解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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