Skip to content
Draft
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "major",
"comment": "observer",
"packageName": "@graphitation/apollo-forest-run",
"email": "pavelglac@gmail.com",
"dependentChangeType": "patch"
}
22 changes: 22 additions & 0 deletions packages/apollo-forest-run/compat/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

105 changes: 95 additions & 10 deletions packages/apollo-forest-run/src/ForestRun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type {
Transaction,
} from "./cache/types";
import { ApolloCache } from "@apollo/client";
import { Observable } from "@apollo/client/utilities";
import { assert } from "./jsutils/assert";
import { accumulate, deleteAccumulated } from "./jsutils/map";
import { read } from "./cache/read";
Expand Down Expand Up @@ -342,11 +343,18 @@ export class ForestRun<
public watch<TData = any, TVariables = any>(
watch: Cache.WatchOptions<TData, TVariables>,
): () => void {
return this.env.optimizeFragmentReads &&
if (
this.env.optimizeFragmentReads &&
isFragmentDocument(watch.query) &&
(watch.id || watch.rootId)
? this.watchFragment(watch)
: this.watchOperation(watch);
) {
const id = watch.id ?? watch.rootId;
accumulate(this.store.fragmentWatches, id, watch);
return () => {
deleteAccumulated(this.store.fragmentWatches, id, watch);
};
}
return this.watchOperation(watch);
}

private watchOperation(watch: Cache.WatchOptions) {
Expand All @@ -370,14 +378,91 @@ export class ForestRun<
};
}

private watchFragment(watch: Cache.WatchOptions) {
const id = watch.id ?? watch.rootId;
assert(id !== undefined);
accumulate(this.store.fragmentWatches, id, watch);

return () => {
deleteAccumulated(this.store.fragmentWatches, id, watch);
public watchFragment<TData = any, TVars = any>(options: {
fragment: DocumentNode;
fragmentName?: string;
from: StoreObject | Reference | string;
optimistic?: boolean;
variables?: TVars;
}): Observable<{
data: TData;
complete: boolean;
missing?: any;
}> {
const {
fragment,
fragmentName,
from,
optimistic = true,
...otherOptions
} = options;
const query = this["getFragmentDoc"](fragment, fragmentName);
// While our TypeScript types do not allow for `undefined` as a valid
// `from`, its possible `useFragment` gives us an `undefined` since it
// calls `cache.identify` and provides that value to `from`. We are
// adding this fix here however to ensure those using plain JavaScript
// and using `cache.identify` themselves will avoid seeing the obscure
// warning.
const id =
typeof from === "undefined" || typeof from === "string"
? from
: this.identify(from);

const diffOptions: Cache.DiffOptions<TData, TVars> = {
...otherOptions,
returnPartialData: true,
id,
query,
optimistic,
};

let latestDiff: Cache.DiffResult<TData> | undefined;

return new Observable((observer) => {
const callback = (diff: Cache.DiffResult<TData>) => {
const data = diff.result == null ? ({} as TData) : diff.result;

if (latestDiff && equal(latestDiff.result, data)) {
return;
}

const result = {
data,
complete: !!diff.complete,
missing: diff.missing,
};

latestDiff = {
...diff,
result: data,
};

observer.next(result);
};

const watchOptions: Cache.WatchOptions<TData, TVars> = {
...diffOptions,
immediate: true,
callback,
};

// Use ForestRun fragment optimization: register directly in
// fragmentWatches keyed by entity ID for efficient lookups
// via collectAffectedWatches / transaction.affectedNodes
if (this.env.optimizeFragmentReads && id) {
const initialDiff = this.diff(watchOptions);
if (this.shouldNotifyWatch(watchOptions, initialDiff)) {
this.notifyWatch(watchOptions, initialDiff);
}
accumulate(this.store.fragmentWatches, id, watchOptions);
return () => {
deleteAccumulated(this.store.fragmentWatches, id, watchOptions);
};
}

// Fall back to standard operation watching
return this.watchOperation(watchOptions);
});
}

// Compatibility with InMemoryCache for Apollo dev tools
Expand Down
Loading
Loading