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();

相关内容

热门资讯

一分钟科普!夜猫十三道有挂(辅... 一分钟科普!夜猫十三道有挂(辅助挂)外挂透视辅助脚本(2022已更新)(哔哩哔哩);是一种具有地方特...
每日必看!德州aa扑克透明挂(... 每日必看!德州aa扑克透明挂(wepoke美元局)外挂透视辅助工具(辅助挂)解密教程(2025已更新...
分享开挂内幕!wpk使用ai辅... 分享开挂内幕!wpk使用ai辅助会封号吗(透视)原来确实真的有挂2020已更新)(哔哩哔哩)1、这是...
两分钟实锤!广西老友摆牌十三张... 您好,广西老友摆牌十三张这款游戏可以开挂的,确实是有挂的,需要了解加微【487309276】很多玩家...
发现玩家!wepoke保险(W... 发现玩家!wepoke保险(Wepoke技巧)外挂透视辅助工具(透视)2024教程(2025已更新)...
玩家必看分享!红龙扑克机制(透... 玩家必看分享!红龙扑克机制(透视)外挂透明挂辅助挂(2023已更新)(哔哩哔哩)是一款可以让一直输的...
5分钟实锤!微信闽南麻将有挂,... 自定义系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助器,不管你是想分享给你好...
透明工具!aa poker辅助... 这是一款非常优秀的拱趴大菠萝切牌规律 ia辅助检测软件,能够让你了解到拱趴大菠萝切牌规律中牌率当中全...
玩家必看教程!红龙扑克都是机器... 您好,红龙扑克这款游戏可以开挂的,确实是有挂的,需要了解加微【285696317】很多玩家在这款游戏...
2分钟攻略!逍遥卡五星胡牌辅助... 2分钟攻略!逍遥卡五星胡牌辅助器,广东雀神挂件去哪买,透明教程(有挂功能)1、每一步都需要思考,不同...