Skip to main content

renew token on graphQL error - apollo client with Firebase authentication

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



 https://able.bio/AnasT/apollo-graphql-async-access-token-refresh--470t1c8


apollo client with Firebase authentication

https://dgraph.io/docs/graphql/todo-app-tutorial/todo-firebase-jwt/


Concurrent Requests

https://able.bio/AnasT/apollo-graphql-async-access-token-refresh--470t1c8
const errorLink = onError( ({ graphQLErrors, networkError, operation, forward }) => { if (graphQLErrors) { for (let err of graphQLErrors) { switch (err.extensions.code) { case 'UNAUTHENTICATED': // error code is set to UNAUTHENTICATED // when AuthenticationError thrown in resolver let forward$; if (!isRefreshing) { isRefreshing = true; forward$ = fromPromise( getNewToken() .then(({ accessToken, refreshToken }) => { // Store the new tokens for your auth link resolvePendingRequests(); return accessToken; }) .catch(error => { pendingRequests = []; // Handle token refresh errors e.g clear stored tokens, redirect to login, ... return; }) .finally(() => { isRefreshing = false; }) ).filter(value => Boolean(value)); } else { // Will only emit once the Promise is resolved forward$ = fromPromise( new Promise(resolve => { pendingRequests.push(() => resolve()); }) ); } return forward$.flatMap(() => forward(operation)); } } } if (networkError) { console.log(`[Network error]: ${networkError}`); // if you would also like to retry automatically on // network errors, we recommend that you use // apollo-link-retry } } ); const apolloLink = concat(errorLink, concat(authLink, httpLink));

Comments

Popular posts from this blog

Online tool to remove background of images

https://www.remove.bg/upload

React way to use D3 - ( Python as API )

The Inspiration Andmap is trying to build new BI product, the tech stacks would be D3 + ReactJS as data visualization frontend, Web API, and Python provides data. As with most new projects, it didn’t take long to figure out the major roadblocks. D3.js is a JavaScript library for manipulating documents based on data. D3 helps you bring data to life using HTML, SVG, and CSS. D3’s emphasis on web standards gives you the full capabilities of modern browsers without tying yourself to a proprietary framework, combining powerful visualization components and a data-driven approach to DOM manipulation. In one of our initial discovery sessions, we realized that our application needs to be able to adapt D3 library in ReactJS way. More than that, we prefer utilizing new feature of React - Hooks. Finally, we build  component for each chart and Helpers file for drawing each chart. Here is sample for map chart: index.js: map.js - map chart container - using useEf...