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

use default makeCacheKey implementation for FragmentRegistry.transform and FragmentRegistry.findFragmentSpreads #11356

Merged
merged 3 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
use default makeCacheKey implementation for
`FragmentRegistry.transform` and `FragmentRegistry.findFragmentSpreads`
  • Loading branch information
phryneas committed Nov 9, 2023
commit 5b71b92934a34ce52037542df5448ea60c38fe4d
5 changes: 5 additions & 0 deletions .changeset/clean-items-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

Fix a potential memory leak in `FragmentRegistry.transform` and `FragmentRegistry.findFragmentSpreads` that would hold on to passed-in `DocumentNodes` for too long.
19 changes: 8 additions & 11 deletions src/cache/inmemory/fragmentRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
} from "graphql";
import { visit } from "graphql";

import { wrap } from "optimism";
import { OptimisticWrapOptions, wrap } from "optimism";

import type { FragmentMap } from "../../utilities/index.js";
import { getFragmentDefinitions } from "../../utilities/index.js";
Expand Down Expand Up @@ -66,7 +66,9 @@ class FragmentRegistry implements FragmentRegistryAPI {
private invalidate(name: string) {}

public resetCaches() {
this.invalidate = (this.lookup = this.cacheUnaryMethod("lookup")).dirty; // This dirty function is bound to the wrapped lookup method.
this.invalidate = (this.lookup = this.cacheUnaryMethod("lookup", {
makeCacheKey: (arg) => arg,
})).dirty; // This dirty function is bound to the wrapped lookup method.
this.transform = this.cacheUnaryMethod("transform");
this.findFragmentSpreads = this.cacheUnaryMethod("findFragmentSpreads");
}
Expand All @@ -76,17 +78,12 @@ class FragmentRegistry implements FragmentRegistryAPI {
FragmentRegistry,
"lookup" | "transform" | "findFragmentSpreads"
>,
>(name: TName) {
>(name: TName, options?: OptimisticWrapOptions<[any]>) {
const registry = this;
const originalMethod = FragmentRegistry.prototype[name];
return wrap(
function () {
return originalMethod.apply(registry, arguments);
},
{
makeCacheKey: (arg) => arg,
}
);
return wrap(function () {
return originalMethod.apply(registry, arguments);
}, options);
}

public lookup(fragmentName: string): FragmentDefinitionNode | null {
Expand Down