feat: include nested query data in responses#15993
Closed
Rich-Harris wants to merge 16 commits into
Closed
Conversation
|
6 tasks
Member
Author
|
closing in favour of #15999 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #15991. This is an alternative to #15952.
This PR makes queries more composable, consistent, and waterfall-proof, and (WIP, but fingers crossed) it does that while reducing the amount of code and complexity. The idea is this: if query A depends on query B, then the values of both will be included in the payload returned from the server, whether for SSR or client-side navigation. That way, if we end up rendering both queries, we don't need to go back to the server for query B's data.
For SSR this isn't such a big deal: if we're already rendering query B on the server then it will end up in the rendered HTML. But for client-side navigation it's a much bigger win, because we eagerly get the stuff query A depends on.
'Depends on' can be expressed in three different ways:
const x = await queryB()causesxto be added to the returned payload. Ifxis part of query A's return value, it doesn't get serialized twice — instead, both queries hold a reference to the same object whether on the server or the clientqueryB().refresh()causes query B to be refreshed at the end of the query, much as happens forcommandandformtoday. (If the data is already available by the end of the query, no additional work happens)queryB().set(data)causes the value of query B to immediately be set todata, and this is what gets sent back to the browser. This is particularly useful when dealing with databases or APIs, where an outer query may get several rows, and can populate the corresponding inner queries with their data without them needing to make their own callsAs well as making waterfalls less likely, this also increases consistency: if query A is refreshed, query B will automatically be refreshed along with it.
Places where this could be useful:
There is one downside here:
The answer, of course, is don't do that. Or put differently: the documentation would steer people away from that pattern and towards smaller, more composable queries.
(Needless to say, if
getAHugeFatBlobOfStuffWeMostlyDontNeedis a normal function and not a query, its return value will not be serialized. You have lots of good ways to design your queries, is what I'm saying.)TODO:
await queryB()doesn't currently do anything for remote requests, because contents ofremote.dataare ignored in this context unlikeremote.refreshes. it only behaves correctly in SSR.queryB().refresh()does work, howeverPlease don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm testand lint the project withpnpm lintandpnpm checkChangesets
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.Edits