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

Use Apollo to fetch the fresh token #12

Open
rvetrsek opened this issue Sep 13, 2019 · 6 comments
Open

Use Apollo to fetch the fresh token #12

rvetrsek opened this issue Sep 13, 2019 · 6 comments

Comments

@rvetrsek
Copy link

Hello,

I have a GraphQL mutation to refresh the token. Can I somehow use the mutation in the fetchAccessToken function?

@jampack
Copy link

jampack commented Sep 15, 2019

@obsessen did u get through with it?

@rvetrsek
Copy link
Author

rvetrsek commented Sep 16, 2019

@akkhan20 unfortunately no. I'm still new to GraphQL and Apollo, so some help would be appreciated.

I think I know what my problem is. I'm trying to call an Apollo mutation in the fetchAccessToken function but the tokenRefreshLink catches this request as well and adds it to the "query waiting list"... Is there a way I can make this work?

@bartholomej
Copy link

@obsessen @da3monHunter Have you tried something like this?

 fetchAccessToken: async () => {
  const resp = await fetch(environment.graphqlRemote, {
    method: 'POST',
    headers: {
      Authorization: `JWT ${currentAccessToken}`,
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      query: `mutation RefreshToken { refreshToken(refreshToken: "${refreshToken}") { token refreshToken } }`
    })
  });
  return resp.json();
},
handleResponse: (operation, accessTokenField) => (response: RefreshReponse) => {
  console.log('handleResponse', response);
}

@jampack
Copy link

jampack commented Oct 20, 2019

const refreshTokenMutation = JSON.stringify({
  query: `mutation{
  refreshToken(data: {refresh_token: "${getRefreshToken()}"}){
    access_token
    refresh_token
    expires_in
  }
}`,
});
fetchAccessToken: async () => {
    const resp = await fetch(URI, {
      headers: { 'content-type': 'application/json' },
      method: 'POST',
      body: refreshTokenMutation,
    });
    return resp.json();
  },
  handleResponse: (operation, accessTokenField) => (response) => {
    console.log(response);
  },

@subotkevic
Copy link

Accidentally found this.

I myself end up with the similar solution to refresh the access token using axios, with mutation query inside the axios request.

Haven't found the solution to use mutation inside ApolloClient onError link...

@sk8Guerra
Copy link

sk8Guerra commented Jul 24, 2020

Im getting a status 500 when fetchAccessToken gets executed...

 body: (...)
 bodyUsed: false
 headers: Headers {}
 ok: false
 redirected: false
 status: 500
 statusText: "Internal Server Error"
 type: "cors"
 url: "http://localhost:3000/graphql"

Any idea what could be happening?

fetch('http://localhost:3000/graphql', {
   method: 'POST',
   headers: { 'content-type': 'application/json' },
   credentials: 'include',
   body: JSON.stringify({ query: '{ refreshToken { accessToken } }' })
});

I'm just using a normal fetch

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

No branches or pull requests

5 participants