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.

feat(persisted): Allow nullish hashes for skipping persisted logic (#3324)

authored by

Phil Pluckthun and committed by
GitHub
a685bc90 2acc6298

+31 -1
+5
.changeset/seven-adults-fix.md
··· 1 + --- 2 + '@urql/exchange-persisted': minor 3 + --- 4 + 5 + Allow persisted query logic to be skipped by the `persistedExchange` if the passed `generateHash` function resolves to a nullish value. This allows (A)PQ to be selectively disabled for individual operations.
+21
exchanges/persisted/src/persistedExchange.test.ts
··· 119 119 expect(operations[2].extensions).toEqual(undefined); 120 120 }); 121 121 122 + it('skips operation when generateHash returns a nullish value', async () => { 123 + const { result, operations, exchangeArgs } = makeExchangeArgs(); 124 + 125 + result.mockImplementationOnce(operation => ({ 126 + ...queryResponse, 127 + operation, 128 + data: null, 129 + })); 130 + 131 + const res = await pipe( 132 + fromValue(queryOperation), 133 + persistedExchange({ generateHash: async () => null })(exchangeArgs), 134 + take(1), 135 + toPromise 136 + ); 137 + 138 + expect(res.operation.context.persistAttempt).toBe(true); 139 + expect(operations.length).toBe(1); 140 + expect(operations[0]).not.toHaveProperty('extensions.persistedQuery'); 141 + }); 142 + 122 143 it.each([true, 'force', 'within-url-limit'] as const)( 123 144 'sets `context.preferGetMethod` to %s when `options.preferGetForPersistedQueries` is %s', 124 145 async preferGetMethodValue => {
+5 -1
exchanges/persisted/src/persistedExchange.ts
··· 68 68 * hashes at compile-time, or need to use a custom SHA-256 function, 69 69 * you may pass one here. 70 70 * 71 + * If `generateHash` returns either `null` or `undefined`, the 72 + * operation will not be treated as a persisted operation, which 73 + * essentially skips this exchange’s logic for a given operation. 74 + * 71 75 * Hint: The default SHA-256 function uses the WebCrypto API. This 72 76 * API is unavailable on React Native, which may require you to 73 77 * pass a custom function here. ··· 75 79 generateHash?( 76 80 query: string, 77 81 document: TypedDocumentNode<any, any> 78 - ): Promise<string>; 82 + ): Promise<string | undefined | null>; 79 83 /** Enables persisted queries to be used for mutations. 80 84 * 81 85 * @remarks