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

support no-cache requests #2934

Merged
merged 1 commit into from
Feb 1, 2018
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 docs/source/basics/caching.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ client.writeQuery({

Here are some common situations where you would need to access the cache directly. If you're manipulating the cache in an interesting way and would like your example to be featured, please send in a pull request!

<h3 id="ignore">Bypassing the cache</h3>
Sometimes it makes sense to not use the cache for a specfic operation. This can be done using either the `network-only` or `no-cache` fetchPolicy. The key difference between these two policies is that `network-only` still saves the response to the cache for later use, bypassing the reading and forcing a network request. The `no-cache` policy does not read, nor does it write to the cache with the response. This may be useful for sensitive data like passwords that you don't want to keep in the cache.

<h3 id="server">Server side rendering</h3>

First, you will need to initialize an `InMemoryCache` on the server and create an instance of `ApolloClient`. In the initial serialized HTML payload from the server, you should include a script tag that extracts the data from the cache. (The `.replace()` is necessary to prevent script injection attacks)
Expand Down
1 change: 1 addition & 0 deletions packages/apollo-client/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Change log

### vNEXT
- Add new fetchPolicy called 'no-cache' to bypass reading from or saving to the cache when making a query

### 2.2.1
- Allow optional parameter to include queries in standby mode when refetching observed queries [PR#2804](https://github.com/apollographql/apollo-client/pull/2804)
Expand Down
60 changes: 38 additions & 22 deletions packages/apollo-client/src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export class QueryManager<TStore> {
refetchQueries = [],
update: updateWithProxyFn,
errorPolicy = 'none',
fetchPolicy,
context = {},
}: MutationOptions): Promise<FetchResult<T>> {
if (!mutation) {
Expand All @@ -131,6 +132,12 @@ export class QueryManager<TStore> {
);
}

if (fetchPolicy && fetchPolicy !== 'no-cache') {
throw new Error(
"fetchPolicy for mutations currently only supports the 'no-cache' policy",
);
}

const mutationId = this.generateQueryId();
const cache = this.dataStore.getCache();
(mutation = cache.transformDocument(mutation)),
Expand Down Expand Up @@ -195,14 +202,16 @@ export class QueryManager<TStore> {

this.mutationStore.markMutationResult(mutationId);

this.dataStore.markMutationResult({
mutationId,
result,
document: mutation,
variables: variables || {},
updateQueries: generateUpdateQueriesInfo(),
update: updateWithProxyFn,
});
if (fetchPolicy !== 'no-cache') {
this.dataStore.markMutationResult({
mutationId,
result,
document: mutation,
variables: variables || {},
updateQueries: generateUpdateQueriesInfo(),
update: updateWithProxyFn,
});
}
storeResult = result as FetchResult<T>;
},
error: (err: Error) => {
Expand Down Expand Up @@ -283,12 +292,17 @@ export class QueryManager<TStore> {
const query = cache.transformDocument(options.query);

let storeResult: any;
let needToFetch: boolean = fetchPolicy === 'network-only';
let needToFetch: boolean =
fetchPolicy === 'network-only' || fetchPolicy === 'no-cache';

// If this is not a force fetch, we want to diff the query against the
// store before we fetch it from the network interface.
// TODO we hit the cache even if the policy is network-first. This could be unnecessary if the network is up.
if (fetchType !== FetchType.refetch && fetchPolicy !== 'network-only') {
if (
fetchType !== FetchType.refetch &&
fetchPolicy !== 'network-only' &&
fetchPolicy !== 'no-cache'
) {
const { complete, result } = this.dataStore.getCache().diff({
query,
variables,
Expand Down Expand Up @@ -1025,7 +1039,7 @@ export class QueryManager<TStore> {
options: WatchQueryOptions;
fetchMoreForQueryId?: string;
}): Promise<ExecutionResult> {
const { variables, context, errorPolicy = 'none' } = options;
const { variables, context, errorPolicy = 'none', fetchPolicy } = options;
const operation = this.buildOperationForLink(document, variables, {
...context,
// TODO: Should this be included for all entry points via
Expand All @@ -1042,17 +1056,19 @@ export class QueryManager<TStore> {
// default the lastRequestId to 1
const { lastRequestId } = this.getQuery(queryId);
if (requestId >= (lastRequestId || 1)) {
try {
this.dataStore.markQueryResult(
result,
document,
variables,
fetchMoreForQueryId,
errorPolicy === 'ignore',
);
} catch (e) {
reject(e);
return;
if (fetchPolicy !== 'no-cache') {
try {
this.dataStore.markQueryResult(
result,
document,
variables,
fetchMoreForQueryId,
errorPolicy === 'ignore',
);
} catch (e) {
reject(e);
return;
}
}

this.queryStore.markQueryResult(
Expand Down
Loading