solidjs-community / mediakit

A set of utilities to use with your Solid apps.
https://mediakit-taupe.vercel.app
137 stars 14 forks source link

How to revalidate PRPC query after a mutation #107

Closed alrightsure closed 1 month ago

alrightsure commented 1 month ago

Hello All,

I love the ease of use of PRPC, but am still confused as to how to use it properly. If I have a query and a mutation on the same page, how would I invalidate/refetch the query after the mutation fires? I have tried calling [query].reftch() in the onSuccess of the mutation, but that doesn't always seem to work, and the revalidate function from the solid router also doesn't seem to do the trick.

OrJDev commented 1 month ago

hey there, after reading this issue i decided to create a new utility function, can you please upgrade to the latest api and try this new function?

https://mediakit-taupe.vercel.app/prpc/useutils


const utils = testQuery2.useUtils()

const helloRes1 = testQuery2(() => ({
  hello: 'jdev',
}))
const helloRes2 = testQuery2(() => ({
  hello: 'jdev22',
}))

const helloMutation = testMutation(() => ({
  async onSuccess() {
    console.log('invalidate all testQuery2')
    await utils.invalidate()

    // invalidate only  jdev
    await utils.invalidate({ hello: 'jdev' })

    // invalidate only jdev22
    await utils.invalidate({ hello: 'jdev22' })
  },
}))

just lmk if it works for you as expected

alrightsure commented 1 month ago

Awesome!

It looks like the revalidation still seems iffy like the refetch, where it will work after the first mutation but then not after the next. I'll see if I can make a minimal reproduction tonight and get back to you.

OrJDev commented 1 month ago

Awesome!

It looks like the revalidation still seems iffy like the refetch, where it will work after the first mutation but then not after the next. I'll see if I can make a minimal reproduction tonight and get back to you.

Alright. If you can't do a repro, could you just send me the part where you calls the mutation & revalidate

OrJDev commented 1 month ago

Please reopen with more info