Skip to content

Commit 45f8b3d

Browse files
committed
1 parent 0ec9503 commit 45f8b3d

File tree

1 file changed

+24
-52
lines changed

1 file changed

+24
-52
lines changed

src/index.jsx

+24-52
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
GraphQLObjectType,
55
GraphQLID,
66
GraphQLString,
7+
GraphQLInt,
78
GraphQLList,
89
} from 'graphql';
910
const PersonType = new GraphQLObjectType({
@@ -25,7 +26,10 @@ const QueryType = new GraphQLObjectType({
2526
fields: {
2627
people: {
2728
type: new GraphQLList(PersonType),
28-
resolve: () => peopleData,
29+
args: {
30+
page: { type: GraphQLInt },
31+
},
32+
resolve: (_, { page }) => page === 1 ? peopleData : new Error('Test Error'),
2933
},
3034
},
3135
});
@@ -82,57 +86,37 @@ const link = new ApolloLink(operation => {
8286
});
8387

8488
/*** APP ***/
85-
import React, { useState } from "react";
89+
import React from "react";
8690
import { render } from "react-dom";
8791
import {
8892
ApolloClient,
8993
ApolloProvider,
9094
InMemoryCache,
9195
gql,
9296
useQuery,
93-
useMutation,
9497
} from "@apollo/client";
9598
import "./index.css";
9699

97100
const ALL_PEOPLE = gql`
98-
query AllPeople {
99-
people {
101+
query AllPeople($page: Int!) {
102+
people(page: $page) {
100103
id
101104
name
102105
}
103106
}
104107
`;
105108

106-
const ADD_PERSON = gql`
107-
mutation AddPerson($name: String) {
108-
addPerson(name: $name) {
109-
id
110-
name
111-
}
112-
}
113-
`;
114109

115110
function App() {
116-
const [name, setName] = useState('');
117111
const {
118112
loading,
119113
data,
120-
} = useQuery(ALL_PEOPLE);
121-
122-
const [addPerson] = useMutation(ADD_PERSON, {
123-
update: (cache, { data: { addPerson: addPersonData } }) => {
124-
const peopleResult = cache.readQuery({ query: ALL_PEOPLE });
125-
126-
cache.writeQuery({
127-
query: ALL_PEOPLE,
128-
data: {
129-
...peopleResult,
130-
people: [
131-
...peopleResult.people,
132-
addPersonData,
133-
],
134-
},
135-
});
114+
fetchMore,
115+
} = useQuery(ALL_PEOPLE, {
116+
errorPolicy: 'none',
117+
variables: { page: 1 },
118+
onError: () => {
119+
console.log('error correctly passed to onError callback');
136120
},
137121
});
138122

@@ -142,32 +126,20 @@ function App() {
142126
<p>
143127
This application can be used to demonstrate an error in Apollo Client.
144128
</p>
145-
<div className="add-person">
146-
<label htmlFor="name">Name</label>
147-
<input
148-
type="text"
149-
name="name"
150-
value={name}
151-
onChange={evt => setName(evt.target.value)}
152-
/>
153-
<button
154-
onClick={() => {
155-
addPerson({ variables: { name } });
156-
setName('');
157-
}}
158-
>
159-
Add person
160-
</button>
161-
</div>
162129
<h2>Names</h2>
163130
{loading ? (
164131
<p>Loading…</p>
165132
) : (
166-
<ul>
167-
{data?.people.map(person => (
168-
<li key={person.id}>{person.name}</li>
169-
))}
170-
</ul>
133+
<>
134+
<ul>
135+
{data?.people.map(person => (
136+
<li key={person.id}>{person.name}</li>
137+
))}
138+
</ul>
139+
<button onClick={() => fetchMore({ variables: { page: 2 } }).catch(() => console.log('error thrown from fetchMore instead of passed to onError'))}>
140+
Fetch More
141+
</button>
142+
</>
171143
)}
172144
</main>
173145
);

0 commit comments

Comments
 (0)