Replace Table with responsive Card. Add refresh of token and retry on 401 errors

This commit is contained in:
2019-01-18 14:03:58 +01:00
parent d4f432f422
commit 86486795f6
11 changed files with 230 additions and 248 deletions
+43 -8
View File
@@ -1,13 +1,48 @@
const { createApolloFetch } = require('apollo-fetch');
import { execute, makePromise, ApolloLink } from 'apollo-link';
import { HttpLink } from 'apollo-link-http';
import gql from 'graphql-tag';
const { includeCredentials } = require('./middleware');
import { onError } from 'apollo-link-error';
import { default as webAuth} from '../auth';
const defaultGraphUri = '/graph/';
const httpLink = new HttpLink({ uri: defaultGraphUri, fetch: includeCredentials, credentials: 'same-origin' });
const errorLink = onError(({ graphQLErrors, networkError, operation, forward }) => {
if (graphQLErrors) {
console.log('GraphQL errors:', graphQLErrors);
// for (let err of graphQLErrors) {
// switch (err.extensions.code) {
// case 'UNAUTHENTICATED':
// // error code is set to UNAUTHENTICATED
// // when AuthenticationError thrown in resolver
//
// // modify the operation context with a new token
// }
// }
}
if (networkError) {
if (networkError.statusCode === 401) {
webAuth.checkSession((response) => {
const oldHeaders = operation.getContext().headers;
operation.setContext({
headers: {
...oldHeaders,
authorization: webAuth.idToken(),
},
});
return forward(operation);
}, (err) => {
console.log(err);
});
}
}
}
);
export const createQuery = (tokenFn, query, variables) => { // eslint-disable-line
const apollo = createApolloFetch({ uri: defaultGraphUri });
apollo.use(includeCredentials(tokenFn));
// apollo.useAfter(trackErrors);
return apollo({ query, variables });
export const createQuery = (query, variables) => { // eslint-disable-line
const operation = {
query: gql(query),
variables: variables
};
return makePromise(execute(ApolloLink.from([errorLink, httpLink]), operation));
};