ApolloGateway:如何在接收响应后进行GQL调用?
创始人
2024-09-09 09:30:23
0
  1. 在Apollo Server中设置Gateway的SelectionSetTransformer配置项:
const server = new ApolloServer({
  gateway,
  subscriptions: false,
  context: ({ req }) => ({ req }),
  plugins: [
    {
      requestDidStart() {
        return {
          async didResolveOperation({ operation }) {
            operation.selectionSet = await gateway.transformRequest({
              document: operation.query,
              variables: operation.variables,
              operationName: operation.operationName,
              context: {}
            }).document.definitions[0].selectionSet;
          }
        };
      }
    }
  ]
});

server.listen().then(({ url }) => {
  console.log(`Server ready at ${url}`);
});
  1. 模拟GraphQL查询:
const { request } = require('graphql-request');
const { ApolloGateway } = require('@apollo/gateway');

const gateway = new ApolloGateway({
  serviceList: [
    { name: 'accounts', url: 'http://localhost:4001' },
    { name: 'inventory', url: 'http://localhost:4002' },
    { name: 'products', url: 'http://localhost:4003' },
  ],
});

const query = `
  query Products {
    products {
      id
      name
      image
    } 
  }
`;

async function makeGatewayRequest() {
  const { data } = await gateway.load({
    query,
    context: {},
  });

  console.log('Data received:', data);

  const { request: subRequest } = data.products;

  const subResponse = await request(subRequest.url, subRequest.query, subRequest.variables);

  console.log('Sub response received:', subResponse);
}

makeGatewayRequest();

相关内容

热门资讯

推荐一款!三哥玩辅助器免费下载... 推荐一款!三哥玩辅助器免费下载,微乐小程序辅助开发透视,详细脚本(有挂解惑)1)微乐小程序辅助开发透...
玩家爆料!广东闲来辅助神器免费... 您好,广东闲来辅助神器免费版这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】...
玩家必看科普!三哥玩摆头可以控... 玩家必看科普!三哥玩摆头可以控制,德友汇辅助器,详细器(有挂猫腻)1、三哥玩摆头可以控制透视辅助软件...
一分钟揭秘!潮友会鱼虾蟹有没有... 一分钟揭秘!潮友会鱼虾蟹有没有挂辅助,柳州天天爱字牌辅助器,详细器(有挂教程)1、实时潮友会鱼虾蟹有...
发现玩家!朋朋政和辅助,闲逸辅... 发现玩家!朋朋政和辅助,闲逸辅助器下载,详细app(有挂秘笈)1、玩家可以在闲逸辅助器下载线上大神俱...
玩家爆料!四川长牌皮皮游戏挂,... 玩家爆料!四川长牌皮皮游戏挂,微信牵手跑辅助,详细方法(真是有挂)该软件可以轻松地帮助玩家将四川长牌...
研究成果!牵手跑得软件,大咖娱... 研究成果!牵手跑得软件,大咖娱乐挂脚本,详细教程(有挂方法)一、大咖娱乐挂脚本游戏安装教程牌型概率发...
必知教程!微友圈app辅助工具... 必知教程!微友圈app辅助工具,微信小程序挂件辅助,详细脚本(讲解有挂)微信小程序挂件辅助破解侠是真...
实测分享!潮汕鱼虾蟹app源码... 实测分享!潮汕鱼虾蟹app源码,开心泉州小程序辅助免费下载,详细器(有挂透视)开心泉州小程序辅助免费...
今日头条!光明大厅透视辅助,天... 今日头条!光明大厅透视辅助,天天透视辅助工具,详细插件(有挂详细)1、许多玩家不知道天天透视辅助工具...