vuejs / apollo

🚀 Apollo/GraphQL integration for VueJS
http://apollo.vuejs.org
MIT License
6.03k stars 523 forks source link

feat: add updateQuery to useQuery #1552

Closed syllomex closed 3 months ago

syllomex commented 6 months ago

The updateQuery is something I use a lot while developing in React. I find it particularly useful because, in many cases, it eliminates the need to use readQuery and writeQuery. I just created a function that calls the existing updateQuery function in the ObservableQuery and returned it in the useQuery, so the cache of a query can be modified like this:

selectedChannelQuery.updateQuery(previousResult => ({
  ...previousResult,
  channel: {
    ...previousResult.channel,
    messages: [
      ...previousResult.channel.messages,
      result?.data.message,
    ],
  },
}))