/
index.jsx
184 lines (168 loc) · 3.62 KB
/
index.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*** SCHEMA ***/
import {
GraphQLSchema,
GraphQLObjectType,
GraphQLID,
GraphQLString,
GraphQLList,
} from 'graphql';
const PersonType = new GraphQLObjectType({
name: 'Person',
fields: {
id: { type: GraphQLID },
name: { type: GraphQLString },
},
});
const peopleData = [
{ id: 1, name: 'John Smith' },
{ id: 2, name: 'Sara Smith' },
{ id: 3, name: 'Budd Deey' },
];
const QueryType = new GraphQLObjectType({
name: 'Query',
fields: {
people: {
type: new GraphQLList(PersonType),
resolve: () => peopleData,
},
},
});
const MutationType = new GraphQLObjectType({
name: 'Mutation',
fields: {
addPerson: {
type: PersonType,
args: {
name: { type: GraphQLString },
},
resolve: function (_, { name }) {
const person = {
id: peopleData[peopleData.length - 1].id + 1,
name,
};
peopleData.push(person);
return person;
}
},
},
});
const schema = new GraphQLSchema({ query: QueryType, mutation: MutationType });
/*** LINK ***/
import { graphql, print } from "graphql";
import { ApolloLink, Observable } from "@apollo/client";
function delay(wait) {
return new Promise(resolve => setTimeout(resolve, wait));
}
const link = new ApolloLink(operation => {
return new Observable(async observer => {
const { query, operationName, variables } = operation;
await delay(300);
try {
const result = await graphql({
schema,
source: print(query),
variableValues: variables,
operationName,
});
observer.next(result);
observer.complete();
} catch (err) {
observer.error(err);
}
});
});
/*** APP ***/
import React, { useState } from "react";
import { render } from "react-dom";
import {
ApolloClient,
ApolloProvider,
InMemoryCache,
gql,
useQuery,
useMutation,
} from "@apollo/client";
import "./index.css";
const ALL_PEOPLE = gql`
query AllPeople {
people {
id
name
}
}
`;
const ADD_PERSON = gql`
mutation AddPerson($name: String) {
addPerson(name: $name) {
id
name
}
}
`;
function App() {
const [name, setName] = useState('');
const {
loading,
data,
} = useQuery(ALL_PEOPLE);
const [addPerson] = useMutation(ADD_PERSON, {
update: (cache, { data: { addPerson: addPersonData } }) => {
const peopleResult = cache.readQuery({ query: ALL_PEOPLE });
cache.writeQuery({
query: ALL_PEOPLE,
data: {
...peopleResult,
people: [
...peopleResult.people,
addPersonData,
],
},
});
},
});
return (
<main>
<h1>Apollo Client Issue Reproduction</h1>
<p>
This application can be used to demonstrate an error in Apollo Client.
</p>
<div className="add-person">
<label htmlFor="name">Name</label>
<input
type="text"
name="name"
value={name}
onChange={evt => setName(evt.target.value)}
/>
<button
onClick={() => {
addPerson({ variables: { name } });
setName('');
}}
>
Add person
</button>
</div>
<h2>Names</h2>
{loading ? (
<p>Loading…</p>
) : (
<ul>
{data?.people.map(person => (
<li key={person.id}>{person.name}</li>
))}
</ul>
)}
</main>
);
}
const client = new ApolloClient({
cache: new InMemoryCache(),
link
});
render(
<ApolloProvider client={client}>
<App />
</ApolloProvider>,
document.getElementById("root")
);