基于上下文的多个ApolloLink

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

我想实现一种根据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
3个回答
0
投票

解决方案解决

    export const client = (): ApolloClient<any> =>
  new ApolloClient({
    link: ApolloLink.split(
      (operation) => operation.getContext().clientName === 'core',
      urlCore,

      ApolloLink.split(
        (operation) => operation.getContext().clientName === 'user',
        urlUsers,
        urlPrueba1,
      ),

      ApolloLink.split(
        (operation) => operation.getContext().clientName === 'prueba1',
        urlPrueba1,
        urlPrueba2,
      ),

      ApolloLink.split(
        (operation) => operation.getContext().clientName === 'prueba2',
        urlPrueba2,
        urlPrueba3,
      ),
    ),
    // headers: addHeaders({}),
    cache: new InMemoryCache(),
  });

0
投票

你解决了吗?我有两台服务器。每个服务器还有ws服务器。这个apollo客户端很难设置多个服务器


-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: {},
})
© www.soinside.com 2019 - 2024. All rights reserved.