【问题标题】:How do I logically split up my GraphQL schema and Resolvers with Kickstart Spring Boot GraphQL如何使用 Kickstart Spring Boot GraphQL 在逻辑上拆分我的 GraphQL 架构和解析器
【发布时间】:2019-11-13 08:25:17
【问题描述】:

目前,我所有的查询解析器都在一个查询类和一个查询架构/类型下:

schema {
    query: Query #I'd prefer UserQueries and OrganisationQueries
    mutation: UserMutations
    mutation: OrganisationMutations
}

type Query {
    fetchUser(email: String): User
    listOrganisations(max: Int): [GenericListing]
}

...

我所有的查询都在一个类中:

@Component
public class Query implements GraphQLQueryResolver {

    public List<GenericListing> listOrganisations (Integer max) {
        ...
    }

    public User fetchUser (String email) {
        ...
    }
}

我已经设法按用户和组织划分并在逻辑上分离我的突变!

@Component
public class UserMutations implements GraphQLMutationResolver {

    public User createUser(String firstname, String lastname, String email, String msisdn, String password) {
        ...
    }
}

我如何在逻辑上分离我的查询 - 或者至少不在 Query 类中包含我的所有查询。

【问题讨论】:

    标签: java spring-boot graphql graphql-java


    【解决方案1】:

    如果您想将与用户相关的查询与与组织相关的查询完全分开,那么:

    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 插件根据您的模式自动生成接口和数据类。顺便说一句,它支持多个模式文件:

    【讨论】:

    • 嗨,我正在尝试执行此操作(多个 graphqls 文件),但 Altair 似乎只选择了“最后一个”文件。与 graphiql 相同的问题。有什么想法吗?
    猜你喜欢
    • 2020-09-17
    • 1970-01-01
    • 2019-07-20
    • 2020-02-22
    • 2019-03-21
    • 2019-07-14
    • 2020-10-01
    • 2021-09-12
    • 2018-03-18
    相关资源
    最近更新 更多