title: How to refresh JWT token using Apollo and GraphQL
body: <p>So we're creating a React-Native app using Apollo and GraphQL. I'm using JWT based authentication(when user logs in both an <em><strong>activeToken</strong></em> and <em><strong>refreshToken</strong></em> is created), and want to implement a flow where the token gets refreshed automatically when the server notices it's been expired.</p>
  <p>The Apollo Docs for Apollo-Link-Error provides a good <a href="https://www.apollographql.com/docs/link/links/error/#retrying-failed-requests" rel="noreferrer">starting point</a> to catch the error from the ApolloClient:</p>
  <pre class="lang-js prettyprint-override"><code>onError(({ graphQLErrors, networkError, operation, forward }) =&gt; {
    if (graphQLErrors) {
      for (let err of graphQLErrors) {
        switch (err.extensions.code) {
          case 'UNAUTHENTICATED':
            // error code is set to UNAUTHENTICATED
            // when AuthenticationError thrown in resolver
  
            // modify the operation context with a new token
            const oldHeaders = operation.getContext().headers;
            operation.setContext({
              headers: {
                ...oldHeaders,
                authorization: getNewToken(),
              },
            });
            // retry the request, returning the new observable
            return forward(operation);
        }
      }
    }
  })
  </code></pre>
  <p>However, I am really struggling to figure out how to implement <strong>getNewToken()</strong>.
  My GraphQL endpoint has the resolver to create new tokens, but I can't call it from Apollo-Link-Error right?</p>
  <p>So how do you refresh the token if the Token is created in the GraphQL endpoint that your Apollo Client will connect to?</p>