4
4
GraphQLObjectType ,
5
5
GraphQLID ,
6
6
GraphQLString ,
7
+ GraphQLInt ,
7
8
GraphQLList ,
8
9
} from 'graphql' ;
9
10
const PersonType = new GraphQLObjectType ( {
@@ -25,7 +26,10 @@ const QueryType = new GraphQLObjectType({
25
26
fields : {
26
27
people : {
27
28
type : new GraphQLList ( PersonType ) ,
28
- resolve : ( ) => peopleData ,
29
+ args : {
30
+ page : { type : GraphQLInt } ,
31
+ } ,
32
+ resolve : ( _ , { page } ) => page === 1 ? peopleData : new Error ( 'Test Error' ) ,
29
33
} ,
30
34
} ,
31
35
} ) ;
@@ -82,57 +86,37 @@ const link = new ApolloLink(operation => {
82
86
} ) ;
83
87
84
88
/*** APP ***/
85
- import React , { useState } from "react" ;
89
+ import React from "react" ;
86
90
import { render } from "react-dom" ;
87
91
import {
88
92
ApolloClient ,
89
93
ApolloProvider ,
90
94
InMemoryCache ,
91
95
gql ,
92
96
useQuery ,
93
- useMutation ,
94
97
} from "@apollo/client" ;
95
98
import "./index.css" ;
96
99
97
100
const ALL_PEOPLE = gql `
98
- query AllPeople {
99
- people {
101
+ query AllPeople($page: Int!) {
102
+ people(page: $page) {
100
103
id
101
104
name
102
105
}
103
106
}
104
107
` ;
105
108
106
- const ADD_PERSON = gql `
107
- mutation AddPerson($name: String) {
108
- addPerson(name: $name) {
109
- id
110
- name
111
- }
112
- }
113
- ` ;
114
109
115
110
function App ( ) {
116
- const [ name , setName ] = useState ( '' ) ;
117
111
const {
118
112
loading,
119
113
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' ) ;
136
120
} ,
137
121
} ) ;
138
122
@@ -142,32 +126,20 @@ function App() {
142
126
< p >
143
127
This application can be used to demonstrate an error in Apollo Client.
144
128
</ 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 >
162
129
< h2 > Names</ h2 >
163
130
{ loading ? (
164
131
< p > Loading…</ p >
165
132
) : (
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
+ </ >
171
143
) }
172
144
</ main >
173
145
) ;
0 commit comments