Compatibility between Mutation
and useMutation
, queryClient.fetchQuery
and useQuery
.
#9038
-
PrefaceI am in the process of writing a React interface framework called Chimeric. It is special kind of interface generator that creates both idiomatic and reactive methods that can be called outside of react (idiomatic path), or within react as a hook (reactive path). These two methods are fused together into a singular type in order to colocate the functionality that is abstracted behind the interface, and to reduce the mental burden of having to reason about your code in reactive and non-reactive terms when you need to eject from react to orchestrate more complicated logic idiomatically. The end result is code that looks like this: // idiomatic
const todos = getTodos();
// reactively within a React component/hook
const todos = getTodos.use(); The name I came up with to describe this duality is a "chimeric" interface, hence the name of the project. The library itself has a core that is not coupled to tanstack query. However, it does express the idea of a "mutation" and a "query" in a way that is generalized. The idea there is that chimeric's definition of a mutation or a query can be reimplemented using tanstack query or some other library, like RTK query (I haven't gotten there yet). Reason for the discussionI am hoping to get some feedback from people with the deepest knowledge of the inner workings of tanstack query to see if I have implemented the compatibility between MutationsHere is what a chimeric definition of a mutation looks like for reference. Idiomatic PathTo create idiomatic compatibility with Reactive PathThe QueriesHere is what a chimeric definition of a query looks like for reference. Idiomatic PathTo create idiomatic compatibility with However, sometimes it is useful to be able to imperatively force a query to refetch its data. I only allow this on the idiomatic member because the reactive member provides a const refetchedData = someChimericQuery({ options: { forceRefetch: true } }); To achieve this with Is this the recommended way to achieve such functionality? I did not see anything in the documentation to indicate there was an argument to achieve this directly with Reactive PathFor now, the only override option supported by reactive queries is the ConclusionSo far I have been able to achieve compatibility with idiomatic and reactive versions of mutations and queries, but any feedback for pitfalls or gotchas are welcome. Thanks for this awesome tool and community. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
you’d want query/packages/query-core/src/mutationCache.ts Lines 96 to 111 in 19b95c4 That will give you a
invalidation refetches queries if they are active. calling
without the invalidation part
that’s perfect. |
Beta Was this translation helpful? Give feedback.
you’d want
queryClient.getMutationCache().build(...)
:query/packages/query-core/src/mutationCache.ts
Lines 96 to 111 in 19b95c4