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