From 846b58c49b876f826ec3d59cff2ac0e00b8e555d Mon Sep 17 00:00:00 2001 From: Ivan Li Date: Sat, 6 Mar 2021 23:05:44 +0800 Subject: [PATCH] =?UTF-8?q?feat(apollo):=20=E6=8E=A5=E5=8F=A3=E6=8A=A5?= =?UTF-8?q?=E9=94=99=E6=97=B6=EF=BC=8C=E7=95=8C=E9=9D=A2=E6=8F=90=E4=BE=9B?= =?UTF-8?q?=E5=8F=8D=E9=A6=88=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/app.tsx | 6 ++---- src/units/apollo-client.ts | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 src/units/apollo-client.ts 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' + }) + ) + }); +}