Files
Alaister Young f8cc6c21bd [FE-2075] feat(studio): bump graphiql to v5 and use prebuilt component (#45404)
Adds `graphiql@5.2.2` and switches from our heavily-customised rebuild
(which used `@graphiql/react` + `@graphiql/toolkit` directly) to the
prebuilt component, restyled to match the dashboard. Role impersonation
re-added as a sidebar plugin.

This is a deliberately simpler setup than what we had – we lose some
layout customisation (sidebar is forced to the left, role impersonation
moves into the sidebar) but future upgrades become much easier since
we're no longer maintaining a fork-by-rewrite.

**Removed:**
- `apps/studio/components/interfaces/GraphQL/GraphiQL.tsx` – custom
rebuild
- `apps/studio/components/interfaces/GraphQL/graphiql.module.css` –
custom styles

**Changed:**
- Added `graphiql` ^5.2.2 (we previously didn't have the top-level
package, just the subpackages)
- `@graphiql/react` ^0.19.4 → ^0.37.3 (now Monaco-based; v0.19 was still
on CodeMirror 5)
- `@graphiql/toolkit` ^0.9.1 → ^0.11.3
- `GraphiQLTab.tsx` now wires up the prebuilt `<GraphiQL />` with worker
setup, theme bridge, and plugins
- New `graphiql.module.css` scopes restyling via `:global(...)` since we
can't add hashed classes to the library's DOM
- `RoleImpersonationSelector` gained an `orientation: 'horizontal' |
'vertical'` prop (default `horizontal`) so it fits in the sidebar pane –
all existing call sites unchanged
- `MonacoThemeProvider` exports `getTheme` so the GraphQL Monaco
instance can reuse Studio's theme

**Added:**
- Theme bridge: `supabase-graphql-dark` / `supabase-graphql-light`
Monaco themes synced with `next-themes` via `forcedTheme`
- Role impersonation sidebar plugin (gated on `field.jwt_secret` read
permission, same as before)

### Notes / tradeoffs

- We don't share Studio's monaco instance – Studio loads it via AMD/CDN,
GraphiQL bundles it as ESM. Both end up on `monaco-editor@0.52.2` but in
different module systems. Sharing would require ripping out Studio's CDN
loader (Studio-wide refactor, out of scope). GraphiQL's monaco is
dynamically imported and only loads when the GraphQL tab opens.
- The dark/light response panel uses different `--graphiql-response-bg`
tokens because the editor sits at very different baseline lightness in
each theme; a single token can't lift it meaningfully in both
directions.
- Session header (tabs row) is hidden – we don't expose multi-tab
workflows.

## To test

- Open `/project/<ref>/api/graphiql` in both light and dark themes –
editor + response panel backgrounds, sidebar borders, button radii
should all match the dashboard
- Run a query and confirm syntax highlighting works (GraphQL-specific
token `argument.identifier.gql` is purple)
- Open the doc explorer and history sidebar plugins
- As a user with `field.jwt_secret` read permission: open the Role
Impersonation sidebar plugin, pick a role, confirm subsequent queries
hit the API with the impersonated JWT
- As a user without that permission: confirm the Role Impersonation
plugin doesn't appear, history still does
- Toggle theme while GraphiQL is open – Monaco theme should swap without
a reload

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

* **New Features**
* Vertical layout option for the role impersonation selector; radios can
expand to full width.

* **Improvements**
* Revamped GraphiQL integration with updated upstream package, plugins,
and editor theming for improved consistency and UX.
* New GraphiQL styling and layout for clearer pane separation and
polished controls.
* Role selector radios now support a full-width mode for improved
responsiveness.

* **Chores**
  * Updated GraphiQL-related dependencies.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
2026-05-01 16:16:26 +08:00

138 lines
4.7 KiB
TypeScript

import 'graphiql/style.css'
import 'graphiql/setup-workers/webpack'
import { useMonaco, type GraphiQLPlugin } from '@graphiql/react'
import { createGraphiQLFetcher, Fetcher } from '@graphiql/toolkit'
import { PermissionAction } from '@supabase/shared-types/out/constants'
import { useParams } from 'common'
import { GraphiQL, HISTORY_PLUGIN } from 'graphiql'
import { User as IconUser } from 'lucide-react'
import { useTheme } from 'next-themes'
import { useEffect, useMemo } from 'react'
import { toast } from 'sonner'
import { LogoLoader } from 'ui'
import styles from './graphiql.module.css'
import { getTheme } from '@/components/interfaces/App/MonacoThemeProvider'
import { RoleImpersonationSelector } from '@/components/interfaces/RoleImpersonationSelector'
import { useSessionAccessTokenQuery } from '@/data/auth/session-access-token-query'
import { useProjectPostgrestConfigQuery } from '@/data/config/project-postgrest-config-query'
import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions'
import { API_URL, IS_PLATFORM } from '@/lib/constants'
import { getRoleImpersonationJWT } from '@/lib/role-impersonation'
import { useGetImpersonatedRoleState } from '@/state/role-impersonation-state'
const ROLE_IMPERSONATION_PLUGIN: GraphiQLPlugin = {
title: 'Role Impersonation',
icon: () => <IconUser />,
content: () => <RoleImpersonationSelector orientation="vertical" />,
}
const MONACO_THEME = { dark: 'supabase-graphql-dark', light: 'supabase-graphql-light' }
const GraphiQLMonacoTheme = ({ resolvedTheme }: { resolvedTheme: 'dark' | 'light' }) => {
const { monaco } = useMonaco()
useEffect(() => {
if (!monaco) return
const dark = getTheme('dark')
const light = getTheme('light')
monaco.editor.defineTheme(MONACO_THEME.dark, {
...dark,
rules: [...dark.rules, { token: 'argument.identifier.gql', foreground: '908aff' }],
})
monaco.editor.defineTheme(MONACO_THEME.light, {
...light,
rules: [...light.rules, { token: 'argument.identifier.gql', foreground: '6c69ce' }],
// Match the dashboard's bg-default in light mode so the editor doesn't read
// as a darker square against the surrounding UI.
colors: { ...light.colors, 'editor.background': '#fcfcfc' },
})
monaco.editor.setTheme(MONACO_THEME[resolvedTheme])
}, [monaco, resolvedTheme])
return null
}
export const GraphiQLTab = () => {
const { resolvedTheme } = useTheme()
const { ref: projectRef } = useParams()
const currentTheme = resolvedTheme?.includes('dark') ? 'dark' : 'light'
const { data: accessToken } = useSessionAccessTokenQuery({ enabled: IS_PLATFORM })
const { data: config } = useProjectPostgrestConfigQuery({ projectRef })
const jwtSecret = config?.jwt_secret
const getImpersonatedRoleState = useGetImpersonatedRoleState()
const { can: canReadJWTSecret } = useAsyncCheckPermissions(
PermissionAction.READ,
'field.jwt_secret'
)
const plugins = useMemo<GraphiQLPlugin[]>(
() => (canReadJWTSecret ? [HISTORY_PLUGIN, ROLE_IMPERSONATION_PLUGIN] : [HISTORY_PLUGIN]),
[canReadJWTSecret]
)
const fetcher = useMemo(() => {
const fetcherFn = createGraphiQLFetcher({
// [Joshen] Opting to hard code /platform for local to match the routes, so that it's clear what's happening
url: `${API_URL}${IS_PLATFORM ? '' : '/platform'}/projects/${projectRef}/api/graphql`,
fetch,
})
const customFetcher: Fetcher = async (graphqlParams, opts) => {
let userAuthorization: string | undefined
const role = getImpersonatedRoleState().role
if (
projectRef !== undefined &&
jwtSecret !== undefined &&
role !== undefined &&
role.type === 'postgrest'
) {
try {
const token = await getRoleImpersonationJWT(projectRef, jwtSecret, role)
userAuthorization = 'Bearer ' + token
} catch (err: any) {
toast.error(`Failed to get JWT for role: ${err.message}`)
}
}
return fetcherFn(graphqlParams, {
...opts,
headers: {
...opts?.headers,
...(accessToken && {
Authorization: `Bearer ${accessToken}`,
}),
'x-graphql-authorization':
opts?.headers?.['Authorization'] ??
opts?.headers?.['authorization'] ??
userAuthorization ??
accessToken,
},
})
}
return customFetcher
}, [projectRef, getImpersonatedRoleState, jwtSecret, accessToken])
if (IS_PLATFORM && !accessToken) {
return <LogoLoader />
}
return (
<>
<GraphiQLMonacoTheme resolvedTheme={currentTheme} />
<GraphiQL
fetcher={fetcher}
forcedTheme={currentTheme}
editorTheme={MONACO_THEME}
className={styles.root}
plugins={plugins}
/>
</>
)
}