4

我想实现一种基于 graphql 查询中设置的上下文切换不同链接的方法。到目前为止,我所做的是这样的事情,它工作正常,但随着时间的推移似乎不是一个好的解决方案。

const link = ApolloLink.from([
  HandlerLink1,
  HandlerLink2,
  ApolloLink.split(
    operation => operation.getContext().service === "x",
    LinkX,
    ApolloLink.split(
      operation => operation.getContext().service === "y",
      LinkY,
      ApolloLink.split(
        operation => operation.getContext().service === "z",
        LinkZ,
        LinkN
      )
    )
  )
]);

有没有比嵌套更好的方法?

4

1 回答 1

0

我也遇到了同样的问题。下面的代码对我来说很好

const client = new ApolloClient({
  cache,
  link: ApolloLink.split(
    (operation) => operation.getContext().clientName === 'link1',
    Link1, // <= apollo will send to this if clientName is "link1"

    ApolloLink.split(
      (operation) => operation.getContext().clientName === 'link2',
      Link2,// <= apollo will send to this if clientName is "link2"
      Link3,// <= else Link3 will run
    ),
  )
  resolvers: {},
})
于 2021-09-15T14:58:16.863 回答