Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

forward(operation) not retrying in my case #9971

Open
ShourovRoy opened this issue Aug 1, 2022 · 2 comments
Open

forward(operation) not retrying in my case #9971

ShourovRoy opened this issue Aug 1, 2022 · 2 comments

Comments

@ShourovRoy
Copy link

Here is my code and when I am trying to retry using forward(operation) by setting the header with new access token the retry is not working.

Here is the code:

const getNewToken = async () => {

return await axios.post('http://localhost:4000/graphql', {
  query: `
    mutation REFRESH($refreshToken: String!){
        refreshToken (body: {
            refreshToken: $refreshToken
        }) {
            accessToken
            refreshToken
        }
    }
  `,
  variables: {
    refreshToken: Cookies.get("refreshToken") || ""
  }
}).then(data => {
 
  Cookies.set("accessToken",  data.data.data.refreshToken.accessToken);
  Cookies.set("refreshToken",  data.data.data.refreshToken.refreshToken);

  return data.data.data.refreshToken.accessToken;
}).catch(err => {
  return "";
})

}

const interceptors = onError(({ networkError, forward, operation, graphQLErrors }) => {

if (graphQLErrors) {
  for (let err of graphQLErrors) {
    
          switch (err.extensions.code) {
    
            case "UNAUTHENTICATED":
    
              const oldHeaders = operation.getContext().headers;

              const tokens = getNewToken();

              tokens.then(accessToken => {
                operation.setContext({
      
                  headers: {
      
                    ...oldHeaders,
      
                    Authorization: accessToken,
      
                  },
      
                });

                return forward(operation);

              })
    
    
          }
    
        }
}

})

const httpLink = new HttpLink({ uri: 'http://localhost:4000/graphql' });

export const client = new ApolloClient({
link: interceptors.concat(httpLink),
cache: new InMemoryCache(),
ssrMode: true,
});

function MyApp({ Component, pageProps }) {
return (


<Component {...pageProps} />


)
}

export default MyApp

@jpvajda
Copy link
Contributor

jpvajda commented Aug 5, 2022

@ShourovRoy Thanks for posting this issue! What's your expected outcome here? It would be helpful to see a reproduction of this issue. You can create a reproduction using https://github.com/apollographql/react-apollo-error-template and link to it here. If you prefer an in-browser way to create reproduction, try: https://codesandbox.io/s/github/apollographql/react-apollo-error-template

@raym
Copy link

raym commented Oct 25, 2022

@ShourovRoy you are not awaiting for your getNewToken which is async.

Even so, that would require you to change the function you're passing to onError to an async function, but onError does not await on the function you pass in, so your approach will not work.

You can see here, there is no await on the errorHandler function you pass to onError:
https://github.com/apollographql/apollo-client/blob/main/src/link/error/index.ts#L38

I believe you need to create and return an Observable.

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

No branches or pull requests

3 participants