Skip to content

Splitting root query and mutation across many classes #35

Closed

Description

@sazzer

Sorry if this is already possible, but would it be an option to have the root query and mutation objects split across many classes?

As best I can tell at present, these are only one single class, so if you have a large number of query fields - as a reasonable size API will - then they all end up in the same class. Worse though, this one class has many different concerns to worry about.

For example, the Query class on a blog might have to support: Users, Posts, Comments, and several different query methods for each of these.

Activity

  1. apottere commented on Jul 10, 2017

    @apottere
    Collaborator

    This seems like a great idea, and it shouldn't be too hard to implement. Thanks for the suggestion!

  2. apottere commented on Jul 11, 2017

    @apottere
    Collaborator

    Duplicated by #32

  3. added a commit that references this issue on Jul 28, 2017
    72e6389
  4. rolandkozma commented on Sep 6, 2017

    @rolandkozma

    Hi. Is there a sample of how to split the root query class across many resolver classes?

  5. apottere commented on Sep 6, 2017

    @apottere
    Collaborator

    @rolandkozma there isn't an example right now, but you can just implement GraphQLQueryResolver on any class you want to be scanned for root resolver methods.

  6. rolandkozma commented on Sep 6, 2017

    @rolandkozma

    That's nice! It works well. Initially I expected to have to split them in graphql schema too.
    Thanks for answering!

  7. santiago-vl commented on Jun 5, 2018

    @santiago-vl

    @rolandkozma In my schema.graphql file i have this:

    schema {
      query: Query
      mutation: Mutation
    }
    
    type User { ... }
    type Product { ... }
    
    type Query {
      getUser(...)
      getProduct(...)
    }
    type Mutation { 
      createUser(...)
      createProduct(...)
    }
    

    I have a RootResolver like this:

    @Component
    public class RootResolver implements GraphQLQueryResolver, GraphQLMutationResolver {
        /********* PRODUCTS ********/
        public Product getProducts(...) { ... }
        public Product createProducts(...) { ... }
        /********* USERS ********/
        public User getUser(...) { ... }
        public Product createUser(...) { ... }
    }
    

    In the schema creation:

    @Autowired
    RootResolver rootResolver;
    
    GraphQLSchema schema = SchemaParser.newParser()
                    .files(schema.graphql)
                    .resolvers(rootResolver)
                    .build()
                    .makeExecutableSchema();
    

    All works correctly. But i want to split my resolvers in more classes according to the model entities:

    UserResolver.java:

    @Component
    public class UserResolver implements GraphQLQueryResolver, GraphQLMutationResolver {
        public User getUser(...) { ... }
        public Product createUser(...) { ... }
    }
    

    ProductResolver.java:

    @Component
    public class ProductResolver implements GraphQLQueryResolver, GraphQLMutationResolver {
        public Product getProducts(...) { ... }
        public Product createProducts(...) { ... }
    }
    

    Application.java:

    @Autowired
    ProductResolver productResolver;
    @Autowired
    UserResolver userResolver;
    
     GraphQLSchema schema = SchemaParser.newParser()
                    .files(schema.graphql)
                    .resolvers(productResolver, userResolver)
                    .build()
                    .makeExecutableSchema();
    

    If i do this i obtain the next error:

    Caused by: com.coxautodev.graphql.tools.FieldResolverError: No method or field found with any of the following signatures (with or without one of [interface graphql.schema.DataFetchingEnvironment] as the last argument), in priority order:
    
    ...graphql.resolvers.SmartGroupsResolver.getUsers()
    

    What am i doing wrong?

  8. pclnzwang commented on Jun 21, 2018

    @pclnzwang

    @SantyVylla Remove schema in the schema.graphqls file and try again. Defined resolvers based on "type Query / Mutation" is fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions