Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exhaustive-deps doesn't understand function call for query key #7077

Closed
fdw opened this issue Mar 11, 2024 · 4 comments · Fixed by #7092
Closed

exhaustive-deps doesn't understand function call for query key #7077

fdw opened this issue Mar 11, 2024 · 4 comments · Fixed by #7092
Labels

Comments

@fdw
Copy link

fdw commented Mar 11, 2024

Describe the bug

We want to offer comfort hooks to query and invalidate calls. Of course, they need to use the same query key, so we extracted it like this:

export function useSomeData(id: number) = useQuery({
  queryKey: queryKeyFor(id),
  queryFn: ...
})

export function useQueryCache() {
  const queryClient = useQueryClient()
  return {
    invalidate: (id: number) =>
      queryClient.invalidateQueries({ queryKey: queryKeyFor(id) })
  }
}

function queryKeyFor(id: number) {
  return ['api', id]
}

In practice, this works perfectly. However, the eslint-plugin exhaustive-deps doesn't understand that id is contained in the query key.

Your minimal, reproducible example

n/a

Steps to reproduce

see above

Expected behavior

I expect the hook to understand that id is used in the query key factory, but it's not.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • OS: Linux
  • Browser: n/a

Tanstack Query adapter

react-query

TanStack Query version

@tanstack/eslint-plugin-query@5.20.1

TypeScript version

5.3.3

Additional context

No response

@TkDodo
Copy link
Collaborator

TkDodo commented Mar 11, 2024

we have tests for this, so please show a reproduction:

{
name: 'should not fail when queryKey is a queryKeyFactory while having a dep as first arg',
code: normalizeIndent`
const fooQueryKeyFactory = {
foo: () => ['foo'] as const,
num: (num: number) => [...fooQueryKeyFactory.foo(), num] as const,
}
const useFoo = (num: number) =>
useQuery({
queryKey: fooQueryKeyFactory.foo(num),
queryFn: () => Promise.resolve(num),
})
`,
},

@fdw
Copy link
Author

fdw commented Mar 11, 2024

Here's a reproduction: https://stackblitz.com/edit/vitejs-vite-hutlfq?file=src%2Fhooks.ts
The issue seems to be more subtly about undefined and coalescing. Maybe you have a better idea how to solve this :)

@TkDodo
Copy link
Collaborator

TkDodo commented Mar 11, 2024

thanks for the reproduction. it really seems to be a bug around ??.

This yields no lint error:

queryKey: `blah${id}`,

but this does:

queryKey: `blah${id ?? -1}`,

@Newbie012 could you have a look at this?

@fdw
Copy link
Author

fdw commented Mar 12, 2024

That was quick! Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants