Skip to content

feat(react-query): add support for async iterables in mutations - #5850

Closed
KATT wants to merge 1 commit into
nextfrom
issues/5846-feat-mut
Closed

KATT wants to merge 1 commit into
nextfrom
issues/5846-feat-mut

Conversation

@KATT

@KATT KATT commented Jul 2, 2024

Copy link
Copy Markdown
Member

Closes #5846

🎯 Changes

Not done, need feedback

βœ… 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.

@railway-app
railway-app Bot temporarily deployed to trpc-pr-5850 July 2, 2024 20:40 Destroyed
@railway-app
railway-app Bot temporarily deployed to trpc-pr-5850 July 2, 2024 20:40 Destroyed
@railway-app

railway-app Bot commented Jul 2, 2024 •

Copy link
Copy Markdown

πŸš… Previously deployed to Railway in the trpc-sse-and-websockets project. Environment has been deleted.

@vercel

vercel Bot commented Jul 2, 2024 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git β†—οΈŽ

Name Status Preview Comments Updated (UTC)
next-prisma-starter βœ… Ready (Inspect) Visit Preview Jul 2, 2024 8:42pm
og-image βœ… Ready (Inspect) Visit Preview πŸ’¬ Add feedback Jul 2, 2024 8:42pm
www βœ… Ready (Inspect) Visit Preview πŸ’¬ Add feedback Jul 2, 2024 8:42pm

Comment on lines +284 to +306
mutationFn: async (input) => {
setDataOverride(null);
const result = await client.mutation(
...getClientArgs([path, { input }], opts),
);

if (!isAsyncIterable(result)) {
return result;
}

// `mutateAsync()` returns an async iterable
async function* iterable() {
setDataOverride([]);
for await (const value of result as AsyncIterable<unknown>) {
setDataOverride((prev) => {
const next = [...(prev ?? []), value];
return next;
});
yield value;
}
}

return iterable();

@KATT KATT Jul 2, 2024 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @TkDodo πŸ‘€

is there a way for me to make the mutation stay as pending but still have mutateAsync return the result?

tl;dr

const mutation = trpc.iterable.useMutation()

mutation.data // <--- should be an array that gets updated
mutation.isPending // should probably be true until we have finished receiving all results
// [...]
await mutation.mutateAsync(); // <--- should return an `AsyncIterable`

I'm not sure how to access the MutationObserver within the function here

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. pending state is defined as "we have no data".

I'm not sure how to access the MutationObserver within the function here

It's not exposed. You would probably have to roll your own useMutation. If you look at the code, it doesn't do that much: Basically create an observer, subscribe to it in useSyncExternalStore and return the result ...

@KATT KATT closed this Sep 6, 2024
@KATT KATT added the ❄️ icebox features / PRs that might be pick up in the future and can be handy to keep track of label Sep 6, 2024
@github-actions

github-actions Bot commented Sep 7, 2024

Copy link
Copy Markdown
Contributor

This pull request has been locked because we are very unlikely to see comments on closed issues. If you think, this PR is still necessary, create a new one with the same branch. Thank you.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Sep 7, 2024
@KATT
KATT deleted the issues/5846-feat-mut branch November 1, 2024 11:24

This branch was successfully deployed

5 active deployments
Preview – www β€” 3d64d98d Deployed Jul 2, 2024 by vercel[bot]
Preview – trpc-next-app-dir β€” 3d64d98d Deployed Jul 2, 2024 by vercel[bot]
Preview – next-prisma-starter β€” 3d64d98d Deployed Jul 2, 2024 by vercel[bot]
Preview – examples-todomvc β€” 3d64d98d Deployed Jul 2, 2024 by vercel[bot]
Preview – og-image β€” 3d64d98d Deployed Jul 2, 2024 by vercel[bot]
trpc-pr-5850 β€” 3d64d98d Deployed Jul 2, 2024 by railway-app[bot]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

❄️ icebox features / PRs that might be pick up in the future and can be handy to keep track of

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: support for Streaming responses to be reflected in useMutation data

2 participants