Skip to content

Commit

Permalink
QueryReference: check for resolve/reject
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Jun 21, 2023
1 parent 43c506a commit 816a7b6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/little-schools-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@apollo/client': patch
---

Fix a bug in `QueryReference` where `this.resolve` or `this.reject` might be executed even if `undefined`.
12 changes: 8 additions & 4 deletions src/react/cache/QueryReference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export class QueryReference<TData = unknown> {
private initialized = false;
private refetching = false;

private resolve: (result: ApolloQueryResult<TData>) => void;
private reject: (error: unknown) => void;
private resolve: ((result: ApolloQueryResult<TData>) => void) | undefined;
private reject: ((error: unknown) => void) | undefined;

constructor(
observable: ObservableQuery<TData>,
Expand Down Expand Up @@ -157,7 +157,9 @@ export class QueryReference<TData = unknown> {
this.initialized = true;
this.refetching = false;
this.result = result;
this.resolve(result);
if (this.resolve) {
this.resolve(result);
}
return;
}

Expand All @@ -182,7 +184,9 @@ export class QueryReference<TData = unknown> {
if (!this.initialized || this.refetching) {
this.initialized = true;
this.refetching = false;
this.reject(error);
if (this.reject) {
this.reject(error);
}
return;
}

Expand Down

0 comments on commit 816a7b6

Please sign in to comment.