-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
feat: add useBackgroundQuery
and useReadQuery
Suspense hooks
#10755
Conversation
🦋 Changeset detectedLatest commit: 46ddb18 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
85e047d
to
ffb3538
Compare
c37dfd7
to
438fa99
Compare
438fa99
to
bb6954a
Compare
bb6954a
to
76a3193
Compare
useBackgroundQuery
and useReadQuery
hooksuseBackgroundQuery
and useReadQuery
Suspense hooks
/release:pr |
A new release has been made for this PR. You can install it with |
One thing to keep in mind: we'll have to try this with the Next package, to make sure that those |
Good call-out, added to the list of future work on the PR description that I'll tackle... next 😀 |
Looking at this, it might sense for us to pair on this. Quick intro: take a look at these wrapped hook definitions. So, looking at the Going through the public properties, |
VariablesCaseData, | ||
VariablesCaseVariables | ||
>(query); | ||
const { data: explicit } = useSuspenseQuery(query); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These being type tests, the explicit setting of the generic type here was actually purposeful (hence the name explicit
). Essentially I wanted to make sure that setting the type of the generic directly rather than relying on type inference ensured that data
was properly typed with the various options you can pass to the hook.
I realize though that I made a mistake and I shouldn't be passing a query here that uses TypedDocumentNode
(which make type inference work correctly). I'll update this in a separate PR to use an untyped query document to test against, which is a bit more realistic.
In the mean time, would you mind reverting these useSuspenseQuery
type tests back to the original?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, my bad - was moving too fast there. Totally unintended, will revert the changes for the types tests now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem at all! Thanks so much 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
56083fb :)
size-limit report 📦
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Outside of my last comment, this PR looks great! I'm super excited to get this into an alpha!
…st (see prev commit)
Adds
useBackgroundQuery
anduseReadQuery
hooks as companion hooks touseSuspenseQuery
for fetching and reading data within React Suspense boundaries.useBackgroundQuery
useBackgroundQuery
allows a user to initiate a query as "high up" in their application's render tree as possible, decoupled from the site where the returned data is consumed, which will trigger the nearest Suspense boundary while the query is pending.The hook accepts
query
andoptions
as positional arguments, the latter extendingSuspenseQueryHookOptions
while omittingreturnPartialData
andrefetchWritePolicy
for a slightly smaller initial API surface. The hook returns aqueryRef
as well asfetchMore
andrefetch
functions.queryRef
In order to link a query initiated by a specific
useBackgroundQuery
call to the place its data are consumed—which can be uniquely identified not only by the query and variables passed, but also the optionalqueryKey
supplied by the user—the hook needs to return a reference to the query that can be read later on.A
queryRef
is aQuerySubscription
instance that holds references to the promises that are read or thrown by React'suse
(or Apollo Client's__use
ifReact.use
is not detected).useReadQuery
useReadQuery
accepts aqueryRef
and returnsdata
,networkStatus
anderror
.Hello World Example
Future PRs
useBackgroundQuery
types fordata
andrefetch
#10878:refetch
data
(this handoff happens across the two hooks, we may need to updateQuerySubscription
)useBackgroundQuery
test cases #10879:queryKey
testsuseBackgroundQuery
/useReadQuery
compatibility with@apollo/experimental-nextjs-app-support
#10875useBackgroundQuery
anduseReadQuery
#10874setVersion