【问题标题】:Multiple ApolloLink based on context基于上下文的多个 ApolloLink
【发布时间】:2021-05-15 05:11:39
【问题描述】:

我想实现一种基于 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
      )
    )
  )
]);

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

【问题讨论】:

    标签: apollo-client react-apollo apollo-link


    【解决方案1】:

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

    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: {},
    })
    

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    • 这是我在问题中已经提到的。我的问题:Is there any better way rather than doing it nested?
    猜你喜欢
    • 1970-01-01
    • 2021-01-23
    • 2015-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-17
    • 1970-01-01
    相关资源
    最近更新 更多