feat(apollo): 接口报错时,界面提供反馈。

This commit is contained in:
Ivan Li 2021-03-06 23:05:44 +08:00
parent 699a7cecd3
commit 846b58c49b
2 changed files with 27 additions and 4 deletions

View File

@ -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 (

View File

@ -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'
})
)
});
}