Files
supabase/apps/studio/components/interfaces/ProjectAPIDocs/ProjectAPIDocs.tsx
Charis 597d1ce56e fix(studio): pass caller options to grant-gated query factories (#53088)
## 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)
2026-11-06 11:04:00 -05:00

163 lines
5.8 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { useParams } from 'common'
import { useState } from 'react'
import { SidePanel } from 'ui'
import { Bucket } from './Content/Bucket'
import { EdgeFunction } from './Content/EdgeFunction'
import { EdgeFunctions } from './Content/EdgeFunctions'
import { Entities } from './Content/Entities'
import { Entity } from './Content/Entity'
import { Introduction } from './Content/Introduction'
import { Realtime } from './Content/Realtime'
import { RPC } from './Content/RPC'
import { Storage } from './Content/Storage'
import { StoredProcedures } from './Content/StoredProcedures'
import { UserManagement } from './Content/UserManagement'
import { FirstLevelNav } from './FirstLevelNav'
import LanguageSelector from './LanguageSelector'
import { SecondLevelNav } from './SecondLevelNav'
import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
import { apiKeysQueryOptions, getKeys } from '@/data/api-keys/api-keys-query'
import { useProjectApiUrl } from '@/data/config/project-endpoint-query'
import { useGrant } from '@/lib/grants'
import { useAppStateSnapshot } from '@/state/app-state'
const getShowKeysTooltip = (status: 'loading' | 'granted' | 'denied' | 'error') => {
if (status === 'denied') return 'You need additional permissions to view API keys'
if (status === 'error') return 'Failed to check API key permissions'
return undefined
}
/**
* [Joshen] Reminder: when we choose to release this as a main feature
* Ensure that UX is better than the existing, and make sure we do the
* necessary communications around releasing this.
*
* Problems:
* - Needs URL support
* - Language selector is not clear, users are missing the bash language option
* - GraphiQL needs a better home, cannot be placed under Database as its "API"
*/
export const ProjectAPIDocs = () => {
const { ref } = useParams()
const snap = useAppStateSnapshot()
const isIntroduction =
snap.activeDocsSection.length === 1 && snap.activeDocsSection[0] === 'introduction'
const isEntityDocs =
snap.activeDocsSection.length === 2 && snap.activeDocsSection[0] === 'entities'
const [showKeys, setShowKeys] = useState(false)
const language = snap.docsLanguage
const apiKeysGrant = useGrant('GET /v1/projects/{ref}/api-keys', { projectRef: ref })
const { data: apiKeysData } = useQuery(
apiKeysQueryOptions(
{ projectRef: ref, grant: apiKeysGrant.grant },
{ enabled: snap.showProjectApiDocs, select: getKeys }
)
)
const canShowKeys = apiKeysGrant.status === 'granted'
const showKeysTooltip = getShowKeysTooltip(apiKeysGrant.status)
const { anonKey } = apiKeysData ?? {}
const { data: endpoint } = useProjectApiUrl(
{ projectRef: ref },
{ enabled: snap.showProjectApiDocs }
)
const apikey = showKeys
? (anonKey?.api_key ?? 'SUPABASE_CLIENT_ANON_KEY')
: 'SUPABASE_CLIENT_ANON_KEY'
return (
<SidePanel
hideFooter
size="xxlarge"
className="max-w-5xl"
visible={snap.showProjectApiDocs}
onCancel={() => snap.setShowProjectApiDocs(false)}
>
<div className="flex items-start h-full">
<div className="w-72 border-r h-full">
<div className="border-b px-4 py-2 flex items-center justify-between">
<h4>API Docs</h4>
<div className="flex items-center space-x-1">
{!isEntityDocs && <LanguageSelector simplifiedVersion />}
{isIntroduction && (
<ButtonTooltip
disabled={!canShowKeys}
onClick={() => setShowKeys(!showKeys)}
tooltip={{ content: { side: 'bottom', text: showKeysTooltip } }}
>
{showKeys ? 'Hide keys' : 'Show keys'}
</ButtonTooltip>
)}
</div>
</div>
{snap.activeDocsSection.length === 1 ? <FirstLevelNav /> : <SecondLevelNav />}
</div>
<div className="flex-1 divide-y space-y-4 max-h-screen overflow-auto">
{snap.activeDocsSection[0] === 'introduction' && (
<Introduction
showKeys={showKeys}
language={language}
apikey={apikey}
endpoint={endpoint}
/>
)}
{snap.activeDocsSection[0] === 'user-management' && (
<UserManagement language={language} apikey={apikey} endpoint={endpoint} />
)}
{snap.activeDocsSection[0] === 'realtime' && <Realtime language={language} />}
{snap.activeDocsSection[0] === 'storage' && (
<>
{snap.activeDocsSection[1] !== undefined ? (
<Bucket language={language} apikey={apikey} endpoint={endpoint} />
) : (
<Storage language={language} />
)}
</>
)}
{snap.activeDocsSection[0] === 'edge-functions' && (
<>
{snap.activeDocsSection[1] !== undefined ? (
<EdgeFunction language={language} apikey={apikey} endpoint={endpoint} />
) : (
<EdgeFunctions language={language} />
)}
</>
)}
{snap.activeDocsSection[0] === 'entities' && (
<>
{snap.activeDocsSection[1] !== undefined ? (
<Entity language={language} apikey={apikey} endpoint={endpoint} />
) : (
<Entities language={language} />
)}
</>
)}
{snap.activeDocsSection[0] === 'stored-procedures' && (
<>
{snap.activeDocsSection[1] !== undefined ? (
<RPC language={language} />
) : (
<StoredProcedures language={language} />
)}
</>
)}
</div>
</div>
</SidePanel>
)
}