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

Allow ApolloCache implementations to specify default value for assumeImmutableResults client option #10567

Merged
merged 3 commits into from
Mar 21, 2023
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
5 changes: 5 additions & 0 deletions .changeset/small-tomatoes-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@apollo/client': minor
---

Allow `ApolloCache` implementations to specify default value for `assumeImmutableResults` client option, improving performance for applications currently using `InMemoryCache` without configuring `new ApolloClient({ assumeImmutableResults: true })`
2 changes: 2 additions & 0 deletions src/cache/core/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { Cache } from './types/Cache';
export type Transaction<T> = (c: ApolloCache<T>) => void;

export abstract class ApolloCache<TSerialized> implements DataProxy {
public readonly assumeImmutableResults: boolean = false;

// required to implement
// core API
public abstract read<TData = any, TVariables = any>(
Expand Down
4 changes: 4 additions & 0 deletions src/cache/inmemory/inMemoryCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export class InMemoryCache extends ApolloCache<NormalizedCacheObject> {
any,
[Cache.WatchOptions]>;

// Override the default value, since InMemoryCache result objects are frozen
// in development and expected to remain logically immutable in production.
public readonly assumeImmutableResults = true;

// Dynamically imported code can augment existing typePolicies or
// possibleTypes by calling cache.policies.addTypePolicies or
// cache.policies.addPossibletypes.
Expand Down
18 changes: 9 additions & 9 deletions src/core/ApolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ export class ApolloClient<TCacheShape> implements DataProxy {
* you are using.
*/
constructor(options: ApolloClientOptions<TCacheShape>) {
if (!options.cache) {
throw new InvariantError(
"To initialize Apollo Client, you must specify a 'cache' property " +
"in the options object. \n" +
"For more information, please visit: https://go.apollo.dev/c/docs"
Copy link
Contributor

Choose a reason for hiding this comment

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

[Not material to this PR but] TIL that shortlink exists

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep, and we can add more short links if need be. It would be cool if each error had its own short link with more info.

);
}

const {
uri,
credentials,
Expand All @@ -143,7 +151,7 @@ export class ApolloClient<TCacheShape> implements DataProxy {
__DEV__,
queryDeduplication = true,
defaultOptions,
assumeImmutableResults = false,
assumeImmutableResults = cache.assumeImmutableResults,
resolvers,
typeDefs,
fragmentMatcher,
Expand All @@ -159,14 +167,6 @@ export class ApolloClient<TCacheShape> implements DataProxy {
: ApolloLink.empty();
}

if (!cache) {
throw new InvariantError(
"To initialize Apollo Client, you must specify a 'cache' property " +
"in the options object. \n" +
"For more information, please visit: https://go.apollo.dev/c/docs"
);
}

this.link = link;
this.cache = cache;
this.disableNetworkFetches = ssrMode || ssrForceFetchDelay > 0;
Expand Down
4 changes: 2 additions & 2 deletions src/core/QueryInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { DeepMerger } from "../utilities"
import { mergeIncrementalData } from '../utilities';
import { WatchQueryOptions, ErrorPolicy } from './watchQueryOptions';
import { ObservableQuery, reobserveCacheFirst } from './ObservableQuery';
import { QueryListener } from './types';
import { QueryListener, MethodKeys } from './types';
import { FetchResult } from '../link/core';
import {
ObservableSubscription,
Expand Down Expand Up @@ -40,7 +40,7 @@ const destructiveMethodCounts = new (

function wrapDestructiveCacheMethod(
cache: ApolloCache<any>,
methodName: keyof ApolloCache<any>,
methodName: MethodKeys<ApolloCache<any>>,
) {
const original = cache[methodName];
if (typeof original === "function") {
Expand Down
4 changes: 2 additions & 2 deletions src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export class QueryManager<TStore> {
ssrMode = false,
clientAwareness = {},
localState,
assumeImmutableResults,
assumeImmutableResults = !!cache.assumeImmutableResults,
}: {
cache: ApolloCache<TStore>;
link: ApolloLink;
Expand All @@ -138,7 +138,7 @@ export class QueryManager<TStore> {
this.clientAwareness = clientAwareness;
this.localState = localState || new LocalState({ cache });
this.ssrMode = ssrMode;
this.assumeImmutableResults = !!assumeImmutableResults;
this.assumeImmutableResults = assumeImmutableResults;
if ((this.onBroadcast = onBroadcast)) {
this.mutationStore = Object.create(null);
}
Expand Down
4 changes: 4 additions & 0 deletions src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import { IsStrictlyAny } from '../utilities';

export { TypedDocumentNode } from '@graphql-typed-document-node/core';

export type MethodKeys<T> = {
[P in keyof T]: T[P] extends Function ? P : never
}[keyof T];

export interface DefaultContext extends Record<string, any> {};

export type QueryListener = (queryInfo: QueryInfo) => void;
Expand Down