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

1

2

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.

Hithermost answered 2/7, 2019 at 16:27 Comment(0)
R
0

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

  1. Split single .graphqls file into separate files:

user.graphqls:

    extend type Query {
        fetchUser(email: String): User
    }
    ...

organization.graphqls:

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

Renal answered 26/12, 2019 at 21:11 Comment(3)
Hi, I'm trying to do this (multiple graphqls files), but Altair seems to only pick up the "last" one. Same issue with graphiql. Any ideas?Woodsy
Hi Did you find the solution? I also have multiple filesRouble
Make sure that if you have multiple queries/mutations spread across multiple files then you use an extend keyword: extend type Query / extend type Mutation.Renal

© 2022 - 2024 — McMap. All rights reserved.