将Apollo服务器用作来自客户端的查询的传递

问题描述 投票:0回答:1

我有一个用例,其中apollo-server-express与基于React的apollo-client一起运行。我有一些查询的外部graphql-datasource。目前,我已将apollo-datasource-graphql配置为用作apollo-server-express的数据源。但是,这需要在Apollo中的解析器以及我的外部graphql系统上的解析器上重复工作。

我是否可以通过Apollo服务器将客户端中的查询传递给外部graphql数据源?

graphql apollo-server apollo-datasource
1个回答
1
投票

也许您可以从第四个解析器参数(resolveInfo)访问GraphQL AST,并将其传递给GraphQL客户端?

这是一些原型代码:

import { print } from 'graphql/language/printer';

function forwardOperationResolver(root, args, context, resolveInfo) {
  return fetch('https://remote.host/graphql', {
    method: 'POST',
    body: JSON.stringify({
      query: print(resolveInfo.operation),
      variables: resolverInfo.variableValues,
    }),
  })
    .then(response => response.json())
    .then(response => {
      if (response.errors) {
        // Handle errors
      }
      return response.data;
    });
}

缺点:这打破了通常在GraphQL中工作的一些东西,例如部分结果和错误位置...

© www.soinside.com 2019 - 2024. All rights reserved.