mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
Fixes TanStack Query spamming `No queryFn was passed as an option` in dev whenever a grant-gated query renders without a grant or required variable (such as `branchesQueryOptions` when `projectRef` is `undefined`). **Changed:** - Replaced `queryFn: undefined` in `grantedQueryOptions` with a stable `fallbackQueryFn` while keeping `enabled: false`. This satisfies `useBaseQuery`'s dev-mode check and still delegates to a subscribed granted observer on invalidation (avoiding the `skipToken` shared-cache regression from #53088). - Updated `granted-query-options.test.ts` and `api-keys-query.test.tsx` to verify no `console.error` is logged when rendering without a grant or `projectRef` and that invalidation still refetches via the granted observer. - Updated the `studio-queries` skill note to match. ## To test - Run `pnpm dev:studio` and open a non-branch project's home page. Confirm the browser console and dev terminal no longer spam `[["projects",null,"branches"]]: No queryFn was passed as an option`. - Open Project Settings -> API Keys, create or delete a key, and confirm the list still refreshes cleanly. - Run `pnpm --filter studio test data/granted-query-options.test.ts data/api-keys/api-keys-query.test.tsx`. Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
66 lines
2.4 KiB
TypeScript
66 lines
2.4 KiB
TypeScript
import {
|
|
queryOptions,
|
|
type QueryFunction,
|
|
type QueryKey,
|
|
type UseQueryOptions,
|
|
} from '@tanstack/react-query'
|
|
|
|
/**
|
|
* Type-only marker (no runtime field) on everything `grantedQueryOptions` returns. The type-aware
|
|
* `fga/no-spread-granted-query-options` lint rule finds it by this property name, so don't rename
|
|
* it without updating `scripts/eslint-rules/fga.cjs`.
|
|
*/
|
|
export type GrantedQueryOptionsBrand = { readonly __grantedQueryOptions?: true }
|
|
|
|
/** Caller options for grant-gated factories. The factory owns queryKey/queryFn and gates `enabled`. */
|
|
export type GrantedQueryCallerOptions<
|
|
TQueryFnData,
|
|
TError,
|
|
TData = TQueryFnData,
|
|
TQueryKey extends QueryKey = QueryKey,
|
|
> = Omit<UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>, 'queryKey' | 'queryFn'>
|
|
|
|
/**
|
|
* Fallback `queryFn` for grant-less observers. Avoids TanStack's dev `No queryFn` error (which
|
|
* fires on `undefined` even when `enabled: false`) while still delegating to a granted observer on
|
|
* invalidation (unlike `skipToken`, which fails if a grant-less observer rendered last).
|
|
*/
|
|
const fallbackQueryFn: QueryFunction<never, QueryKey> = (context) => {
|
|
const query = context.client.getQueryCache().find({ queryKey: context.queryKey, exact: true })
|
|
const queryFn = query?.observers.find(
|
|
(o) => typeof o.options.queryFn === 'function' && o.options.queryFn !== fallbackQueryFn
|
|
)?.options.queryFn
|
|
if (typeof queryFn === 'function') return queryFn(context)
|
|
return Promise.reject(new Error(`Missing queryFn: '${query?.queryHash}'`))
|
|
}
|
|
|
|
/**
|
|
* Builds query options for a grant-gated query. Without a grant (or other required variables) the
|
|
* factory passes `queryFn: undefined` and this helper forces `enabled: false` with a delegating
|
|
* fallback `queryFn`.
|
|
*/
|
|
export function grantedQueryOptions<
|
|
TQueryFnData,
|
|
TError,
|
|
TData = TQueryFnData,
|
|
TQueryKey extends QueryKey = QueryKey,
|
|
>(
|
|
{
|
|
queryKey,
|
|
queryFn,
|
|
}: { queryKey: TQueryKey; queryFn: QueryFunction<TQueryFnData, TQueryKey> | undefined },
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: GrantedQueryCallerOptions<TQueryFnData, TError, TData, TQueryKey> = {}
|
|
) {
|
|
const result = queryOptions<TQueryFnData, TError, TData, TQueryKey>({
|
|
...options,
|
|
queryKey,
|
|
queryFn: queryFn ?? fallbackQueryFn,
|
|
enabled: queryFn ? enabled : false,
|
|
})
|
|
const branded: typeof result & GrantedQueryOptionsBrand = result
|
|
return branded
|
|
}
|