Apollo Server - GraphQL Error: There can be only one type named "Query" Apollo Server - GraphQL Error: There can be only one type named "Query" express express

Apollo Server - GraphQL Error: There can be only one type named "Query"


When following the pattern of deep modularization, where you want to have each type definition in its own file, and each set of resolvers in their own file, you want to use the extend keyword and create "empty" definitions.

Supposing that you have root and user type definitions in separate files, your index file that puts them together should look like this:

const user = require('./user');const root= require('./root');const typeDefs = gql`    type Query{        _empty: String    }    type Mutation {        _empty: String    }    ${user}    ${root}`;module.exports = typeDefs;

You're using

    type Query{        _empty: String    }

to make an empty Query. Then you're adding your user and root at the end.

Within your user file, you'd want this:

    extend type Query {        getUsers: [User]    }

So the extend keyword is you extending the empty query you created in your index file.

You can read more on modularization here https://blog.apollographql.com/modularizing-your-graphql-schema-code-d7f71d5ed5f2