mirror of
https://github.com/supabase/supabase.git
synced 2026-12-03 23:18:57 +00:00
## Context Grant-gated query factories returned `queryFn: skipToken` when the caller had no grant. TanStack Query keeps one options object per cache entry, overwritten by whichever observer rendered last. When that was a grant-less observer, invalidation refetched with `skipToken` and failed with `Missing queryFn`, breaking the query for every other observer on the key. #53075 patched two call sites; this fixes the factories. ## Changes - New `grantedQueryOptions` helper (`data/granted-query-options.ts`). Without a grant, the factory sets `queryFn: undefined` instead of `skipToken`, so TanStack borrows the query function from a subscribed observer that has one. The caller's `enabled` applies only when the factory has a query function. - `apiKeysQueryOptions`, `revealedApiKeysQueryOptions`, `branchesQueryOptions`, `branchQueryOptions` and `branchDiffQueryOptions` take caller options as a second argument. Call sites change from `useQuery({ ...f(vars), enabled })` to `useQuery(f(vars, { enabled }))`, so callers can't override the grant gate. - New type-aware `fga/no-spread-granted-query-options` lint rule (error). `grantedQueryOptions` returns a type-only brand, and the rule flags any spread or `Object.assign` of a branded value, however it was built or named. - Restores typed linting as a separate pass (`eslint.type-checks.config.cjs`, run by `pnpm lint` via `lint:type-checks`), as #44678 did. Enabling type info in the main config changes what other plugins report, so it stays separate. - `studio-queries` skill documents the pattern. Callers still pass their own grant, so the UI gates on the same grant the request carries. ## Testing - `data/api-keys/api-keys-query.test.tsx`: a granted and a grant-less component share the key, with the grant-less one rendering last; invalidation via the delete mutation refetches successfully. Fails on master with `Missing queryFn`. - `data/granted-query-options.test.ts`: `enabled` gating, factory fields win over caller fields, `select` type inference. - Lint rule cases in `scripts/__tests__/fga-eslint-rules.test.ts`. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
79 lines
2.1 KiB
TypeScript
79 lines
2.1 KiB
TypeScript
import { branchKeys } from './keys'
|
|
import { get, handleError } from '@/data/fetchers'
|
|
import { grantedQueryOptions, type GrantedQueryCallerOptions } from '@/data/granted-query-options'
|
|
import { IS_PLATFORM } from '@/lib/constants'
|
|
import type { StudioError } from '@/lib/errors/studio-errors'
|
|
import type { GrantForVariants } from '@/lib/grants'
|
|
|
|
export type BranchDiffGrant = GrantForVariants<'get', '/v1/branches/{branch_id_or_ref}/diff'>
|
|
|
|
export type BranchDiffVariables = {
|
|
branchRef?: string
|
|
projectRef?: string
|
|
includedSchemas?: string
|
|
pgdelta?: boolean
|
|
grant?: BranchDiffGrant
|
|
}
|
|
|
|
export async function getBranchDiff({
|
|
branchRef,
|
|
includedSchemas,
|
|
pgdelta,
|
|
grant,
|
|
}: {
|
|
branchRef: string
|
|
includedSchemas?: string
|
|
pgdelta?: boolean
|
|
grant: BranchDiffGrant
|
|
}) {
|
|
const query: { included_schemas?: string; pgdelta?: string } = {}
|
|
if (includedSchemas) query.included_schemas = includedSchemas
|
|
if (pgdelta === true) query.pgdelta = 'true'
|
|
|
|
const { data: diffData, error } = await get('/v1/branches/{branch_id_or_ref}/diff', {
|
|
params: {
|
|
path: { branch_id_or_ref: branchRef },
|
|
query: Object.keys(query).length > 0 ? query : undefined,
|
|
},
|
|
headers: {
|
|
Accept: 'text/plain',
|
|
},
|
|
parseAs: 'text',
|
|
grant,
|
|
})
|
|
|
|
if (error) {
|
|
handleError(error)
|
|
}
|
|
|
|
// Handle empty object responses (when no diff exists)
|
|
if (typeof diffData === 'object' && Object.keys(diffData).length === 0) {
|
|
return ''
|
|
}
|
|
|
|
return diffData || ''
|
|
}
|
|
|
|
export type BranchDiffData = Awaited<ReturnType<typeof getBranchDiff>>
|
|
export type BranchDiffError = StudioError
|
|
|
|
export const branchDiffQueryOptions = <TData = BranchDiffData>(
|
|
{ branchRef, projectRef, includedSchemas, pgdelta, grant }: BranchDiffVariables,
|
|
options?: GrantedQueryCallerOptions<
|
|
BranchDiffData,
|
|
BranchDiffError,
|
|
TData,
|
|
ReturnType<typeof branchKeys.diff>
|
|
>
|
|
) =>
|
|
grantedQueryOptions(
|
|
{
|
|
queryKey: branchKeys.diff(projectRef, branchRef, pgdelta),
|
|
queryFn:
|
|
IS_PLATFORM && branchRef && grant
|
|
? () => getBranchDiff({ branchRef, includedSchemas, pgdelta, grant })
|
|
: undefined,
|
|
},
|
|
options
|
|
)
|