Splitting GraphQL mutations and queries into multiple schema files

Viewed 287

I am using Spring Boot and Netflix's DGS library for GraphQL on the backend. Our app has multiple modules (multi-gradle project) and each module is responsible for one thing. Therefore, I wanted to split GQL schema into multiple files based on the business logic of that module (let's say we have packages like cars, people, animals and parent module app, which contains the main SpringBootApp class), so I want only mutations and queries for one particular package on the path and not others. (I don't want to see Mutations for cars in the animal package)

Problem is that I cannot have multiple definitions of the Mutation type in the project, so I tried to put extend type Mutation, but this fails when I run my tests because they don't know the parent since the only schema for cars is copied and not the parent.

I also tried to create some Gradle task, that would copy the schema and remove the extend keyword from the file, which would work, but it's not the best solution.

Another option would be perhaps to create a new type for mutations like UseMutation, CarMutation, but not sure if that is the correct approach.

Do you have any other ideas, how to work and deal with this schema separation? Thanks a lot.

1 Answers

What you want to do is probably have a folder entitled schema , where all your schema definitions reside.

You may have, for example in car.js:

extend type Mutation{
 someRandomMutation : String !
}

extend type Query{
 someRandomQuery : Int!
}

Then , in your index.js you have to basically define the type Mutation and Query for all these pieces that extend them in the first place:

import car from './car'
   import people from './people'

    const linkSchema=gql`
        scalar Date    
        scalar Upload
        
        type Query{
            _:Boolean
        }
    
        type Mutation{
            _:Boolean
        }
    
        type Subscription{
            _:Boolean
        }
    `
    export default [car , people , linkSchema]

Then, in the index.js of your server (or wherever you initialize Apollo Server):

import typeDefs from './schema'

const schema = makeExecutableSchema({ typeDefs, resolvers });

  const server = new ApolloServer({
    schema,
    ....
  });
Related