Files
supabase/apps/studio/components/interfaces/Integrations/GraphQL/IntrospectionConfirmModal.tsx
Charis 72cebe3976 feat(studio): toggle pg_graphql introspection from GraphiQL (#46170)
## Summary

- pg_graphql 1.6+ disables schema introspection by default, which breaks
GraphiQL's docs explorer and field autocomplete. This PR adds an in-app
notice + confirmation flow so users can opt into (or later opt out of)
introspection without leaving the GraphQL tab.
- Introspection state is read from, and written to, the `@graphql(...)`
directive embedded in the target schema's Postgres comment (`public` by
default). Other directive options the user has set are preserved when
the introspection key is toggled.
- Ships `parseSchemaComment` / `buildSchemaCommentWith` helpers (with
unit tests) and a `useSetIntrospection` mutation hook, plus collapsible
disabled-state and dismissible enabled-state notices rendered above
GraphiQL. GraphiQL is re-mounted after a toggle so it re-runs
introspection.

## Test plan

- [ ] On a project with pg_graphql >= 1.6 and introspection disabled:
disabled-state notice appears, confirm modal shows the SQL that will
run, enabling re-mounts GraphiQL and populates the docs explorer.
- [ ] On a project with introspection enabled: small enabled-state
banner appears, disabling clears the docs explorer and updates the
schema comment.
- [ ] Existing `@graphql({...})` options (e.g. `inflect_names`,
`max_rows`) survive a toggle; malformed directive text is replaced and a
warning is shown in the confirm modal.
- [ ] On pg_graphql < 1.6 (or extension not installed): no notice
renders, GraphiQL behaves as before.
- [ ] Collapsed-disabled-notice state persists per project via local
storage.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
  * GraphQL introspection toggle with enable/disable confirmation modal.
* Notices showing current introspection state with controls to change
it.
* GraphiQL automatically remounts and updates when introspection status
changes.
* Per-project persisted collapsed/expanded state for the introspection
notice.
* Background detection of introspection support and schema comment
handling for targeted schemas.

* **Tests**
* Comprehensive tests for parsing/building schema comment directives and
version behavior.

<!-- review_stack_entry_start -->

[![Review Change
Stack](https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg)](https://app.coderabbit.ai/change-stack/supabase/supabase/pull/46170?utm_source=github_walkthrough&utm_medium=github&utm_campaign=change_stack)

<!-- review_stack_entry_end -->
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-05-21 07:07:35 -04:00

120 lines
3.4 KiB
TypeScript

import { type SafeSqlFragment } from '@supabase/pg-meta'
import type { ReactNode } from 'react'
import { CodeBlock } from 'ui-patterns/CodeBlock'
import ConfirmationModal from 'ui-patterns/Dialogs/ConfirmationModal'
type IntrospectionMode = 'enable' | 'disable'
interface ModeCopy {
title: string
confirmLabel: string
confirmLabelLoading: string
persistenceBullet: (schema: string) => ReactNode
securityBullet: ReactNode
}
const COPY: Record<IntrospectionMode, ModeCopy> = {
enable: {
title: 'Enable GraphQL introspection?',
confirmLabel: 'Enable introspection',
confirmLabelLoading: 'Enabling...',
persistenceBullet: (_schema) => <>This setting persists until explicitly disabled.</>,
securityBullet: (
<>
External actors will be able to introspect your schema using the <code>anon</code> key.
</>
),
},
disable: {
title: 'Disable GraphQL introspection?',
confirmLabel: 'Disable introspection',
confirmLabelLoading: 'Disabling...',
persistenceBullet: (_schema) => <>This setting persists until explicitly re-enabled.</>,
securityBullet: (
<>
External actors will no longer be able to introspect your schema via the <code>anon</code>{' '}
key. GraphiQL's docs explorer and autocomplete will stop working until introspection is
re-enabled.
</>
),
},
}
interface IntrospectionConfirmModalProps {
mode: IntrospectionMode
visible: boolean
schema: string
sql: SafeSqlFragment
otherExistingKeys: string[]
existingDirectiveIsMalformed: boolean
isPending: boolean
onCancel: () => void
onConfirm: () => void
}
export const IntrospectionConfirmModal = ({
mode,
visible,
schema,
sql,
otherExistingKeys,
existingDirectiveIsMalformed,
isPending,
onCancel,
onConfirm,
}: IntrospectionConfirmModalProps) => {
const copy = COPY[mode]
const hasPreservedOptions = otherExistingKeys.length > 0
return (
<ConfirmationModal
visible={visible}
size="large"
title={copy.title}
confirmLabel={copy.confirmLabel}
confirmLabelLoading={copy.confirmLabelLoading}
cancelLabel="Cancel"
loading={isPending}
onCancel={onCancel}
onConfirm={onConfirm}
>
<div className="space-y-4 text-sm">
<ul className="list-disc space-y-1 pl-5 text-foreground-light">
<li>{copy.persistenceBullet(schema)}</li>
<li>{copy.securityBullet}</li>
{hasPreservedOptions && (
<li>
Existing <code>@graphql(...)</code> options on this schema will be preserved:{' '}
<PreservedOptionKeys keys={otherExistingKeys} />.
</li>
)}
{existingDirectiveIsMalformed && (
<li className="text-warning">
The existing <code>@graphql(...)</code> directive on this schema could not be parsed
and will be replaced by the statement below.
</li>
)}
</ul>
<div>
<p className="text-foreground-light mb-2">The following statement will be executed:</p>
<CodeBlock language="sql" className="text-xs" hideLineNumbers>
{sql}
</CodeBlock>
</div>
</div>
</ConfirmationModal>
)
}
const PreservedOptionKeys = ({ keys }: { keys: string[] }) => (
<>
{keys.map((k, i) => (
<span key={k}>
{i > 0 && ', '}
<code>{k}</code>
</span>
))}
</>
)