Skip to content

Commit

Permalink
Move core fragment masking logic back to cache
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Aug 27, 2024
1 parent 2b6beb2 commit 62c6a1d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 58 deletions.
19 changes: 11 additions & 8 deletions src/cache/core/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import type {
import type { MissingTree } from "./types/common.js";
import { equalByQuery } from "../../core/equalByQuery.js";
import { invariant } from "../../utilities/globals/index.js";
import { maskFragment } from "../../core/masking.js";

export type Transaction<T> = (c: ApolloCache<T>) => void;

Expand Down Expand Up @@ -252,6 +253,7 @@ export abstract class ApolloCache<TSerialized> implements DataProxy {
} = options;
const query = this.getFragmentDoc(fragment, fragmentName);
const id = typeof from === "string" ? from : this.identify(from);
const dataMasking = !!(options as any)[Symbol.for("apollo.dataMasking")];

if (__DEV__) {
const actualFragmentName =
Expand Down Expand Up @@ -279,21 +281,22 @@ export abstract class ApolloCache<TSerialized> implements DataProxy {
return this.watch<TData, TVars>({
...diffOptions,
immediate: true,
callback(diff) {
callback: (diff) => {
const data =
dataMasking ?
maskFragment(diff.result, fragment, this, fragmentName)
: diff.result;

if (
// Always ensure we deliver the first result
latestDiff &&
equalByQuery(
query,
{ data: latestDiff?.result },
{ data: diff.result }
)
equalByQuery(query, { data: latestDiff?.result }, { data })
) {
return;
}

const result = {
data: diff.result as DeepPartial<TData>,
data,
complete: !!diff.complete,
} as WatchFragmentResult<TData>;

Expand All @@ -303,7 +306,7 @@ export abstract class ApolloCache<TSerialized> implements DataProxy {
);
}

latestDiff = diff;
latestDiff = { ...diff, result: data };
observer.next(result);
},
});
Expand Down
39 changes: 4 additions & 35 deletions src/core/ApolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { FetchResult, GraphQLRequest } from "../link/core/index.js";
import { ApolloLink, execute } from "../link/core/index.js";
import type { ApolloCache, DataProxy, Reference } from "../cache/index.js";
import type { DocumentTransform } from "../utilities/index.js";
import { Observable } from "../utilities/index.js";
import type { Observable } from "../utilities/index.js";
import { version } from "../version.js";
import type { UriFunction } from "../link/http/index.js";
import { HttpLink } from "../link/http/index.js";
Expand Down Expand Up @@ -164,7 +164,6 @@ import type {
WatchFragmentOptions,
WatchFragmentResult,
} from "../cache/core/cache.js";
import { equalByQuery } from "./equalByQuery.js";
export { mergeOptions };

/**
Expand Down Expand Up @@ -548,39 +547,9 @@ export class ApolloClient<TCacheShape> implements DataProxy {
>(
options: WatchFragmentOptions<TFragmentData, TVariables>
): Observable<WatchFragmentResult<TFragmentData>> {
const { fragment, fragmentName } = options;

const observable = this.cache.watchFragment(options);
let latestResult: WatchFragmentResult<TFragmentData> | undefined;

return new Observable((observer) => {
const subscription = observable.subscribe({
next: (result) => {
result.data = this.queryManager.maskFragment({
fragment,
fragmentName,
data: result.data,
});

if (
latestResult &&
equalByQuery(
this.cache["getFragmentDoc"](fragment, fragmentName),
{ data: latestResult.data },
{ data: result.data }
)
) {
return;
}

latestResult = result;
observer.next(result);
},
complete: observer.complete.bind(observer),
error: observer.error.bind(observer),
});

return () => subscription.unsubscribe();
return this.cache.watchFragment({
...options,
[Symbol.for("apollo.dataMasking")]: this.queryManager.dataMasking,
});
}

Expand Down
16 changes: 1 addition & 15 deletions src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,7 @@ interface TransformCacheEntry {
import type { DefaultOptions } from "./ApolloClient.js";
import { Trie } from "@wry/trie";
import { AutoCleanedWeakCache, cacheSizes } from "../utilities/index.js";
import { maskFragment, maskOperation } from "./masking.js";

interface MaskFragmentOptions<TData> {
fragment: DocumentNode;
data: TData;
fragmentName?: string;
}
import { maskOperation } from "./masking.js";

interface MaskOperationOptions<TData> {
document: DocumentNode;
Expand Down Expand Up @@ -1538,14 +1532,6 @@ export class QueryManager<TStore> {
return this.dataMasking ? maskOperation(data, document, this.cache) : data;
}

public maskFragment<TData = unknown>(options: MaskFragmentOptions<TData>) {
const { data, fragment, fragmentName } = options;

return this.dataMasking ?
maskFragment(data, fragment, this.cache, fragmentName)
: data;
}

private fetchQueryByPolicy<TData, TVars extends OperationVariables>(
queryInfo: QueryInfo,
{
Expand Down

0 comments on commit 62c6a1d

Please sign in to comment.