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

71 lines
2.5 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { useParams } from 'common'
import { UseFormReturn } from 'react-hook-form'
import { useWatch } from 'ui'
import { KeyValueFieldArray } from 'ui-patterns/form/KeyValueFieldArray/KeyValueFieldArray'
import { WebhookFormValues } from './EditHookPanel.constants'
import { buildEdgeFunctionHeaderAddActions } from '@/components/interfaces/Functions/httpHeaderAddActions'
import { ApiKeysAccessNote } from '@/components/ui/ApiKeysAccessNote'
import {
FormSection,
FormSectionContent,
FormSectionLabel,
} from '@/components/ui/Forms/FormSection'
import { getKeys, revealedApiKeysQueryOptions } from '@/data/api-keys/api-keys-query'
import { useGrant } from '@/lib/grants'
import { uuidv4 } from '@/lib/helpers'
interface HTTPHeadersProps {
form: UseFormReturn<WebhookFormValues>
}
export const HTTPHeaders = ({ form }: HTTPHeadersProps) => {
const { ref } = useParams()
const apiKeysGrant = useGrant(
'GET /v1/projects/{ref}/api-keys',
{ projectRef: ref },
{ variant: 'reveal' }
)
const { data: apiKeyData } = useQuery(
revealedApiKeysQueryOptions({ projectRef: ref, grant: apiKeysGrant.grant }, { select: getKeys })
)
const { serviceKey, secretKey } = apiKeyData ?? {}
const apiKey = secretKey?.api_key ?? serviceKey?.api_key ?? '[YOUR API KEY]'
const functionType = useWatch({ control: form.control, name: 'function_type' })
const addActions =
functionType === 'supabase_function'
? buildEdgeFunctionHeaderAddActions({
apiKey,
createRow: (name: string, value: string) => ({ id: uuidv4(), name, value }),
})
: []
return (
<FormSection
header={<FormSectionLabel className="lg:col-span-4!">HTTP Headers</FormSectionLabel>}
>
<FormSectionContent loading={false} className="lg:col-span-8!">
<KeyValueFieldArray
control={form.control}
name="httpHeaders"
keyFieldName="name"
valueFieldName="value"
createEmptyRow={() => ({ id: uuidv4(), name: '', value: '' })}
keyPlaceholder="Header name"
valuePlaceholder="Header value"
addLabel="Add header"
addActions={addActions}
/>
{functionType === 'supabase_function' && (
<ApiKeysAccessNote
grant={apiKeysGrant}
fallback="The Authorization header uses a placeholder you can replace."
/>
)}
</FormSectionContent>
</FormSection>
)
}