eslint-plugin-query: add support for queryKeyFactories #4862
Send to my agent@Newbie012 I think this is still not supported, right?
const fooQueryKeyFactory = {
foo: () => ['foo'] as const,
num: (num: number) => [...fooQueryKeyFactory.foo(), num] as const,
}
const useFoo = (num: number) =>
useQuery({
queryKey: fooQueryKeyFactory.foo(),
queryFn: () => Promise.resolve(num),
})
Here, I would expect an error because we're using num in the queryFn, but not in the queryKey. The fix would be:
- queryKey: fooQueryKeyFactory.foo(),
+ queryKey: fooQueryKeyFactory.num(num)
This doesn't have to auto-fix (because I think it's impossible), but I think it should error when we're not using num to produce the queryKey. Do you think that's possible ?
TkDodo added labelsenhancement
TkDodo added labelspackage: eslint-plugin-query
Although, I have to ask - what's the added value of separating the queryKey from queryFn?
Yeah, I tend to agree. Query Keys and Query Functions are a pair that should go together. I will try to move away from "query key factories" towards "query factories". However, one advantage is that type inference of the queryFn works better when you inline it, like:
const fooQueryKeyFactory = { foo: () => ['foo'] as const, num: (num: number) => [...fooQueryKeyFactory.foo(), num] as const, } const useFoo = (num: number) => useQuery({ queryKey: fooQueryKeyFactory.foo(num), queryFn: ({ queryKey, signal }) => Promise.resolve(queryKey[1]), // ⬆️ inferred ⬆️ type safe })if you extract
queryKeyandqueryFnto a factory, you have to type annotate:const fooQueryFactory = { foo: () => ['foo'] as const, num: (num: number) => ({ queryKey: [...fooQueryKeyFactory.foo(), num] as const, queryFn: ({ signal }: QueryFunctionContext) => ... // ⬆️ needs manual annotation }) }and then again, you'd need to pass a generic to
QueryFunctionContextif you'd want thequeryKeypassed to it to be properly inferred ...It's funny that I stumbled across this exact issue, so I wrote this utility function (mostly copied from the source code):
function createQuery< TQueryFnData = unknown, TError = unknown, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey >( options: Omit<UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>, "initialData"> & { initialData?: () => undefined; } ) { return options; }This allows me to define inferred + type safe queries as follows:

As a plus, I can write a function
updateOptimisiticallywhich takes a query factory and update it type safely:
In any case, I understand that a lot of the community now is using query key factories, so I hope I'll have enough time today to raise a PR 🙂
Cool! Maybe we should ship this function out of the box 🤔
Which one?
Back to the original issue - I raised a PR
Which one?
the
createQueryone :)TkDodo closed this
Sign in to join this conversation.
Correct, it seems like query key factories aren't supported yet, but I can start working on it if you want. It shouldn't be complicated.
https://github.com/TanStack/query/blob/main/packages/eslint-plugin-query/src/rules/exhaustive-deps/exhaustive-deps.rule.ts#L72-L75
Although, I have to ask - what's the added value of separating the
queryKeyfromqueryFn? It might be a bit off-topic, but it reminds me back in the old days when there were no components in AngularJS, so each time we want to define a page or modal, we had to pass bothtemplateUrl(html) andcontroller(js), until the Angular team introduced components.