Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix: graphiql on self-hosted

Alaister Young8f525fdc0172
diff --git a/studio/pages/api/projects/[ref]/api/graphql.ts b/studio/pages/api/projects/[ref]/api/graphql.ts
new file mode 100644
index 00000000000..73f206a218c
--- /dev/null
+++ b/studio/pages/api/projects/[ref]/api/graphql.ts
@@ -0,0 +1,40 @@
+import { NextApiRequest, NextApiResponse } from 'next'
+import apiWrapper from 'lib/api/apiWrapper'
+
+export default (req: NextApiRequest, res: NextApiResponse) => apiWrapper(req, res, handler)
+
+async function handler(req: NextApiRequest, res: NextApiResponse) {
+ const { method } = req
+
+ switch (method) {
+ case 'POST':
+ return handleGet(req, res)
+
+ default:
+ res.setHeader('Allow', ['POST'])
+ res.status(405).json({ data: null, error: { message: `Method ${method} Not Allowed` } })
+ }
+}
+
+const handleGet = async (req: NextApiRequest, res: NextApiResponse) => {
+ const authorizationHeader = req.headers['x-graphql-authorization']
+
+ const response = await fetch(`${process.env.SUPABASE_URL}/graphql/v1`, {
+ method: 'POST',
+ headers: {
+ apikey: process.env.SUPABASE_SERVICE_KEY!,
+ Authorization:
+ (Array.isArray(authorizationHeader) ? authorizationHeader[0] : authorizationHeader) ??
+ `Bearer ${process.env.SUPABASE_ANON_KEY}`,
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify(req.body),
+ })
+ if (response.ok) {
+ const data = await response.json()
+
+ return res.status(200).json(data)
+ }
+
+ return res.status(500).json({ error: { message: 'Internal Server Error' } })
+}
diff --git a/studio/pages/project/[ref]/api/graphiql.tsx b/studio/pages/project/[ref]/api/graphiql.tsx
index 02843ee891b..5317b8e57eb 100644
--- a/studio/pages/project/[ref]/api/graphiql.tsx
+++ b/studio/pages/project/[ref]/api/graphiql.tsx
@@ -4,7 +4,7 @@ import { createGraphiQLFetcher, Fetcher } from '@graphiql/toolkit'
import { NextPageWithLayout } from 'types'
import { useParams, useStore } from 'hooks'
-import { API_URL } from 'lib/constants'
+import { API_URL, IS_PLATFORM } from 'lib/constants'
import ExtensionCard from 'components/interfaces/Database/Extensions/ExtensionCard'
import GraphiQL from 'components/interfaces/GraphQL/GraphiQL'
import { DocsLayout } from 'components/layouts'
@@ -19,7 +19,7 @@ const GraphiQLPage: NextPageWithLayout = () => {
const isExtensionsLoading = meta.extensions.isLoading
const pgGraphqlExtension = meta.extensions.byId('pg_graphql')
- const { data: accessToken } = useSessionAccessTokenQuery()
+ const { data: accessToken } = useSessionAccessTokenQuery({ enabled: IS_PLATFORM })
const { data: settings, isFetched } = useProjectApiQuery({ projectRef })
const apiService = settings?.autoApiService
@@ -54,17 +54,17 @@ const GraphiQLPage: NextPageWithLayout = () => {
return customFetcher
}, [graphqlUrl, accessToken])
- if (!accessToken || !isFetched || (isExtensionsLoading && !pgGraphqlExtension)) {
+ if ((IS_PLATFORM && !accessToken) || !isFetched || (isExtensionsLoading && !pgGraphqlExtension)) {
return <Connecting />
}
if (pgGraphqlExtension?.installed_version === null) {
return (
- <div className="flex-1 flex flex-col justify-center items-center px-4">
- <div className="max-w-md w-full">
+ <div className="flex flex-col items-center justify-center flex-1 px-4">
+ <div className="w-full max-w-md">
<div className="mb-6">
- <h1 className="text-2xl mt-8 mb-2">Enable the GraphQL Extension</h1>
- <h2 className="text-scale-1100 text-sm">
+ <h1 className="mt-8 mb-2 text-2xl">Enable the GraphQL Extension</h1>
+ <h2 className="text-sm text-scale-1100">
Toggle the switch below to enable the GraphQL extension. You can then use the GraphQL
API with your Supabase Database.
</h2>