habx / apollo-multi-endpoint-link

⛓ Apollo link which add an api directive to fetch data from multi endpoints
MIT License
132 stars 14 forks source link

Adding different error links for different endpoints #430

Open f1devrouaa opened 1 year ago

f1devrouaa commented 1 year ago

We are trying to pass an apollo link object instead of string to the two endpoints but we are getting error that the apollo link is not assignable to type string.

  return new ApolloClient({
    ssrMode: typeof window === 'undefined',
    link: ApolloLink.from([
      new MultiAPILink({
          endpoints: {
              laravel: {
                  link: ApolloLink.from([
                      authLink,
                      errorLink,
                      httpLink
                  ])
              },
              wordpress: {
                  link: ApolloLink.from([
                      authLinkwp,
                      errorLinkwp,
                      httpLinkwp
                  ])
              }
          } 
      })
  ]),
  cache: new InMemoryCache(),
  });
}

How can this be fixed and how can we add different cache for each

f1devrouaa commented 1 year ago
  return new ApolloClient({
    ssrMode: typeof window === 'undefined',
    link: ApolloLink.from([
      new MultiAPILink({
        getContext: (endpoint) => {
            if (endpoint === 'laravel') {
                return {
                    headers: {
                        authorization: `Bearer ${Cookies.get(AUTH_TOKEN_KEY)}`,
                    },
                    errorLink: errorLink,
                    authLink : authLink,
                    cache : new InMemoryCache(),
                }
              } else if (endpoint === 'wordpress') {
                return {
                  headers: {

                  },
                  errorLink: errorLinkwp,
                  authLink: authLinkwp,
                  afterware,
                  cache : new InMemoryCache(),
              }
          }
          return {}
      },
      endpoints: {
          laravel: httpLink,
          wordpress: httpLinkwp,
          ...

      }, //expression expected
      createHttpLink: () => createHttpLink(),
  }),

This is the alternative method we tried to get rid of error but we get now an error expression expected

ibatra commented 1 year ago

So I ran into same issue as you and mananegd to fix it with a bit of struggle, basically instead of setting context or instead of using createHttpLink I did something like (url, endpoint) => if (endpoint == "main") { return ApolloLink.from([authLink, httpLink])}