Compatibility between Mutation
and useMutation
, queryClient.fetchQuery
and useQuery
.
#9038
Unanswered
zacharyweidenbach
asked this question in
Q&A
Replies: 0 comments
# for free
to join this conversation on GitHub.
Already have an account?
# to comment
-
Preface
I 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:
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 discussion
I 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
useMutation
andMutation
as well asqueryClient.fetchQuery
anduseQuery
correctly.Mutations
Here is what a chimeric definition of a mutation looks like for reference.
And how it gets used.
Idiomatic Path
To create idiomatic compatibility with
useMutation
, I am using theMutation
api to achieve this. However, I did notice that theMutation
api needs amutationId
if it is to correctly associate the mutation with the state of auseMutation
instance that uses amutationKey
. I set themutationId
default to0
if it cannot find an existingmutationId
with the providedmutationKey
(if amutationKey
is provided). Is this a sane default if amutationId
cannot be found?Reactive Path
The
mutationOptions
supported by Tanstack query are passed through touseMutation
. Chimeric redefines the types that are allowed to be passed through as those which live in Tanstack query'sMutationOptions
type.Queries
Here is what a chimeric definition of a query looks like for reference.
And how it gets used reactively and idiomatically.
Idiomatic Path
To create idiomatic compatibility with
useQuery
, I am using theQueryClient.fetchQuery
interface. This allows me to access the same cache asuseQuery
such that idiomatic calls will return the cached value instead of refetching.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
refetch
function to do the same thing. The call signature for doing this inchimeric
would look something like:To achieve this with
queryClient.fetchQuery
, I am invalidating the query prior to calling thefetchQuery
.https://github.com/dataquail/chimeric/blob/main/packages/react-query/src/lib/Query/IdiomaticQueryFactory.ts#L44
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
fetchQuery
. It works but feels hacky.Reactive Path
For now, the only override option supported by reactive queries is the
enabled
flag. I may add more if it makes sense, but I don't want to let implementation details leak into thechimeric
core types if I can avoid it. At the time of definition, the ChimericQueryFactory expects to be handed a function that returns aQueryOptions
type defined by Tanstack Query. This side steps the need to redefine the supported passthru types like with chimeric mutations.Conclusion
So 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.
All reactions