Skip to content

[RFC] feat(react-query server client) Streaming results from mutations and queries - #4489

Closed
iamnafets wants to merge 10 commits into
trpc:mainfrom
Optick-Labs:main
Closed

iamnafets wants to merge 10 commits into
trpc:mainfrom
Optick-Labs:main

Conversation

@iamnafets

@iamnafets iamnafets commented Jun 6, 2023 •

Copy link
Copy Markdown
Contributor

Closes #4477

🎯 Changes

This change (1) introduces a queryGenerator and mutationGenerator method to the client, and (2) adds a streamingData state to useQuery and useMutation. The developer experience is that you can now implement your query or mutation function with an AsyncGenerator (e.g. async function*) and retrieve every emitted event on the client side.

Caveats:

  • This is currently a proof of concept. It's working (deploying to production soon), but probably has lots of rough edges.
  • I have not yet implemented code for the wslink etc as I'd like to get some agreement on the API first.
  • I've abused next-minimal-starter, I'll fix that (among other things) before requesting a proper review.

Notes:

  • I like the idea of having an async generator implementation of query and mutation. It forces the developer to think properly about the boundaries of the request as opposed to callbacks or observables which seem like they can be persistent but actually are not.
  • I'm totally abusing @Sheraff 's streaming HTTP link here by making the observables properly observable and having multiple returns for a given call.
  • When streaming is not supported by the link, it seems sensible to just fall back to the last element passed over the wire. This enables a graceful fallback for cases where you're using streaming to progressively improve the UI (e.g. OpenAI APIs) but the fidelity of the final message is most important.
  • I experimented with a version of useQuery that simply overrode the data through setQueryData each time it had a new chunk. This blew up the loading semantics and felt rather hacky so the streamingData state is a bit cleaner.

✅ Checklist

  • I have followed the steps listed in the Contributing guide.
  • If necessary, I have added documentation related to the changes made.
  • I have added or updated the tests related to the changes made.

@iamnafets
iamnafets requested a review from a team as a code owner June 6, 2023 05:36
@vercel

vercel Bot commented Jun 6, 2023

Copy link
Copy Markdown

@iamnafets is attempting to deploy a commit to the trpc Team on Vercel.

A member of the Team first needs to authorize it.

@iamnafets iamnafets mentioned this pull request Jun 6, 2023
1 task done
@iamnafets iamnafets changed the title [RFC] Streaming results from mutations and queries [RFC] feat(react-query server client) Streaming results from mutations and queries Jun 6, 2023
@iamnafets
iamnafets marked this pull request as draft June 6, 2023 05:52
@Sheraff

Sheraff commented Jun 6, 2023

Copy link
Copy Markdown
Contributor
  • Could you give a description of the resulting API? I feel a little overwhelmed to have to read a 5k LOC changes PR to understand it. (like what is the shape of the request, how is it streamed back, what happens on the client to make the data useable by the user?)
  • Could you try running pnpm format:write so that changes that are just formatting issues don't appear in the PR? That'll trim down what I need to focus on
  • On the client side, during my streaming PR we debated on the use of async generators / iterators and it was concluded that they're fine on the server, but should be avoided on the client for compatibility reasons
  • this PR adds a lot of client-side code, and there is a goal to try and keep it small where possible. Maybe you should consider a separate link so that loading all of this code is optional?

@sachinraja

sachinraja commented Jun 8, 2023 •

Copy link
Copy Markdown
Member

I really wish you hadn't done this much work already because I don't think a generator is the right API for streaming. This also complicates the code a lot right now and in the future (adds 2 new procedure types!). Something like what SvelteKit does would probably be better.

@iamnafets

Copy link
Copy Markdown
Contributor Author

I really wish you hadn't done this much work already because I don't think a generator is the right API for streaming. This also complicates the code a lot right now and in the future (adds 2 new procedure types!). Something like what SvelteKit does would probably be better.

I'm not too worried about it. This is mostly a draft. If you have a better idea on what the API should be, feel free to comment on the attached feature issue.

@Sheraff

Sheraff commented Jun 8, 2023 •

Copy link
Copy Markdown
Contributor

Remix does something similar to what SvelteKit does, doesn't it? I find their implementation a little more readable:

and they only parse promises at level 0 of the object, no recursion, nesting, ... It's not too complicated to do.

But my eternal question is always the same: what do you give the client and when, what to do put in the cache? Receiving a const { data } = useQuery() where data contains promises (or observables) isn't very handy.

And if you want an "arbitrarily long list" of promises (à la openAI API for chatGPT) you need recursion. Which can probably be done, but again, not sure about the client-side DX.

@vercel

vercel Bot commented Aug 20, 2023 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
trpc-next-app-dir ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 22, 2023 6:06am

@KATT

KATT commented Nov 17, 2023

Copy link
Copy Markdown
Member

Superseded #4911

@KATT KATT closed this Nov 17, 2023
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Dec 17, 2023

This branch was successfully deployed

2 active deployments
Preview – trpc-next-app-dir — cdb20706 Deployed Sep 22, 2023 by vercel[bot]
Preview – todomvc — cdb20706 Deployed Sep 22, 2023 by vercel[bot]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: Streaming Mutations / Queries

4 participants