-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
feat(eslint-plugin-query): add prefer-query-options rule #10359
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1ec11e9
feat(eslint-plugin-query): add prefer-query-options rule
Newbie012 7d51d46
add changeset
Newbie012 3a8e0f4
ci: apply automated fixes
autofix-ci[bot] afd00a9
address review comments
Newbie012 26737a2
Merge branch 'feat-eslint-prefer-query-options' of github.com:TanStacβ¦
Newbie012 aec9ed5
update example
Newbie012 c46f6fe
add coverage
Newbie012 87d26cf
Merge branch 'main' into feat-eslint-prefer-query-options
Newbie012 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@tanstack/eslint-plugin-query': minor | ||
| --- | ||
|
|
||
| Add `prefer-query-options` rule and `recommendedStrict` config |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| --- | ||
| id: prefer-query-options | ||
| title: Prefer the use of queryOptions | ||
| --- | ||
|
|
||
| Separating `queryKey` and `queryFn` can cause unexpected runtime issues when the same query key is accidentally used with more than one `queryFn`. Wrapping them in `queryOptions` (or `infiniteQueryOptions`) co-locates the key and function, making queries safer and easier to reuse. | ||
|
|
||
| ## Rule Details | ||
|
|
||
| Examples of **incorrect** code for this rule: | ||
|
|
||
| ```tsx | ||
| /* eslint "@tanstack/query/prefer-query-options": "error" */ | ||
|
|
||
| function Component({ id }) { | ||
| const query = useQuery({ | ||
| queryKey: ['get', id], | ||
| queryFn: () => Api.get(`/foo/${id}`), | ||
| }) | ||
| // ... | ||
| } | ||
| ``` | ||
|
|
||
| ```tsx | ||
| /* eslint "@tanstack/query/prefer-query-options": "error" */ | ||
|
|
||
| function useFooQuery(id) { | ||
| return useQuery({ | ||
| queryKey: ['get', id], | ||
| queryFn: () => Api.get(`/foo/${id}`), | ||
| }) | ||
| } | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule: | ||
|
|
||
| ```tsx | ||
| /* eslint "@tanstack/query/prefer-query-options": "error" */ | ||
|
|
||
| function getFooOptions(id) { | ||
| return queryOptions({ | ||
| queryKey: ['get', id], | ||
| queryFn: () => Api.get(`/foo/${id}`), | ||
| }) | ||
| } | ||
|
|
||
| function Component({ id }) { | ||
| const query = useQuery(getFooOptions(id)) | ||
| // ... | ||
| } | ||
| ``` | ||
|
|
||
| ```tsx | ||
| /* eslint "@tanstack/query/prefer-query-options": "error" */ | ||
|
|
||
| function getFooOptions(id) { | ||
| return queryOptions({ | ||
| queryKey: ['get', id], | ||
| queryFn: () => Api.get(`/foo/${id}`), | ||
| }) | ||
| } | ||
|
|
||
| function useFooQuery(id) { | ||
| return useQuery({ ...getFooOptions(id), select: (data) => data.foo }) | ||
| } | ||
| ``` | ||
|
|
||
| The rule also enforces reusing `queryKey` from a `queryOptions` result instead of typing it manually in `QueryClient` methods or filters. | ||
|
|
||
| Examples of **incorrect** `queryKey` references for this rule: | ||
|
|
||
| ```tsx | ||
| /* eslint "@tanstack/query/prefer-query-options": "error" */ | ||
|
|
||
| function todoOptions(id) { | ||
| return queryOptions({ | ||
| queryKey: ['todo', id], | ||
| queryFn: () => api.getTodo(id), | ||
| }) | ||
| } | ||
|
|
||
| function Component({ id }) { | ||
| const queryClient = useQueryClient() | ||
| return queryClient.getQueryData(['todo', id]) | ||
| } | ||
| ``` | ||
|
|
||
| ```tsx | ||
| /* eslint "@tanstack/query/prefer-query-options": "error" */ | ||
|
|
||
| function todoOptions(id) { | ||
| return queryOptions({ | ||
| queryKey: ['todo', id], | ||
| queryFn: () => api.getTodo(id), | ||
| }) | ||
| } | ||
|
|
||
| function Component({ id }) { | ||
| const queryClient = useQueryClient() | ||
| return queryClient.invalidateQueries({ queryKey: ['todo', id] }) | ||
| } | ||
| ``` | ||
|
|
||
| Examples of **correct** `queryKey` references for this rule: | ||
|
|
||
| ```tsx | ||
| /* eslint "@tanstack/query/prefer-query-options": "error" */ | ||
|
|
||
| function todoOptions(id) { | ||
| return queryOptions({ | ||
| queryKey: ['todo', id], | ||
| queryFn: () => api.getTodo(id), | ||
| }) | ||
| } | ||
|
|
||
| function Component({ id }) { | ||
| const queryClient = useQueryClient() | ||
| return queryClient.getQueryData(todoOptions(id).queryKey) | ||
| } | ||
| ``` | ||
|
|
||
| ```tsx | ||
| /* eslint "@tanstack/query/prefer-query-options": "error" */ | ||
|
|
||
| function todoOptions(id) { | ||
| return queryOptions({ | ||
| queryKey: ['todo', id], | ||
| queryFn: () => api.getTodo(id), | ||
| }) | ||
| } | ||
|
|
||
| function Component({ id }) { | ||
| const queryClient = useQueryClient() | ||
| return queryClient.invalidateQueries({ queryKey: todoOptions(id).queryKey }) | ||
| } | ||
| ``` | ||
|
|
||
| ## When Not To Use It | ||
|
|
||
| If you do not want to enforce the use of `queryOptions` in your codebase, you will not need this rule. | ||
|
|
||
| ## Attributes | ||
|
|
||
| - [x] β Recommended (strict) | ||
| - [ ] π§ Fixable | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
examples/react/eslint-plugin-demo/src/prefer-query-options-demo.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import { useQuery, useQueryClient } from '@tanstack/react-query' | ||
| import { todoOptions, todosOptions } from './queries' | ||
|
|
||
| // β passes: consuming imported queryOptions result directly | ||
| export function Todos() { | ||
| const query = useQuery(todosOptions) | ||
| return query.data | ||
| } | ||
|
|
||
| // β passes: spreading imported queryOptions result with additional options | ||
| export function Todo({ id }: { id: string }) { | ||
| const query = useQuery({ | ||
| ...todoOptions(id), | ||
| select: (data) => data.title, | ||
| }) | ||
| return query.data | ||
| } | ||
|
|
||
| // β passes: referencing queryKey from imported queryOptions result | ||
| export function invalidateTodos() { | ||
| const queryClient = useQueryClient() | ||
| queryClient.invalidateQueries({ queryKey: todosOptions.queryKey }) | ||
| } | ||
|
|
||
| // β fails: inline queryKey + queryFn should use queryOptions() | ||
| export function TodosBad() { | ||
| const query = useQuery( | ||
| // eslint-disable-next-line @tanstack/query/prefer-query-options -- Prefer using queryOptions() or infiniteQueryOptions() to co-locate queryKey and queryFn. | ||
| { | ||
| queryKey: ['todos'], | ||
| queryFn: () => fetch('/api/todos').then((r) => r.json()), | ||
| }, | ||
| ) | ||
| return query.data | ||
| } | ||
|
|
||
| // β fails: inline queryKey should reference queryOptions result | ||
| export function InvalidateTodosBad() { | ||
| const queryClient = useQueryClient() | ||
| // eslint-disable-next-line @tanstack/query/prefer-query-options -- Prefer referencing a queryKey from a queryOptions() result instead of typing it manually. | ||
| queryClient.invalidateQueries({ queryKey: ['todos'] }) | ||
| } | ||
|
|
||
| // β fails: inline queryKey as direct parameter | ||
| export function GetTodosDataBad() { | ||
| const queryClient = useQueryClient() | ||
| // eslint-disable-next-line @tanstack/query/prefer-query-options -- Prefer referencing a queryKey from a queryOptions() result instead of typing it manually. | ||
| return queryClient.getQueryData(['todos']) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { queryOptions } from '@tanstack/react-query' | ||
|
|
||
| // β passes: queryKey and queryFn are co-located via queryOptions() | ||
| export const todosOptions = queryOptions({ | ||
| queryKey: ['todos'], | ||
| queryFn: () => fetchTodos(), | ||
| }) | ||
|
|
||
| // β passes: factory function wrapping queryOptions() | ||
| export const todoOptions = (id: string) => | ||
| queryOptions({ | ||
| queryKey: ['todo', id], | ||
| queryFn: () => fetchTodo(id), | ||
| }) | ||
|
|
||
| function fetchTodos(): Promise<Array<{ id: string; title: string }>> { | ||
| throw new Error('not implemented') | ||
| } | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| function fetchTodo(id: string): Promise<{ id: string; title: string }> { | ||
| throw new Error('not implemented') | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.