Search code examples
node.jsjestjsapollots-jesttypegraphql

Can't create a new test Apollo Server


I would like to test my resolvers with the executeOperation API, according to the docs I need to create a test server, here's mine.

export const getTestServer = async () => {
  const redis = new Redis({});

  const schema = await buildSchema({
    resolvers: [UserResolver],
    validate: false,
  });

  const server = new ApolloServer({
    schema,
    context: ({ req, res }): AppContext => {
      req = mocks.createRequest();
      req.session.userId = 1;

      return { req, res, redis };
    },

  return server;
};

In here, I'm using typegraphql for building my schema and this code works for my actual server, but in my tests it throws the following error:

    Some errors occurred while generating GraphQL schema:
      Type Query must define one or more fields.
    Please check the `details` property of the error to get more detailed info.

       9 |   const redis = new Redis({});
      10 |
    > 11 |   const schema = await buildSchema({
         |                  ^
      12 |     resolvers: [UserResolver],
      13 |     validate: false,
      14 |   });

      at Function.generateFromMetadata (node_modules/type-graphql/dist/schema/schema-generator.js:20:23)
      at buildSchema (node_modules/type-graphql/dist/utils/buildSchema.js:10:20)
      at getTestServer (src/__tests__/config.ts:11:18)
      at Object.<anonymous> (src/__tests__/resolvers/user.test.ts:19:20)

Solution

  • Turns out I need at least 1 @Query decorator in my resolvers, I just added a dummy resolver until I need an actual query. For testing, I switched to supertest, check this example if you are interested in using it too for testing