apollo-server: Error: Unknown type "Query".

I’ve been getting this error for a while now when I am trying to compile my code.

Error: Unknown type "Query".

index.js

import { ApolloServer } from 'apollo-server';
import path from 'path';
import { fileLoader, mergeTypes, mergeResolvers } from 'merge-graphql-schemas';

import models from './models/index';

const typeDefs = mergeTypes(fileLoader(path.join(__dirname, './schema')));
const resolvers = mergeResolvers(
  fileLoader(path.join(__dirname, './resolvers'))
);

const server = new ApolloServer({ typeDefs, resolvers, context: models });

models.sequelize.sync({ force: true }).then(x => {
  server.listen().then(({ url }) => {
    console.log(`🚀  Server ready at ${url}`);
  });
});

schema/user.js

export default {
  Query: {
    getUser: (parent, { id }, { models }) => {
      models.User.findOne({ where: { id } });
    },
    allUsers: (parent, args, { models }) => {
      models.User.findAll();
    }
  },
  Mutation: {
    createUser: (parent, args, { models }) => {
      models.User.create(args);
    }
  }
};

Can somebody please help me because the error message doesn’t tell me much.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 5
  • Comments: 16 (1 by maintainers)

Most upvoted comments

I am new to graphql aswell and I had the same problem. I am using merge-graphql-schemas aswell. My problem was that I was using Typescript and my .graphql files weren’t copied over to the output directory (by default directory named dist, check out your tsconfig.json) by tsc command. Now I am using cpx (a node package) to copy over all my .graphql files to my tsc output directory.

Fixed. It was an easy one! I was not copying the typedef files to the dist folder. This was required for merge-graphql-schemas to stitch the schema together. You can now see the complete working solution here.

@eladnm, in my use case I am creating “mini” GraphQL schemas and merging them to one final schema using a library called merge-graphql-schemas. This works in development mode because merge-graphql-schemas can find the “mini” schemas in the src folder. However when I build for production mode (i.e. compile TypeScript into JavaScript), then merge-graphql-schemas cannot find the scemas in the dist folder. Hence the error. To prevent this error, I have to copy the mini schemas to the dist folder. Hope that explains.

@reeversedev , I know it’s late but i think your error seems to be your folder location ,in path.join(__dirname,relative_folder_location) eg: if schema is in slack-clone-server/src/schema then it should be as path.join(__dirname,./src/schema)

For me this is still open because honestly, I’ve not been able to reproduce more.

@Istjustaname can you provide a Github repo to share. So we can see how you did it? I have the same problem as @reeversedev