Skip to content
Source
Sign inSign up
TanStack/query

eslint-plugin-query: add support for queryKeyFactories #4862

Send to my agent
ClosedTkDodo opened this issue 6 comments
TkDodocommented

@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 ?

  1. TkDodo added labelsenhancement

  2. TkDodo added labelspackage: eslint-plugin-query

  3. 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 queryKey from queryFn? 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 both templateUrl (html) and controller (js), until the Angular team introduced components.

  4. 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 queryKey and queryFn to 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 QueryFunctionContext if you'd want the queryKey passed to it to be properly inferred ...

  5. 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:

    image

    As a plus, I can write a function updateOptimisitically which takes a query factory and update it type safely:

    image

    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 🙂

  6. Cool! Maybe we should ship this function out of the box 🤔

  7. Which one?

    Back to the original issue - I raised a PR

  8. Which one?

    the createQuery one :)

  9. TkDodo closed this

Sign in to join this conversation.