首页 > 解决方案 > 是否可以在网关级别模拟 GraphQL 服务?

问题描述

想象一下下面的 Node(简化)模块。它使用底层 Apollo 联合网关定义了一个 Hapi 服务器。服务器有一个/graphql能够接受 GraphQL 请求并聚合来自多个联合 GraphQL 服务器的数据的路由。

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;
};

我想模拟 GraphQL 响应以进行测试。我的猜测是,根据我从文档中得到的信息,不能在这个级别使用普通的 Apollo 或 GraphQL 模拟策略,主要是因为我读到的模拟策略依赖于手头的模式(使用graphql-tools mockServer, for例子)。

我的假设是否正确,我是否需要在服务器级别模拟单个服务?这是否意味着可以认为没有必要使用模拟服务测试 Apollo 网关?

标签: mockinggraphqlapolloapollo-federation

解决方案


推荐阅读