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

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

Changed files
+31 -1
.changeset
exchanges
+5
.changeset/seven-adults-fix.md
···
+
---
+
'@urql/exchange-persisted': minor
+
---
+
+
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
···
expect(operations[2].extensions).toEqual(undefined);
});
+
it('skips operation when generateHash returns a nullish value', async () => {
+
const { result, operations, exchangeArgs } = makeExchangeArgs();
+
+
result.mockImplementationOnce(operation => ({
+
...queryResponse,
+
operation,
+
data: null,
+
}));
+
+
const res = await pipe(
+
fromValue(queryOperation),
+
persistedExchange({ generateHash: async () => null })(exchangeArgs),
+
take(1),
+
toPromise
+
);
+
+
expect(res.operation.context.persistAttempt).toBe(true);
+
expect(operations.length).toBe(1);
+
expect(operations[0]).not.toHaveProperty('extensions.persistedQuery');
+
});
+
it.each([true, 'force', 'within-url-limit'] as const)(
'sets `context.preferGetMethod` to %s when `options.preferGetForPersistedQueries` is %s',
async preferGetMethodValue => {
+5 -1
exchanges/persisted/src/persistedExchange.ts
···
* hashes at compile-time, or need to use a custom SHA-256 function,
* you may pass one here.
*
+
* If `generateHash` returns either `null` or `undefined`, the
+
* operation will not be treated as a persisted operation, which
+
* essentially skips this exchange’s logic for a given operation.
+
*
* Hint: The default SHA-256 function uses the WebCrypto API. This
* API is unavailable on React Native, which may require you to
* pass a custom function here.
···
generateHash?(
query: string,
document: TypedDocumentNode<any, any>
-
): Promise<string>;
+
): Promise<string | undefined | null>;
/** Enables persisted queries to be used for mutations.
*
* @remarks