webpack-bootstrap-ui-kit/src/js/ajax/apollo/init.js

107 lines
3.0 KiB
JavaScript
Raw Normal View History

2022-05-03 20:50:57 +02:00
import Events from '../../_events'
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
import { cache } from './cache'
2021-08-09 18:04:09 +02:00
import {
from,
ApolloClient,
HttpLink,
ApolloLink,
concat,
2022-05-03 20:50:57 +02:00
} from '@apollo/client'
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
import { onError } from '@apollo/client/link/error'
const NAME = 'appolo'
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
const API_META = document.querySelector('meta[name="api_url"]')
2021-08-18 20:51:15 +02:00
const API_URL = API_META
2022-05-03 20:50:57 +02:00
? API_META.getAttribute('content')
: `${window.location.protocol}//${window.location.host}/graphql`
2021-08-09 18:04:09 +02:00
const authMiddleware = new ApolloLink((operation, forward) => {
// add the authorization to the headers
operation.setContext({
headers: {
apikey: `${GRAPHQL_API_KEY}`,
},
2022-05-03 20:50:57 +02:00
})
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
return forward(operation)
})
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
console.info(`%cAPI: ${API_URL}`, 'color:green;font-size:10px')
2021-08-09 18:04:09 +02:00
const link = from([
authMiddleware,
new ApolloLink((operation, forward) => {
operation.setContext({
start: new Date(),
2022-05-03 20:50:57 +02:00
})
return forward(operation)
2021-08-09 18:04:09 +02:00
}),
2021-08-18 20:51:15 +02:00
onError(({ operation, response, graphQLErrors, networkError, forward }) => {
2022-05-03 20:50:57 +02:00
if (operation.operationName === 'IgnoreErrorsQuery') {
console.error(`${NAME}: IgnoreErrorsQuery`)
response.errors = null
return
2021-08-09 18:04:09 +02:00
}
if (graphQLErrors) {
2021-08-18 20:51:15 +02:00
graphQLErrors.forEach(({ message, locations, path }) =>
2021-08-09 18:04:09 +02:00
console.error(
2021-08-18 20:51:15 +02:00
`${NAME}: [GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`
)
2022-05-03 20:50:57 +02:00
)
2021-08-09 18:04:09 +02:00
}
if (networkError) {
2022-05-03 20:50:57 +02:00
/* let msg = '';
2021-08-09 18:04:09 +02:00
switch (networkError.statusCode) {
case 404:
msg = 'Not Found.';
break;
case 500:
msg = 'Server issue, please try again latter.';
break;
default:
msg = 'Something went wrong.';
break;
2022-05-03 20:50:57 +02:00
} */
console.error(`${NAME}: [Network error] ${networkError.statusCode}`)
2021-08-09 18:04:09 +02:00
}
2022-05-03 20:50:57 +02:00
console.error(`${NAME}: [APOLLO_ERROR]`)
window.dispatchEvent(new Event(Events.APOLLO_ERROR))
2021-08-09 18:04:09 +02:00
}),
new ApolloLink((operation, forward) => {
return forward(operation).map((data) => {
// data from a previous link
2022-05-03 20:50:57 +02:00
const time = new Date() - operation.getContext().start
2021-08-09 18:04:09 +02:00
console.log(
2021-08-18 20:51:15 +02:00
`${NAME}: operation ${operation.operationName} took ${time} ms to complete`
2022-05-03 20:50:57 +02:00
)
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
window.dispatchEvent(new Event(Events.ONLINE))
return data
})
2021-08-09 18:04:09 +02:00
}),
new HttpLink({
uri: API_URL,
// Use explicit `window.fetch` so tha outgoing requests
// are captured and deferred until the Service Worker is ready.
fetch: (...args) => fetch(...args),
2022-05-03 20:50:57 +02:00
credentials: 'same-origin', // 'include',
connectToDevTools: process.env.NODE_ENV === 'development',
2021-08-09 18:04:09 +02:00
}),
2022-05-03 20:50:57 +02:00
])
2021-08-09 18:04:09 +02:00
// Isolate Apollo client so it could be reused
// in both application runtime and tests.
const client = new ApolloClient({
cache,
link,
2022-05-03 20:50:57 +02:00
})
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
export { client }