Closed denisborovikov closed 1 year ago
Seems that it is unable to infer the correct type for useDefaultOptions's context after i tried.
Cannot pass the hook options into useDefaultOptions. Cuz the hook options is the last options u pass in and it will override useDefaultOptions.
I don't want to add more built-in methods. It will add more complexity to this library. But setData
is different, it let you to have a better typescript experience.
queryKey
is the key of current query, and getKey
is just a static method. If u using getKey
do same thing, it will make more code. as bwlown
const todosQuery = useTodos()
queryClient.invalidateQueries(useTodos.getKey(todosQuery.variables))
Thanks for the reply!
Now you can using middleware to customize ur hook whatever u neeed.
Hey, I just found this library, and it has exciting ideas. Thank you! I have a few questions, though.
I tried to reimplement your optimistic update example but moved the implementation into the mutation definition instead of using the hook instance (a similar question was raised in this issue #21). This implementation is more common because I want to have optimistic updates working for every instance of the hook, no matter how many times I use it in the app and don't copy over the implementation for every instance.
This is what I got codesandbox
I found a few issues there:
useDefaultOptions
returns an options object, theonError/onSettle
context type is inferred incorrectly (infersunknown
instead of{ previousTodos: Todos }
- the return type of theonMutate
callback.If I remove
useDefaultOptions
and pass options tocreateMutation
directly, the context type is inferred correctly.useDefaultOptions
should receive the options object from the hook as an argument. Otherwise, if both options are used, the hook options override the ones fromuseDefaultOptions
and there's no way to access the options used in the hook instance. Make sure options don't override each other.Using the optimistic update code as an example, I want to clear the input after a user clicks the add button:
If I do this, the
onMutate
fromuseAddTodo
replaces theonMutate
increateMutation
.If
useDefaultOptions
would receive options as an argument, it could be fixed like this:setData
forsetQueryData
withqueryClient
andqueryKey
built in. What do you think of adding other most used methods? Now your optimistic updates example looks inconsistent because it used a mixed API. It usestodosQuery.setData()
but at the same timeawait queryClient.cancelQueries(todosQuery.queryKey);
andqueryClient.invalidateQueries(useTodos.getKey())
.It will be nicer if you expose other methods like getData for
queryClient.getQueryData
,invalidateQuery
forqueryClient.invalidateQueries
etc.I.e.
Maybe make it configurable so devs can decide which methods to expose.
queryKey
as a hook return andgetKey()
as a static method?getKey
seems self-sufficient for this purpose.queryKey
does the same, but it makes API look inconsistent.