3

Imagine the following Node (simplified) module. It defines a Hapi server using an underlying Apollo federated gateway. The server has a /graphql route capable of accepting GraphQL requests and aggregating data from several federated GraphQL servers.

const Hapi = require("hapi");
const { ApolloServer } = require("apollo-server-hapi");
const initGateway = require("./init-gateway"); // Apollo Gateway
const context = require("./some-context-getter");

module.exports = async ({ config, plugins, routes }) => {  
  const gateway = initGateway();
  const hapiServer = new Hapi.Server(config);
  const apolloServer = new ApolloServer({
    gateway,
    context,
    // mocks: true, doesn't work
    subscriptions: false
  });

  await hapiServer.register(plugins);
  hapiServer.route(routes);

  await apolloServer.applyMiddleware({
    app: hapiServer
  });

  return hapiServer;
};

I would like to mock GraphQL responses for testing purposes. My guess is, from what I've got from the docs, that the normal Apollo or GraphQL mocking strategies can't be used at this level, mainly because the mocking strategies I read about depend on having the schema at hand (using graphql-tools mockServer, for example).

Is my assumption right, would I need to mock individual services at the server level? Does this mean that testing an Apollo gateway with mocked services could be considered unnecessary?

Mario Gil
  • 493
  • 1
  • 5
  • 14

0 Answers0