-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Set session cookie to expire after 4 hours * Handle unauthenticated GraphQL errors
- Loading branch information
Suzanne Rozier
authored
Dec 10, 2021
1 parent
02faafd
commit 14ccee4
Showing
3 changed files
with
37 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,34 @@ | ||
import { ApolloClient, InMemoryCache } from '@apollo/client' | ||
import { ApolloClient, InMemoryCache, HttpLink, from } from '@apollo/client' | ||
import { onError } from '@apollo/client/link/error' | ||
|
||
const httpLink = new HttpLink({ | ||
uri: `/api/graphql`, | ||
}) | ||
|
||
const errorLink = onError(({ graphQLErrors, networkError }) => { | ||
if (graphQLErrors) | ||
graphQLErrors.forEach(({ message, locations, path, extensions }) => { | ||
// TODO - log error | ||
// eslint-disable-next-line no-console | ||
console.error( | ||
`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}` | ||
) | ||
|
||
// Check for auth | ||
if (extensions.code === 'UNAUTHENTICATED') { | ||
// Redirect | ||
window.location.href = '/login' | ||
} | ||
}) | ||
|
||
if (networkError) { | ||
// TODO - log error | ||
// eslint-disable-next-line no-console | ||
console.error(`[Network error]: ${networkError}`) | ||
} | ||
}) | ||
|
||
export const client = new ApolloClient({ | ||
uri: '/api/graphql', | ||
link: from([errorLink, httpLink]), | ||
cache: new InMemoryCache(), | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters