Skip to content

Commit

Permalink
feat(persisted): Allow nullish hashes for skipping persisted logic (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kitten authored Jul 21, 2023
1 parent 2acc629 commit a685bc9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/seven-adults-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@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 changes: 21 additions & 0 deletions exchanges/persisted/src/persistedExchange.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,27 @@ it('retries query persisted query resulted in unsupported', async () => {
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 => {
Expand Down
6 changes: 5 additions & 1 deletion exchanges/persisted/src/persistedExchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,18 @@ export interface PersistedExchangeOptions {
* 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
Expand Down

0 comments on commit a685bc9

Please sign in to comment.