How do I logically split up my GraphQL schema and Resolvers with Kickstart Spring Boot GraphQL

江枫思渺然 提交于 2020-04-10 05:20:08

问题


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) {
        ...
    }
}

How do I logically separate my queries - or at least not have all my queries in the Query class.


回答1:


If you want to completely separate user-related queries from organization-related queries, then:

  1. Split single .graphqls file into separate files:

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. Split resolvers:
    @Component
    public class UserQuery implements GraphQLQueryResolver {
        public User fetchUser(String email) {
            ...
        }
    }
    @Component
    public class OrganizationQuery implements GraphQLQueryResolver {
        public List<GenericListing> listOrganisations(Integer max) {
            ...
        }
    }

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 plugin: graphql-java-codegen-gradle-plugin
  • Maven plugin: grapqhl-java-codegen-maven-plugin


来源:https://stackoverflow.com/questions/56856688/how-do-i-logically-split-up-my-graphql-schema-and-resolvers-with-kickstart-sprin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!