Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tough-queries-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/eslint-plugin-query': patch
---

Recognize `query` and `infiniteQuery` client calls in the `prefer-query-options` rule.
Original file line number Diff line number Diff line change
Expand Up @@ -221,23 +221,6 @@ describe('infiniteQueryOptions', () => {
// @ts-expect-error cannot pass infinite options to non-infinite query functions
queryClient.query(options),
)

// deprecated methods below to be removed next major version
assertType(
// @ts-expect-error cannot pass infinite options to non-infinite query functions
// eslint-disable-next-line no-restricted-syntax -- grandfathered direct test
queryClient.ensureQueryData(options),
)
assertType(
// @ts-expect-error cannot pass infinite options to non-infinite query functions
// eslint-disable-next-line no-restricted-syntax -- grandfathered direct test
queryClient.fetchQuery(options),
)
assertType(
// @ts-expect-error cannot pass infinite options to non-infinite query functions
// eslint-disable-next-line no-restricted-syntax -- grandfathered direct test
queryClient.prefetchQuery(options),
)
})

it('should allow optional initialData function', () => {
Expand Down
102 changes: 10 additions & 92 deletions packages/eslint-plugin-query/src/__tests__/no-void-query-fn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,66 +204,29 @@ ruleTester.run('no-void-query-fn', rule, {
`,
},
{
name: 'fetchQuery queryFn returns a value',
name: 'query queryFn returns a value',
code: normalizeIndent`
import { QueryClient } from '@tanstack/react-query'

const queryClient = new QueryClient()
queryClient.fetchQuery({
queryClient.query({
queryKey: ['test'],
queryFn: () => fetch('/api/test').then((r) => r.json()),
})
`,
},
{
name: 'prefetchQuery queryFn returns a value',
name: 'infiniteQuery queryFn returns a value',
code: normalizeIndent`
import { QueryClient } from '@tanstack/react-query'

const queryClient = new QueryClient()
queryClient.prefetchQuery({
queryKey: ['test'],
queryFn: () => fetch('/api/test').then((r) => r.json()),
})
`,
},
{
name: 'prefetchInfiniteQuery queryFn returns a value',
code: normalizeIndent`
import { QueryClient } from '@tanstack/react-query'

const queryClient = new QueryClient()
queryClient.prefetchInfiniteQuery({
queryKey: ['test'],
queryFn: ({ pageParam }: { pageParam: number }) =>
fetch(\`/api/test?page=\${pageParam}\`).then((r) => r.json()),
initialPageParam: 0,
})
`,
},
{
name: 'ensureQueryData queryFn returns a value',
code: normalizeIndent`
import { QueryClient } from '@tanstack/react-query'

const queryClient = new QueryClient()
queryClient.ensureQueryData({
queryKey: ['test'],
queryFn: () => fetch('/api/test').then((r) => r.json()),
})
`,
},
{
name: 'ensureInfiniteQueryData queryFn returns a value',
code: normalizeIndent`
import { QueryClient } from '@tanstack/react-query'

const queryClient = new QueryClient()
queryClient.ensureInfiniteQueryData({
queryClient.infiniteQuery({
queryKey: ['test'],
queryFn: ({ pageParam }: { pageParam: number }) =>
fetch(\`/api/test?page=\${pageParam}\`).then((r) => r.json()),
initialPageParam: 0,
getNextPageParam: () => undefined,
})
`,
},
Expand Down Expand Up @@ -555,58 +518,12 @@ ruleTester.run('no-void-query-fn', rule, {
errors: [{ messageId: 'noVoidReturn' }],
},
{
name: 'fetchQuery queryFn returns void',
code: normalizeIndent`
import { QueryClient } from '@tanstack/react-query'

const queryClient = new QueryClient()
queryClient.fetchQuery({
queryKey: ['test'],
queryFn: async () => {
await fetch('/api/test')
},
})
`,
errors: [{ messageId: 'noVoidReturn' }],
},
{
name: 'prefetchQuery queryFn returns void',
code: normalizeIndent`
import { QueryClient } from '@tanstack/react-query'

const queryClient = new QueryClient()
queryClient.prefetchQuery({
queryKey: ['test'],
queryFn: async () => {
await fetch('/api/test')
},
})
`,
errors: [{ messageId: 'noVoidReturn' }],
},
{
name: 'prefetchInfiniteQuery queryFn returns void',
code: normalizeIndent`
import { QueryClient } from '@tanstack/react-query'

const queryClient = new QueryClient()
queryClient.prefetchInfiniteQuery({
queryKey: ['test'],
queryFn: async ({ pageParam }: { pageParam: number }) => {
await fetch(\`/api/test?page=\${pageParam}\`)
},
initialPageParam: 0,
})
`,
errors: [{ messageId: 'noVoidReturn' }],
},
{
name: 'ensureQueryData queryFn returns void',
name: 'query queryFn returns void',
code: normalizeIndent`
import { QueryClient } from '@tanstack/react-query'

const queryClient = new QueryClient()
queryClient.ensureQueryData({
queryClient.query({
queryKey: ['test'],
queryFn: async () => {
await fetch('/api/test')
Expand All @@ -616,17 +533,18 @@ ruleTester.run('no-void-query-fn', rule, {
errors: [{ messageId: 'noVoidReturn' }],
},
{
name: 'ensureInfiniteQueryData queryFn returns void',
name: 'infiniteQuery queryFn returns void',
code: normalizeIndent`
import { QueryClient } from '@tanstack/react-query'

const queryClient = new QueryClient()
queryClient.ensureInfiniteQueryData({
queryClient.infiniteQuery({
queryKey: ['test'],
queryFn: async ({ pageParam }: { pageParam: number }) => {
await fetch(\`/api/test?page=\${pageParam}\`)
},
initialPageParam: 0,
getNextPageParam: () => undefined,
})
`,
errors: [{ messageId: 'noVoidReturn' }],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ describe('prefer-query-options', () => {
const queryClient = useQueryClient()

function run(queryClient) {
queryClient.fetchQuery({
queryClient.query({
queryKey: ['todos'],
queryFn: () => fetchTodos(),
})
Expand All @@ -232,20 +232,20 @@ describe('prefer-query-options', () => {
`,
},
{
name: 'non-queryClient fetchQuery call is ignored',
name: 'non-queryClient query call is ignored',
code: normalizeIndent`
import { useQuery } from '@tanstack/react-query'

const analytics = {
fetchQuery(options) {
query(options) {
return options
},
}

function Component() {
useQuery(todosOptions)

analytics.fetchQuery({
analytics.query({
queryKey: ['todos'],
queryFn: () => fetchTodos(),
})
Expand Down Expand Up @@ -581,13 +581,13 @@ describe('prefer-query-options', () => {
valid: [],
invalid: [
{
name: 'client.fetchQuery with inline queryKey + queryFn',
name: 'client.query with inline queryKey + queryFn',
code: normalizeIndent`
import { useQueryClient } from '@tanstack/react-query'

function Component() {
const client = useQueryClient()
client.fetchQuery({
client.query({
queryKey: ['todos'],
queryFn: () => fetchTodos(),
})
Expand All @@ -603,7 +603,7 @@ describe('prefer-query-options', () => {

function Component() {
const client = getClient()
client.fetchQuery({
client.query({
queryKey: ['todos'],
queryFn: () => fetchTodos(),
})
Expand All @@ -619,7 +619,7 @@ describe('prefer-query-options', () => {

const queryClient = new Client()

queryClient.fetchQuery({
queryClient.query({
queryKey: ['todos'],
queryFn: () => fetchTodos(),
})
Expand Down Expand Up @@ -661,13 +661,13 @@ describe('prefer-query-options', () => {
valid: [],
invalid: [
{
name: 'queryClient.fetchQuery with inline queryKey + queryFn',
name: 'queryClient.query with inline queryKey + queryFn',
code: normalizeIndent`
import { useQueryClient } from '@tanstack/react-query'

function Component() {
const queryClient = useQueryClient()
queryClient.fetchQuery({
queryClient.query({
queryKey: ['todos'],
queryFn: () => fetchTodos(),
})
Expand All @@ -677,81 +677,13 @@ describe('prefer-query-options', () => {
errors: [{ messageId: 'preferQueryOptions' }],
},
{
name: 'queryClient.prefetchQuery with inline queryKey + queryFn',
name: 'queryClient.infiniteQuery with inline queryKey + queryFn',
code: normalizeIndent`
import { useQueryClient } from '@tanstack/react-query'

function Component() {
const queryClient = useQueryClient()
queryClient.prefetchQuery({
queryKey: ['todos'],
queryFn: () => fetchTodos(),
})
return null
}
`,
errors: [{ messageId: 'preferQueryOptions' }],
},
{
name: 'queryClient.fetchInfiniteQuery with inline queryKey + queryFn',
code: normalizeIndent`
import { useQueryClient } from '@tanstack/react-query'

function Component() {
const queryClient = useQueryClient()
queryClient.fetchInfiniteQuery({
queryKey: ['todos'],
queryFn: ({ pageParam }) => fetchTodos(pageParam),
initialPageParam: 0,
getNextPageParam: (lastPage) => lastPage.nextCursor,
})
return null
}
`,
errors: [{ messageId: 'preferQueryOptions' }],
},
{
name: 'queryClient.prefetchInfiniteQuery with inline queryKey + queryFn',
code: normalizeIndent`
import { useQueryClient } from '@tanstack/react-query'

function Component() {
const queryClient = useQueryClient()
queryClient.prefetchInfiniteQuery({
queryKey: ['todos'],
queryFn: ({ pageParam }) => fetchTodos(pageParam),
initialPageParam: 0,
getNextPageParam: (lastPage) => lastPage.nextCursor,
})
return null
}
`,
errors: [{ messageId: 'preferQueryOptions' }],
},
{
name: 'queryClient.ensureQueryData with inline queryKey + queryFn',
code: normalizeIndent`
import { useQueryClient } from '@tanstack/react-query'

function Component() {
const queryClient = useQueryClient()
queryClient.ensureQueryData({
queryKey: ['todos'],
queryFn: () => fetchTodos(),
})
return null
}
`,
errors: [{ messageId: 'preferQueryOptions' }],
},
{
name: 'queryClient.ensureInfiniteQueryData with inline queryKey + queryFn',
code: normalizeIndent`
import { useQueryClient } from '@tanstack/react-query'

function Component() {
const queryClient = useQueryClient()
queryClient.ensureInfiniteQueryData({
queryClient.infiniteQuery({
queryKey: ['todos'],
queryFn: ({ pageParam }) => fetchTodos(pageParam),
initialPageParam: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const queriesHooks = ['useQueries', 'useSuspenseQueries']
const filterHooks = ['useIsFetching']

const queryClientOptionMethods = [
'query',
'infiniteQuery',
'fetchQuery',
'prefetchQuery',
'fetchInfiniteQuery',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,23 +231,6 @@ describe('infiniteQueryOptions', () => {
// @ts-expect-error cannot pass infinite options to non-infinite query functions
queryClient.query(options),
)

// deprecated methods below to be removed next major version
assertType(
// @ts-expect-error cannot pass infinite options to non-infinite query functions
// eslint-disable-next-line no-restricted-syntax -- grandfathered direct test
queryClient.ensureQueryData(options),
)
assertType(
// @ts-expect-error cannot pass infinite options to non-infinite query functions
// eslint-disable-next-line no-restricted-syntax -- grandfathered direct test
queryClient.fetchQuery(options),
)
assertType(
// @ts-expect-error cannot pass infinite options to non-infinite query functions
// eslint-disable-next-line no-restricted-syntax -- grandfathered direct test
queryClient.prefetchQuery(options),
)
})

it('allow optional initialData function', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/query-core/src/__tests__/queryClient.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1955,7 +1955,7 @@ describe('queryClient', () => {
gcTime: 10,
})
.catch(noop)
expect(queryCache.find({ queryKey: key })).toBeDefined()
expect(queryCache.find({ queryKey: key })?.state.data).toBe('data')
await vi.advanceTimersByTimeAsync(15)
expect(queryCache.find({ queryKey: key })).not.toBeDefined()
})
Expand Down
Loading