Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(persisted-fetch): Add enableForMutation option #2951

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/slimy-nails-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/exchange-persisted-fetch': minor
---

Adds enableForMutation option for exchange-persisted-fetch to enable persisted operations for mutations
1 change: 1 addition & 0 deletions docs/api/persisted-fetch-exchange.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,4 @@ const client = createClient({
| `preferGetForPersistedQueries` | This is similar to [the `Client`'s `preferGetMethod` option](./core.md#client) and will cause all persisted queries to be sent using a GET request. |
| `enforcePersistedQueries` | This option enforced persisted queries. Instead of allowing automatic persisted queries or triggering any retry logic when the API responds, it instead assumes that persisted queries will succeed and run like normal GraphQL API requests. |
| `generateHash` | This option accepts a function that receives the `query` as a string and the raw `DocumentNode` as a second argument and must return a `Promise<string>` resolving to a SHA256 hash. This can be used to swap out the SHA256 API, e.g. for React Native, or to use pre-generated SHA256 strings from the `DocumentNode`. |
| `enableForMutation` | This option allows mutations to be persisted in addition to queries. It's false by default. When a persisted mutation is requested, `preferGetForPersistedQueries` will be ignored and a POST method will always be used. |
16 changes: 13 additions & 3 deletions exchanges/persisted-fetch/src/persistedFetchExchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ interface PersistedFetchExchangeOptions {
preferGetForPersistedQueries?: boolean;
enforcePersistedQueries?: boolean;
generateHash?: (query: string, document: DocumentNode) => Promise<string>;
enableForMutation?: boolean;
}

export const persistedFetchExchange = (
Expand All @@ -46,13 +47,18 @@ export const persistedFetchExchange = (
const preferGetForPersistedQueries = !!options.preferGetForPersistedQueries;
const enforcePersistedQueries = !!options.enforcePersistedQueries;
const hashFn = options.generateHash || hash;
const enableForMutation = options.enableForMutation || false;
let supportsPersistedQueries = true;

const operationFilter = (operation: Operation) =>
(enableForMutation && operation.kind === 'mutation') ||
operation.kind === 'query';

return ops$ => {
const sharedOps$ = share(ops$);
const fetchResults$ = pipe(
sharedOps$,
filter(operation => operation.kind === 'query'),
filter(operationFilter),
mergeMap(operation => {
const { key } = operation;
const teardown$ = pipe(
Expand Down Expand Up @@ -86,11 +92,15 @@ export const persistedFetchExchange = (
},
};
}
const useGet =
operation.kind === 'query' &&
preferGetForPersistedQueries &&
!!sha256Hash;
return makePersistedFetchSource(
operation,
body,
dispatchDebug,
!!(preferGetForPersistedQueries && sha256Hash)
useGet
);
}),
mergeMap(result => {
Expand Down Expand Up @@ -128,7 +138,7 @@ export const persistedFetchExchange = (

const forward$ = pipe(
sharedOps$,
filter(operation => operation.kind !== 'query'),
filter(operation => !operationFilter(operation)),
forward
);

Expand Down