Mirror: The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix(core/graphcache): Add missing hasNext/stale flags to cached results (#3059)

authored by

Phil Pluckthun and committed by
GitHub
a6d79867 b9d46493

+42 -17
+6
.changeset/polite-penguins-notice.md
··· 1 + --- 2 + '@urql/exchange-graphcache': patch 3 + '@urql/core': patch 4 + --- 5 + 6 + Add missing `hasNext` and `stale` passthroughs on caching exchanges.
+9 -3
exchanges/graphcache/src/cacheExchange.ts
··· 195 195 result: OperationResult, 196 196 pendingOperations: Operations 197 197 ): OperationResult => { 198 - const { error, extensions } = result; 199 - 200 198 // Retrieve the original operation to remove changes made by formatDocument 201 199 const originalOperation = operations.get(result.operation.key); 202 200 const operation = originalOperation ··· 261 259 updateDependencies(result.operation, queryDependencies); 262 260 } 263 261 264 - return { data, error, extensions, operation }; 262 + return { 263 + operation, 264 + data, 265 + error: result.error, 266 + extensions: result.extensions, 267 + hasNext: result.hasNext, 268 + stale: result.stale, 269 + }; 265 270 }; 266 271 267 272 return ops$ => { ··· 339 344 error: res.error, 340 345 extensions: res.extensions, 341 346 stale: shouldReexecute || isPartial, 347 + hasNext: false, 342 348 }; 343 349 344 350 if (!shouldReexecute) {
+1
exchanges/graphcache/src/offlineExchange.test.ts
··· 226 226 error: undefined, 227 227 extensions: undefined, 228 228 operation: expect.any(Object), 229 + hasNext: false, 229 230 stale: false, 230 231 }); 231 232
+6 -1
packages/core/src/exchanges/ssr.test.ts
··· 198 198 expect(output).not.toHaveBeenCalled(); 199 199 expect(onPush).toHaveBeenCalledWith({ 200 200 ...queryResponse, 201 + stale: false, 202 + hasNext: false, 201 203 operation: { 202 204 ...queryResponse.operation, 203 205 context: { ··· 234 236 expect(output).toHaveBeenCalledTimes(1); 235 237 expect(onPush).toHaveBeenCalledTimes(2); 236 238 expect(onPush.mock.calls[1][0]).toEqual({ 239 + ...queryResponse, 237 240 hasNext: true, 238 - ...queryResponse, 241 + stale: false, 239 242 operation: { 240 243 ...queryResponse.operation, 241 244 context: { ··· 267 270 expect(Object.keys(ssr.extractData()).length).toBe(0); 268 271 expect(onPush).toHaveBeenCalledWith({ 269 272 ...queryResponse, 273 + stale: false, 274 + hasNext: false, 270 275 operation: { 271 276 ...queryResponse.operation, 272 277 context: {
+20 -13
packages/core/src/exchanges/ssr.ts
··· 105 105 106 106 /** Serialize an OperationResult to plain JSON */ 107 107 const serializeResult = ( 108 - { hasNext, data, extensions, error }: OperationResult, 108 + result: OperationResult, 109 109 includeExtensions: boolean 110 110 ): SerializedResult => { 111 - const result: SerializedResult = {}; 112 - if (data !== undefined) result.data = JSON.stringify(data); 113 - if (includeExtensions && extensions !== undefined) { 114 - result.extensions = JSON.stringify(extensions); 111 + const serialized: SerializedResult = { 112 + data: JSON.stringify(result.data), 113 + hasNext: result.hasNext, 114 + }; 115 + 116 + if (result.data !== undefined) { 117 + serialized.data = JSON.stringify(result.data); 118 + } 119 + 120 + if (includeExtensions && result.extensions !== undefined) { 121 + serialized.extensions = JSON.stringify(result.extensions); 115 122 } 116 - if (hasNext) result.hasNext = true; 117 123 118 - if (error) { 119 - result.error = { 120 - graphQLErrors: error.graphQLErrors.map(error => { 124 + if (result.error) { 125 + serialized.error = { 126 + graphQLErrors: result.error.graphQLErrors.map(error => { 121 127 if (!error.path && !error.extensions) return error.message; 122 128 123 129 return { ··· 128 134 }), 129 135 }; 130 136 131 - if (error.networkError) { 132 - result.error.networkError = '' + error.networkError; 137 + if (result.error.networkError) { 138 + serialized.error.networkError = '' + result.error.networkError; 133 139 } 134 140 } 135 141 136 - return result; 142 + return serialized; 137 143 }; 138 144 139 145 /** Deserialize plain JSON to an OperationResult ··· 158 164 graphQLErrors: result.error.graphQLErrors, 159 165 }) 160 166 : undefined, 161 - hasNext: result.hasNext, 167 + stale: false, 168 + hasNext: !!result.hasNext, 162 169 }); 163 170 164 171 const revalidated = new Set<number>();