diff --git a/src/components/app.tsx b/src/components/app.tsx index d0fb9ba..737d761 100644 --- a/src/components/app.tsx +++ b/src/components/app.tsx @@ -7,11 +7,9 @@ import { useObserver } from 'mobx-react'; import { appStore } from '../app.store'; import Router, { Route } from 'preact-router'; import { ProjectDetails } from '../routes/projects/project-details'; +import { createApolloClient } from '../units/apollo-client'; -const client = new ApolloClient({ - uri: '/api/graphql', - cache: new InMemoryCache() -}); +const client = createApolloClient(); const App: FunctionalComponent = () => { return ( diff --git a/src/units/apollo-client.ts b/src/units/apollo-client.ts new file mode 100644 index 0000000..61c32ae --- /dev/null +++ b/src/units/apollo-client.ts @@ -0,0 +1,25 @@ +import { Message } from './../components/commons/message/index'; +import { ApolloClient, concat, HttpLink, InMemoryCache } from '@apollo/client'; +import { onError } from '@apollo/client/link/error'; +export function createApolloClient() { + const errorLink = onError(({ graphQLErrors, networkError }) => { + if (graphQLErrors) { + console.error(graphQLErrors); + graphQLErrors.forEach(({ message, locations, path }) => { + console.log( + `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}` + ); + }); + } + if (networkError) console.log(`[Network error]: ${networkError}`); + }); + return new ApolloClient({ + cache: new InMemoryCache(), + link: concat( + errorLink, + new HttpLink({ + uri: '/api/graphql' + }) + ) + }); +}