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

Clarify mutation fetchPolicy options using MutationFetchPolicy union type #8602

Merged
merged 2 commits into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
- Reevaluate `window.fetch` each time `HttpLink` uses it, if not configured using `options.fetch`. This change enables a variety of strategies for instrumenting `window.fetch`, without requiring those strategies to run before `@apollo/client/link/http` is first imported. <br/>
[@benjamn](https://github.com/benjamn) in [#8603](https://github.com/apollographql/apollo-client/pull/8603)

- Clarify mutation `fetchPolicy` options (`"network-only"` or `"no-cache"`) using `MutationFetchPolicy` union type. <br/>
[@benjamn](https://github.com/benjamn) in [#8602](https://github.com/apollographql/apollo-client/pull/8602)

### Bug Fixes

- Restore full `@apollo/client/apollo-client.cjs.js` CommonJS bundle for older bundlers.
Expand Down
12 changes: 7 additions & 5 deletions src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
MutationOptions,
WatchQueryFetchPolicy,
ErrorPolicy,
MutationFetchPolicy,
} from './watchQueryOptions';
import { ObservableQuery, applyNextFetchPolicy, logMissingFieldErrors } from './ObservableQuery';
import { NetworkStatus, isNetworkRequestInFlight } from './networkStatus';
Expand Down Expand Up @@ -162,7 +163,7 @@ export class QueryManager<TStore> {
update: updateWithProxyFn,
onQueryUpdated,
errorPolicy = 'none',
fetchPolicy,
fetchPolicy = 'network-only',
keepRootFields,
context,
}: MutationOptions<TData, TVariables, TContext>): Promise<FetchResult<TData>> {
Expand All @@ -172,8 +173,9 @@ export class QueryManager<TStore> {
);

invariant(
!fetchPolicy || fetchPolicy === 'no-cache',
"Mutations only support a 'no-cache' fetchPolicy. If you don't want to disable the cache, remove your fetchPolicy setting to proceed with the default mutation behavior."
fetchPolicy === 'network-only' ||
fetchPolicy === 'no-cache',
"Mutations support only 'network-only' or 'no-cache' fetchPolicy strings. The default `network-only` behavior automatically writes mutation results to the cache. Passing `no-cache` skips the cache write."
);

const mutationId = this.generateMutationId();
Expand Down Expand Up @@ -321,7 +323,7 @@ export class QueryManager<TStore> {
result: FetchResult<TData>;
document: DocumentNode;
variables?: TVariables;
fetchPolicy?: "no-cache";
fetchPolicy?: MutationFetchPolicy;
errorPolicy: ErrorPolicy;
context?: TContext;
updateQueries: UpdateQueries<TData>;
Expand Down Expand Up @@ -479,7 +481,7 @@ export class QueryManager<TStore> {
mutationId: string;
document: DocumentNode;
variables?: TVariables;
fetchPolicy?: "no-cache";
fetchPolicy?: MutationFetchPolicy;
errorPolicy: ErrorPolicy;
context?: TContext;
updateQueries: UpdateQueries<TData>,
Expand Down
15 changes: 10 additions & 5 deletions src/core/watchQueryOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ export type FetchPolicy =

export type WatchQueryFetchPolicy = FetchPolicy | 'cache-and-network';

export type MutationFetchPolicy = Extract<
FetchPolicy,
| 'network-only' // default behavior (mutation results written to cache)
| 'no-cache' // alternate behavior (results not written to cache)
>;
Comment on lines +33 to +37
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type is ultimately just 'network-only' | 'no-cache', but "extracting" that union from FetchPolicy (which is also just a union of strings) felt like the right thing to do to convey the code's intent.


export type RefetchWritePolicy = "merge" | "overwrite";

/**
Expand Down Expand Up @@ -297,12 +303,11 @@ export interface MutationOptions<
mutation: DocumentNode | TypedDocumentNode<TData, TVariables>;

/**
* Specifies the {@link FetchPolicy} to be used for this query. Mutations only
* support a 'no-cache' fetchPolicy. If you don't want to disable the cache,
* remove your fetchPolicy setting to proceed with the default mutation
* behavior.
* Specifies the {@link MutationFetchPolicy} to be used for this query.
* Mutations support only 'network-only' and 'no-cache' fetchPolicy strings.
* If fetchPolicy is not provided, it defaults to 'network-only'.
*/
fetchPolicy?: Extract<FetchPolicy, 'no-cache'>;
fetchPolicy?: MutationFetchPolicy;

/**
* To avoid retaining sensitive information from mutation root field
Expand Down