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) - Add generateHash option to the persistedFetchExchange (#870)

* Add support for a custom hash function in @exchange/persisted-fetch

* Add a changeset

* Update the readme

* Clean up the test

* Update the changeset language

authored by

Loren Riesenfeld and committed by
GitHub
f80c96d7 9e72bc90

+49 -2
+5
.changeset/green-lies-help.md
··· 1 + --- 2 + '@urql/exchange-persisted-fetch': minor 3 + --- 4 + 5 + Adds support for custom hash functions by adding a `generateHash` option to the exchange.
+8 -1
exchanges/persisted-fetch/README.md
··· 25 25 exchanges: [ 26 26 dedupExchange, 27 27 cacheExchange, 28 - persistedFetchExchange, 28 + persistedFetchExchange({ 29 + /* optional config */ 30 + }), 29 31 fetchExchange 30 32 ], 31 33 }); 32 34 ``` 35 + 36 + The `persistedQueryExchange` supports two configuration options: 37 + 38 + - `preferGetForPersistedQueries`: Use `GET` for fetches with persisted queries 39 + - `generateHash`: A function that takes a GraphQL query and returns the hashed result. This defaults to the `window.crypto` API in the browser and the `crypto` module in node. 33 40 34 41 The `persistedFetchExchange` only handles queries, so for mutations we keep the 35 42 `fetchExchange` around alongside of it.
+33
exchanges/persisted-fetch/src/persistedFetchExchange.test.ts
··· 193 193 }, 194 194 }); 195 195 }); 196 + 197 + it('supports a custom hash function', async () => { 198 + const expected = { 199 + data: { 200 + test: true, 201 + }, 202 + }; 203 + 204 + fetch.mockResolvedValueOnce({ 205 + json: () => expected, 206 + }); 207 + 208 + const hashFn = () => Promise.resolve('hello'); 209 + 210 + await pipe( 211 + fromValue(queryOperation), 212 + persistedFetchExchange({ generateHash: hashFn })(exchangeArgs), 213 + toPromise 214 + ); 215 + 216 + expect(fetch).toHaveBeenCalledTimes(1); 217 + 218 + const body = JSON.parse(fetch.mock.calls[0][1].body); 219 + 220 + expect(body).toMatchObject({ 221 + extensions: { 222 + persistedQuery: { 223 + version: 1, 224 + sha256Hash: 'hello', 225 + }, 226 + }, 227 + }); 228 + });
+3 -1
exchanges/persisted-fetch/src/persistedFetchExchange.ts
··· 32 32 33 33 interface PersistedFetchExchangeOptions { 34 34 preferGetForPersistedQueries?: boolean; 35 + generateHash?: (query: string) => Promise<string>; 35 36 } 36 37 37 38 export const persistedFetchExchange = ( ··· 39 40 ): Exchange => ({ forward, dispatchDebug }) => { 40 41 if (!options) options = {}; 41 42 43 + const hashFn = options.generateHash || hash; 42 44 let supportsPersistedQueries = true; 43 45 44 46 return ops$ => { ··· 66 68 67 69 return pipe( 68 70 // Hash the given GraphQL query 69 - fromPromise(hash(query)), 71 + fromPromise(hashFn(query)), 70 72 mergeMap(sha256Hash => { 71 73 // Attach SHA256 hash and remove query from body 72 74 body.query = undefined;