Skip to content

Commit

Permalink
Compare DocumentNode used in refetchQueries as strings (#12236)
Browse files Browse the repository at this point in the history
Co-authored-by: Jerel Miller <[email protected]>
  • Loading branch information
charpeni and jerelmiller authored Dec 19, 2024
1 parent 2fb7d7a commit 4334d30
Show file tree
Hide file tree
Showing 4 changed files with 326 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-sheep-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

Fix an issue with `refetchQueries` where comparing `DocumentNode`s internally by references could lead to an unknown query, even though the `DocumentNode` was indeed an active query—with a different reference.
4 changes: 2 additions & 2 deletions .size-limits.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"dist/apollo-client.min.cjs": 41615,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 34349
"dist/apollo-client.min.cjs": 41639,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 34381
}
40 changes: 25 additions & 15 deletions src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -899,15 +899,19 @@ export class QueryManager<TStore> {
include: InternalRefetchQueriesInclude = "active"
) {
const queries = new Map<string, ObservableQuery<any>>();
const queryNamesAndDocs = new Map<string | DocumentNode, boolean>();
const queryNames = new Map<string, string | null>();
const queryNamesAndQueryStrings = new Map<string, boolean>();
const legacyQueryOptions = new Set<QueryOptions>();

if (Array.isArray(include)) {
include.forEach((desc) => {
if (typeof desc === "string") {
queryNamesAndDocs.set(desc, false);
queryNames.set(desc, desc);
queryNamesAndQueryStrings.set(desc, false);
} else if (isDocumentNode(desc)) {
queryNamesAndDocs.set(this.transform(desc), false);
const queryString = print(this.transform(desc));
queryNames.set(queryString, getOperationName(desc));
queryNamesAndQueryStrings.set(queryString, false);
} else if (isNonNullObject(desc) && desc.query) {
legacyQueryOptions.add(desc);
}
Expand Down Expand Up @@ -935,12 +939,12 @@ export class QueryManager<TStore> {

if (
include === "active" ||
(queryName && queryNamesAndDocs.has(queryName)) ||
(document && queryNamesAndDocs.has(document))
(queryName && queryNamesAndQueryStrings.has(queryName)) ||
(document && queryNamesAndQueryStrings.has(print(document)))
) {
queries.set(queryId, oq);
if (queryName) queryNamesAndDocs.set(queryName, true);
if (document) queryNamesAndDocs.set(document, true);
if (queryName) queryNamesAndQueryStrings.set(queryName, true);
if (document) queryNamesAndQueryStrings.set(print(document), true);
}
}
});
Expand Down Expand Up @@ -969,15 +973,21 @@ export class QueryManager<TStore> {
});
}

if (__DEV__ && queryNamesAndDocs.size) {
queryNamesAndDocs.forEach((included, nameOrDoc) => {
if (__DEV__ && queryNamesAndQueryStrings.size) {
queryNamesAndQueryStrings.forEach((included, nameOrQueryString) => {
if (!included) {
invariant.warn(
typeof nameOrDoc === "string" ?
`Unknown query named "%s" requested in refetchQueries options.include array`
: `Unknown query %o requested in refetchQueries options.include array`,
nameOrDoc
);
const queryName = queryNames.get(nameOrQueryString);

if (queryName) {
invariant.warn(
`Unknown query named "%s" requested in refetchQueries options.include array`,
queryName
);
} else {
invariant.warn(
`Unknown anonymous query requested in refetchQueries options.include array`
);
}
}
});
}
Expand Down
Loading

0 comments on commit 4334d30

Please sign in to comment.