Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

accessing client in apollo-link-error onError() #595

Closed
zsolt-dev opened this issue Apr 6, 2018 · 4 comments
Closed

accessing client in apollo-link-error onError() #595

zsolt-dev opened this issue Apr 6, 2018 · 4 comments

Comments

@zsolt-dev
Copy link

Hi,

I have spent several days on this without a success.

What I basically need is to access client, so I can call a mutation to change a global state if there is an error in the arterware.

const client = location => new ApolloClient({
  link: ApolloLink.from([
    onError(({ graphQLErrors, networkError }) => {
      if (graphQLErrors && graphQLErrors.find(isUnauthorized) && !location.pathname.startsWith('/login')) {
        client.resetStore();
        client.mutate({ mutation: notificationAddMutation, variables: { text: 'You did not have rights to this operation. Maybe logged out?' } });
      }
      if (networkError) {
        client.mutate({ mutation: notificationAddMutation, variables: { text: 'There was a network problem. Please check your connection' } });
      }
    }),
    withClientState({
      cache,
      defaults,
      resolvers,
      typeDefs,
    }),
    httpLink,
  ]),
  cache,
});

I get this error: TypeError: client.mutate is not a function

Any ideas how to access client there? Or how to call a local mutation without a client?

@evans
Copy link
Contributor

evans commented Apr 10, 2018

In your code client is being set to a function that returns an instance of Apollo Client, so I think you will have to do something like:

const clientFunction = location => {
  const client = new ApolloClient({
    link: ApolloLink.from([
      onError(({ graphQLErrors, networkError }) => {
        if (
          graphQLErrors &&
          graphQLErrors.find(isUnauthorized) &&
          !location.pathname.startsWith('/login')
        ) {
          client.resetStore();
          client.mutate({
            mutation: notificationAddMutation,
            variables: {
              text:
                'You did not have rights to this operation. Maybe logged out?',
            },
          });
        }
        if (networkError) {
          client.mutate({
            mutation: notificationAddMutation,
            variables: {
              text: 'There was a network problem. Please check your connection',
            },
          });
        }
      }),
      withClientState({
        cache,
        defaults,
        resolvers,
        typeDefs,
      }),
      httpLink,
    ]),
    cache,
  });
  return client;
};

clientFunction(LOCATION);

@evans evans closed this as completed Apr 10, 2018
@zsolt-dev
Copy link
Author

Thank you very much,

this worked.

For anyone interested:
I am running into lot of issues with afterware and I still have to have substantial logic in each mutation error handler, so I will be probably abandoning the afterware completely and have a function that I will use in each mutation catch block.

@mrdulin
Copy link

mrdulin commented Aug 3, 2018

@ptimson
Copy link

ptimson commented Jan 14, 2020

Hey I can't seem to get this working

const graphQLClient = new ApolloClient({
  cache: new InMemoryCache(),
  link: ApolloLink.from([
    authLink(graphQLClient),
    refreshLink(graphQLClient),
    httpLink,
  ]),
})

const authLink = (graphQLClient) => setContext(async (req, { headers }) => { ... }
const refreshLink = (graphQLClient) => onError(({ graphQLErrors, networkError, operation, forward }) => { ... }

I have passed graphQLClient to the link functions but graphQLClient is undefined in the links

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants