# Docs should not suggest returning graphql errors on authentication failure

**URL:** <https://community.apollographql.com/t/docs-should-not-suggest-returning-graphql-errors-on-authentication-failure/5852>\
**Category:** Client SDKs\
**Tags:** web, client\
**Created:** [March 2, 2023, 5:01am UTC](https://community.apollographql.com/t/docs-should-not-suggest-returning-graphql-errors-on-authentication-failure/5852 "2023-03-02T05:01:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jedwards1211](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/jedwards1211/32/3979_2.png) [@jedwards1211](https://community.apollographql.com/u/jedwards1211)\
**Post date:** [March 2, 2023, 5:01am UTC](https://community.apollographql.com/t/docs-should-not-suggest-returning-graphql-errors-on-authentication-failure/5852/1 "2023-03-02T05:01:47Z")

</div>

I was implementing OAuth refresh token exchange in my app and came across this misguided example in the Apollo docs:

```auto
onError(({ graphQLErrors, networkError, operation, forward }) => {
  if (graphQLErrors) {
    for (let err of graphQLErrors) {
      switch (err.extensions.code) {
        // Apollo Server sets code to UNAUTHENTICATED
        // when an AuthenticationError is thrown in a resolver
        case "UNAUTHENTICATED":
          // 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);
      }
    }
  }

  // To retry on network errors, we recommend the RetryLink
  // instead of the onError link. This just logs the error.
  if (networkError) {
    console.log(`[Network error]: ${networkError}`);
  }
});

```

As far as I understand from the GraphQL over HTTP draft spec, [the server should be responding with 401 if it requires authentication](https://graphql.github.io/graphql-over-http/draft/#sel-CAHDLAADNKyBh1E), e.g. because a token expired, which must be handled as a network error. It would be wrong to return graphql field errors when authentication is required; resolvers should only throw **authorization** errors (meaning user is authenticated but not authorized to query a certain field or perform a certain mutation, while others may be allowed).

This example led me astray and I wasted time trying to handle token refresh in `onError`. The proper way appears to be to use a `RetryLink` with an async `retryIf` function that exchanges a refresh token on 401, then resolves to true. I can’t find any examples of `async retryIf` usage but I see in the code that it works and that’s what I had to do to get my refresh token exchange working without any errors popping up in the UI.

---

<div class="post-metadata">

**Author:** ![JeffAuriemma](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/jeffauriemma/32/2845_2.png) [@JeffAuriemma](https://community.apollographql.com/u/JeffAuriemma)\
**Post date:** [March 13, 2023, 8:43pm UTC](https://community.apollographql.com/t/docs-should-not-suggest-returning-graphql-errors-on-authentication-failure/5852/2 "2023-03-13T20:43:11Z")

</div>

Hi @jedwards1211 👋 thanks for the feedback! I’m glad you’re unblocked and it’s unfortunate that you had a bit of a side quest while implementing. Since this is fresh in your mind would you be interested in submitting a change via a PR? The maintainers would be eager to review it. Thanks a bunch 🙏

---

<div class="post-metadata">

**Author:** ![jedwards1211](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/jedwards1211/32/3979_2.png) [@jedwards1211](https://community.apollographql.com/u/jedwards1211)\
**Post date:** [March 13, 2023, 10:55pm UTC](https://community.apollographql.com/t/docs-should-not-suggest-returning-graphql-errors-on-authentication-failure/5852/3 "2023-03-13T22:55:51Z")

</div>

Yeah that’s a good idea, I’ll try to work on it tomorrow night!

---

<div class="post-metadata">

**Author:** ![Justin\_Gonzalez](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/justin_gonzalez/32/5359_2.png) [@Justin\_Gonzalez](https://community.apollographql.com/u/Justin_Gonzalez)\
**Post date:** [February 6, 2025, 8:49pm UTC](https://community.apollographql.com/t/docs-should-not-suggest-returning-graphql-errors-on-authentication-failure/5852/4 "2025-02-06T20:49:17Z")

</div>

Archived on February 6, 2025, 8:49pm UTC


