Skip to content

Commit

Permalink
Add @defer support to useSuspenseQuery (#10324)
Browse files Browse the repository at this point in the history
* Add support for @defer queries in useSuspenseQuery

* Add test to verify deferred queries work with lists

* Add more tests to ensure deferred queries work with various conditions

* Add tests to ensure cached data is properly returned with deferred queries

* Add tests to check error behavior returned by first chunk

* Add handling of errors returned in deferred chunks

* Add tests to validate errorPolicy behavior with deferred queries

* Slight change to verbiage in test

* Add test to verify partial data with returnPartialData

* Update bundlesize

* Rename function to better convey intent

* Enable skipped tests since fix is merged to `main`

* Add changeset

* Update comment for reobserveAsConcast

* Update bundlesize

* Update bundlesize

Co-authored-by: Alessia Bellisario <alessia@apollographql.com>
  • Loading branch information
jerelmiller and alessbell committed Dec 21, 2022
1 parent ad852e8 commit 95eb228
Show file tree
Hide file tree
Showing 5 changed files with 1,519 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/sixty-trains-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@apollo/client': patch
---

Add `@defer` support to `useSuspenseQuery`.
2 changes: 1 addition & 1 deletion config/bundlesize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { join } from "path";
import { gzipSync } from "zlib";
import bytes from "bytes";

const gzipBundleByteLengthLimit = bytes("32.79KB");
const gzipBundleByteLengthLimit = bytes("33.01KB");
const minFile = join("dist", "apollo-client.min.cjs");
const minPath = join(__dirname, "..", minFile);
const gzipByteLen = gzipSync(readFileSync(minPath)).byteLength;
Expand Down
23 changes: 19 additions & 4 deletions src/core/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -784,10 +784,15 @@ Did you mean to call refetch(variables) instead of refetch({ variables })?`);
return this.last;
}

public reobserve(
// For cases like suspense with a deferred query where we need a custom
// promise wrapped around the concast, we need access to the raw concast
// created from `reobserve`. This function provides the original `reobserve`
// functionality, but returns a concast instead of a promise. Most consumers
// should prefer `reobserve` instead of this function.
public reobserveAsConcast(
newOptions?: Partial<WatchQueryOptions<TVariables, TData>>,
newNetworkStatus?: NetworkStatus,
): Promise<ApolloQueryResult<TData>> {
newNetworkStatus?: NetworkStatus
): Concast<ApolloQueryResult<TData>> {
this.isTornDown = false;

const useDisposableConcast =
Expand Down Expand Up @@ -860,7 +865,17 @@ Did you mean to call refetch(variables) instead of refetch({ variables })?`);

concast.addObserver(observer);

return concast.promise;
return concast;
}

public reobserve(
newOptions?: Partial<WatchQueryOptions<TVariables, TData>>,
newNetworkStatus?: NetworkStatus,
): Promise<ApolloQueryResult<TData>> {
return this.reobserveAsConcast(
newOptions,
newNetworkStatus
).promise
}

// (Re)deliver the current result to this.observers without applying fetch
Expand Down
Loading

0 comments on commit 95eb228

Please sign in to comment.