feat: auto userdata fetch

fix: gql directives and overall minor fixes
This commit is contained in:
Sérgio Salgado 2021-04-07 16:11:06 +01:00 committed by Josh Harvey
parent 3f6c0e6037
commit 9fa97725ec
22 changed files with 94 additions and 127 deletions

View file

@ -4,7 +4,6 @@ import { ApolloClient } from 'apollo-client'
import { ApolloLink } from 'apollo-link'
import { onError } from 'apollo-link-error'
import { HttpLink } from 'apollo-link-http'
import * as R from 'ramda'
import React, { useContext } from 'react'
import { useHistory, useLocation } from 'react-router-dom'
@ -19,12 +18,10 @@ const getClient = (history, location, setUserData) =>
onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors)
graphQLErrors.forEach(({ message, locations, path, extensions }) => {
handle(
{ message, locations, path, extensions },
history,
location,
setUserData
)
if (extensions?.code === 'UNAUTHENTICATED') {
setUserData(null)
if (location.pathname !== '/login') history.push('/login')
}
console.log(
`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`
)
@ -52,22 +49,6 @@ const getClient = (history, location, setUserData) =>
}
})
const handle = (apolloError, ...args) => {
const handler = {
UNAUTHENTICATED: (...args) => {
const history = args[0]
const location = args[1]
const setUserData = args[2]
setUserData(null)
if (location.pathname !== '/login') history.push('/login')
}
}
if (!R.has(apolloError.extensions?.code, handler)) return apolloError
return handler[apolloError.extensions?.code](...args)
}
const Provider = ({ children }) => {
const history = useHistory()
const location = useLocation()