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.

(persisted-fetch) - Don't send persisted queries if the hash operation fails (#934)

* Don't send persisted queries if the hashing operation fails

* Add a test case

* Fix typo

* Add a changeset

* Move the conditional up

authored by

Loren Riesenfeld and committed by
GitHub
1651fd51 8033b9bc

+57 -11
+5
.changeset/wet-cows-melt.md
··· 1 + --- 2 + '@urql/exchange-persisted-fetch': patch 3 + --- 4 + 5 + Stops sending a persisted query if the hashing function fails
+37
exchanges/persisted-fetch/src/persistedFetchExchange.test.ts
··· 235 235 `; 236 236 expect(hashFn).toBeCalledWith(queryString, queryOperation.query); 237 237 }); 238 + 239 + it('falls back to a non-persisted query if the hash is falsy', async () => { 240 + const expected = { 241 + data: { 242 + test: true, 243 + }, 244 + }; 245 + 246 + fetch.mockResolvedValueOnce({ 247 + json: () => expected, 248 + }); 249 + 250 + const hashFn = jest.fn(() => Promise.resolve('')); 251 + 252 + await pipe( 253 + fromValue(queryOperation), 254 + persistedFetchExchange({ generateHash: hashFn })(exchangeArgs), 255 + toPromise 256 + ); 257 + 258 + expect(fetch).toHaveBeenCalledTimes(1); 259 + 260 + const body = JSON.parse(fetch.mock.calls[0][1].body); 261 + 262 + expect(body).toMatchObject({ 263 + query: 264 + 'query getUser($name: String) {\n' + 265 + ' user(name: $name) {\n' + 266 + ' id\n' + 267 + ' firstName\n' + 268 + ' lastName\n' + 269 + ' }\n' + 270 + '}\n', 271 + operationName: 'getUser', 272 + variables: { name: 'Clara' }, 273 + }); 274 + });
+15 -11
exchanges/persisted-fetch/src/persistedFetchExchange.ts
··· 71 71 // Hash the given GraphQL query 72 72 fromPromise(hashFn(query, operation.query)), 73 73 mergeMap(sha256Hash => { 74 - // Attach SHA256 hash and remove query from body 75 - body.query = undefined; 76 - body.extensions = { 77 - persistedQuery: { 78 - version: 1, 79 - sha256Hash, 80 - }, 81 - }; 82 - 74 + // if the hashing operation was successful, add the persisted query extension 75 + if (sha256Hash) { 76 + // Attach SHA256 hash and remove query from body 77 + body.query = undefined; 78 + body.extensions = { 79 + persistedQuery: { 80 + version: 1, 81 + sha256Hash, 82 + }, 83 + }; 84 + } 83 85 return makePersistedFetchSource( 84 86 operation, 85 87 body, 86 88 dispatchDebug, 87 - !!(options as PersistedFetchExchangeOptions) 88 - .preferGetForPersistedQueries 89 + !!( 90 + (options as PersistedFetchExchangeOptions) 91 + .preferGetForPersistedQueries && sha256Hash 92 + ) 89 93 ); 90 94 }), 91 95 mergeMap(result => {