Skip to content

Commit fa06945

Browse files
committed
04-Apollo Client Error Handling in React
1 parent 44ceb04 commit fa06945

File tree

5 files changed

+46
-2
lines changed

5 files changed

+46
-2
lines changed

package-lock.json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
"dependencies": {
66
"apollo-cache-inmemory": "^1.3.6",
77
"apollo-client": "^2.4.3",
8+
"apollo-link": "^1.2.3",
9+
"apollo-link-error": "^1.1.1",
810
"apollo-link-http": "^1.5.5",
911
"graphql": "^14.0.2",
1012
"graphql-tag": "^2.10.0",

src/Error/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from 'react';
2+
3+
import './style.css';
4+
5+
const ErrorMessage = ({ error }) => (
6+
<div className="ErrorMessage">
7+
<small>{error.toString()}</small>
8+
</div>
9+
);
10+
11+
export default ErrorMessage;

src/Profile/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Query } from 'react-apollo';
44

55
import RepositoryList from '../Repository';
66
import Loading from '../Loading';
7+
import ErrorMessage from '../Error';
78

89
const GET_REPOSITORIES_OF_CURRENT_USER = gql`
910
{
@@ -42,7 +43,11 @@ const GET_REPOSITORIES_OF_CURRENT_USER = gql`
4243

4344
const Profile = () => (
4445
<Query query={GET_REPOSITORIES_OF_CURRENT_USER}>
45-
{({ data, loading }) => {
46+
{({ data, loading, error }) => {
47+
if (error) {
48+
return <ErrorMessage error={error} />;
49+
}
50+
4651
const { viewer } = data;
4752

4853
if (loading || !viewer) {

src/index.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import React from 'react';
22
import ReactDOM from 'react-dom';
33
import { ApolloProvider } from 'react-apollo';
44
import { ApolloClient } from 'apollo-client';
5+
import { ApolloLink } from 'apollo-link';
56
import { HttpLink } from 'apollo-link-http';
7+
import { onError } from 'apollo-link-error';
68
import { InMemoryCache } from 'apollo-cache-inmemory';
79

810
import * as serviceWorker from './serviceWorker';
@@ -21,10 +23,26 @@ const httpLink = new HttpLink({
2123
},
2224
});
2325

26+
const errorLink = onError(({ graphQLErrors, networkError }) => {
27+
if (graphQLErrors) {
28+
graphQLErrors.map(({ message, locations, path }) =>
29+
console.log(
30+
`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`,
31+
),
32+
);
33+
}
34+
35+
if (networkError) {
36+
console.log(`[Network error]: ${networkError}`);
37+
}
38+
});
39+
40+
const link = ApolloLink.from([errorLink, httpLink]);
41+
2442
const cache = new InMemoryCache();
2543

2644
const client = new ApolloClient({
27-
link: httpLink,
45+
link,
2846
cache,
2947
});
3048

0 commit comments

Comments
 (0)