Files
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

141 lines
5.0 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { useParams } from 'common'
import { CopyIcon } from 'lucide-react'
import { useEffect, useState } from 'react'
import { Button, copyToClipboard } from 'ui'
import { Input } from 'ui-patterns/DataInputs/Input'
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
import ContentSnippet from '../ContentSnippet'
import { DOCS_CONTENT } from '../ProjectAPIDocs.constants'
import type { ContentProps } from './Content.types'
import { apiKeysQueryOptions, getKeys } from '@/data/api-keys/api-keys-query'
import { useProjectSettingsV2Query } from '@/data/config/project-settings-v2-query'
import { useGrant } from '@/lib/grants'
import { useTrack } from '@/lib/telemetry/track'
export const Introduction = ({ showKeys, language, apikey, endpoint }: ContentProps) => {
const { ref } = useParams()
const apiKeysGrant = useGrant('GET /v1/projects/{ref}/api-keys', { projectRef: ref })
const { data: apiKeysData } = useQuery(
apiKeysQueryOptions({ projectRef: ref, grant: apiKeysGrant.grant }, { select: getKeys })
)
useProjectSettingsV2Query({ projectRef: ref })
const { anonKey, serviceKey } = apiKeysData ?? {}
const track = useTrack()
const [copied, setCopied] = useState<'anon' | 'service'>()
useEffect(() => {
if (copied !== undefined) setTimeout(() => setCopied(undefined), 2000)
}, [copied])
const anonApiKey = anonKey?.api_key
const serviceApiKey = serviceKey?.api_key ?? 'SUPABASE_CLIENT_SERVICE_KEY'
return (
<>
<ContentSnippet
selectedLanguage={language}
apikey={apikey}
endpoint={endpoint}
snippet={DOCS_CONTENT.init}
>
<div className="px-4 space-y-6">
<div className="flex flex-col space-x-4 mt-8">
<FormItemLayout isReactForm={false} layout="horizontal" label="Project URL">
<Input disabled readOnly copy size="small" value={endpoint} className="w-full" />
</FormItemLayout>
</div>
<div className="flex flex-col space-x-4">
<FormItemLayout
isReactForm={false}
layout="horizontal"
label="Client API key"
description="This key is safe to use in a browser if you have enabled Row Level Security (RLS) for your tables and configured policies."
>
<Input
disabled
readOnly
size="small"
value={showKeys ? apikey : 'Reveal API keys via dropdown in the header'}
actions={[
<Button
key="copy"
icon={<CopyIcon />}
onClick={() => {
setCopied('anon')
copyToClipboard(anonApiKey ?? 'SUPABASE_CLIENT_ANON_KEY')
track('api_docs_code_copy_button_clicked', {
title: 'Client API key',
selectedLanguage: language,
})
}}
>
{copied === 'anon' ? 'Copied' : 'Copy'}
</Button>,
]}
/>
</FormItemLayout>
</div>
<div className="flex flex-col space-x-4">
<FormItemLayout
isReactForm={false}
layout="horizontal"
label="Service key"
description={
<p>
This key has the ability to bypass Row Level Security.{' '}
<span className="text-amber-900">Never share it publicly.</span>
</p>
}
>
<Input
disabled
readOnly
size="small"
value={
showKeys
? (serviceApiKey ?? 'SUPABASE_CLIENT_SERVICE_KEY')
: 'Reveal API keys via dropdown in the header'
}
actions={[
<Button
key="copy"
icon={<CopyIcon />}
onClick={() => {
setCopied('service')
copyToClipboard(serviceApiKey)
track('api_docs_code_copy_button_clicked', {
title: 'Service key',
selectedLanguage: language,
})
}}
>
{copied === 'service' ? 'Copied' : 'Copy'}
</Button>,
]}
/>
</FormItemLayout>
</div>
</div>
</ContentSnippet>
<ContentSnippet
selectedLanguage={language}
apikey={apikey}
endpoint={endpoint}
snippet={DOCS_CONTENT.clientApiKeys}
/>
<ContentSnippet
selectedLanguage={language}
apikey={showKeys ? serviceApiKey : 'SUPABASE_CLIENT_SERVICE_KEY'}
endpoint={endpoint}
snippet={DOCS_CONTENT.serviceApiKeys}
/>
</>
)
}