The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
feat(content api): add dummy api/graphql route (#35141)
Charisa0f5c798dfee
diff --git a/apps/docs/app/api/graphql/route.test.ts b/apps/docs/app/api/graphql/route.test.tsnew file mode 100644index 00000000000..77d12a09ac4--- /dev/null+++ b/apps/docs/app/api/graphql/route.test.ts@@ -0,0 +1,56 @@+import { beforeEach, describe, expect, it, vi } from 'vitest'+import { POST } from './route'++describe('GraphQL API endpoint', () => {+ beforeEach(() => {+ vi.clearAllMocks()+ vi.spyOn(console, 'error').mockImplementation(() => {})+ })++ it('should return 400 if request body is not valid JSON', async () => {+ const request = new Request('http://localhost/api/graphql', {+ method: 'POST',+ body: 'not json',+ })++ const response = await POST(request)+ expect(response.status).toBe(400)+ expect(await response.text()).toBe('Invalid request: Request body must be valid JSON')+ })++ it('should return 400 if request body is missing required fields', async () => {+ const request = new Request('http://localhost/api/graphql', {+ method: 'POST',+ body: JSON.stringify({ variables: {} }),+ })++ const response = await POST(request)+ expect(response.status).toBe(400)+ expect(await response.text()).toContain('Request body must be a valid GraphQL request object')+ })++ it('should return 400 if query is not a string', async () => {+ const request = new Request('http://localhost/api/graphql', {+ method: 'POST',+ body: JSON.stringify({ query: 123 }),+ })++ const response = await POST(request)+ expect(response.status).toBe(400)+ })++ it('should return 500 for internal server errors', async () => {+ const request = new Request('http://localhost/api/graphql', {+ method: 'POST',+ body: JSON.stringify({ query: 'query { hello }' }),+ })++ vi.spyOn(request, 'json').mockImplementation(() => {+ throw new Error('Unexpected error')+ })++ const response = await POST(request)+ expect(response.status).toBe(500)+ expect(await response.text()).toBe('Internal Server Error')+ })+})diff --git a/apps/docs/app/api/graphql/route.ts b/apps/docs/app/api/graphql/route.tsnew file mode 100644index 00000000000..7c5e8c9fdde--- /dev/null+++ b/apps/docs/app/api/graphql/route.ts@@ -0,0 +1,49 @@+// import { graphql } from 'graphql'+import { NextResponse } from 'next/server'+import { z } from 'zod'++import { ApiError, InvalidRequestError } from '~/app/api/utils'++export const runtime = 'edge'++const graphQLRequestSchema = z.object({+ query: z.string(),+ variables: z.record(z.any()).optional(),+ operationName: z.string().optional(),+})++async function handleGraphQLRequest(request: Request): Promise<NextResponse> {+ const body = await request.json().catch(() => {+ throw new InvalidRequestError('Request body must be valid JSON')+ })+ const parsedBody = graphQLRequestSchema.safeParse(body)+ if (!parsedBody.success) {+ const errorMessage = parsedBody.error?.message+ throw new InvalidRequestError(+ `Request body must be a valid GraphQL request object${errorMessage ? `: ${errorMessage}` : ''}`+ )+ }++ // const { query, variables, operationName } = parsedBody.data+ // When endpoint is complete, this should return a response with the GraphQL+ // query result++ // For now, just return a 404 response+ return new NextResponse('Not Found', { status: 404 })+}++export async function POST(request: Request): Promise<NextResponse> {+ try {+ return await handleGraphQLRequest(request)+ } catch (error: unknown) {+ console.error(error)++ if (error instanceof ApiError) {+ return new NextResponse(error.isPrivate() ? 'Internal Server Error' : error.message, {+ status: error.statusCode(),+ })+ } else {+ return new NextResponse('Internal Server Error', { status: 500 })+ }+ }+}diff --git a/apps/docs/app/api/utils.ts b/apps/docs/app/api/utils.tsnew file mode 100644index 00000000000..5b3f93ea15b--- /dev/null+++ b/apps/docs/app/api/utils.ts@@ -0,0 +1,29 @@+import { z } from 'zod'++export class ApiError extends Error {+ constructor(message: string) {+ super(message)+ }++ isPrivate() {+ return true+ }++ statusCode() {+ return 500+ }+}++export class InvalidRequestError extends ApiError {+ constructor(message: string) {+ super(`Invalid request: ${message}`)+ }++ isPrivate() {+ return false+ }++ statusCode() {+ return 400+ }+}diff --git a/apps/docs/package.json b/apps/docs/package.jsonindex 4208f151170..dd046d93891 100644--- a/apps/docs/package.json+++ b/apps/docs/package.json@@ -55,6 +55,7 @@"eslint-config-supabase": "workspace:*","framer-motion": "^11.0.3","github-slugger": "^2.0.0",+ "graphql": "^16.10.0","gray-matter": "^4.0.3","hast-util-has-property": "^2.0.1","hast-util-to-html": "^9.0.0",diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex ae1312c5e06..b21aa326339 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -282,6 +282,9 @@ importers:github-slugger:specifier: ^2.0.0version: 2.0.0+ graphql:+ specifier: ^16.10.0+ version: 16.10.0gray-matter:specifier: ^4.0.3version: 4.0.3@@ -564,10 +567,10 @@ importers:version: 0.2.15'@graphiql/react':specifier: ^0.19.4- version: 0.19.4(@codemirror/[email protected])(@types/[email protected])(@types/[email protected])(@types/[email protected])([email protected]([email protected]))([email protected])([email protected]([email protected]))([email protected])+ version: 0.19.4(@codemirror/[email protected])(@types/[email protected])(@types/[email protected])(@types/[email protected])([email protected]([email protected]))([email protected])([email protected]([email protected]))([email protected])'@graphiql/toolkit':specifier: ^0.9.1'@gregnr/postgres-meta':specifier: ^0.82.0-dev.2version: 0.82.0-dev.2([email protected])([email protected])@@ -979,7 +982,7 @@ importers:version: 2.0.7([email protected]([email protected]))graphql-ws:specifier: 5.14.1- version: 5.14.1([email protected])+ version: 5.14.1([email protected])import-in-the-middle:specifier: ^1.13.1version: 1.13.1@@ -8675,6 +8678,7 @@ packages:resolution: {integrity: sha512-t0q23FIpvHDTtnORW+bDJziGsal5uh9RJTJ1fyH8drd4lICOoXhJ5pLMUZ5C0VQei6dNmwTzzoTRgMkO9JgHEQ==}peerDependencies:eslint: '>= 5'+ bundledDependencies: []resolution: {integrity: sha512-BbPC0cuExzhiMo4Ff1BTVwHpjjv28C5R+btTOGaCRC7UEz801up0JadwkeSk5Ued6TG34uaczuVuH6qyy5YUxw==}@@ -9311,6 +9315,10 @@ packages:peerDependencies:graphql: '>=0.11 <=16'+ resolution: {integrity: sha512-AjqGKbDGUFRKIRCP9tCKiIGHyriz2oHEbPIbEtcSLSs4YjReZOIPQQWek4+6hjw62H9QShXHyaGivGiYVLeYFQ==}+ engines: {node: ^12.22.0 || ^14.16.0 || ^16.0.0 || >=17.0.0}+resolution: {integrity: sha512-59LZHPdGZVh695Ud9lRzPBVTtlX9ZCV150Er2W43ro37wVof0ctenSaskPPjN7lVTIN8mSZt8PHUNKZuNQUuxw==}engines: {node: ^12.22.0 || ^14.16.0 || ^16.0.0 || >=17.0.0}@@ -16075,9 +16083,9 @@ snapshots:'@gar/[email protected]': {}- '@graphiql/[email protected](@codemirror/[email protected])(@types/[email protected])(@types/[email protected])(@types/[email protected])([email protected]([email protected]))([email protected])([email protected]([email protected]))([email protected])':+ '@graphiql/[email protected](@codemirror/[email protected])(@types/[email protected])(@types/[email protected])(@types/[email protected])([email protected]([email protected]))([email protected])([email protected]([email protected]))([email protected])':dependencies:- '@graphiql/toolkit': 0.9.1(@types/[email protected])([email protected]([email protected]))([email protected])+ '@graphiql/toolkit': 0.9.1(@types/[email protected])([email protected]([email protected]))([email protected])'@radix-ui/react-dialog': 1.0.5(@types/[email protected])(@types/[email protected])([email protected]([email protected]))([email protected])'@radix-ui/react-dropdown-menu': 2.0.6(@types/[email protected])(@types/[email protected])([email protected]([email protected]))([email protected])@@ -16086,11 +16094,11 @@ snapshots:'@types/codemirror': 5.60.10clsx: 1.2.1codemirror: 5.65.15copy-to-clipboard: 3.3.3- graphql: 16.8.1- graphql-language-service: 5.2.0([email protected])+ graphql: 16.10.0+ graphql-language-service: 5.2.0([email protected])markdown-it: 12.3.2react: 18.3.1react-dom: 18.3.1([email protected])@@ -16102,13 +16110,13 @@ snapshots:- '@types/react-dom'- graphql-ws- '@graphiql/[email protected](@types/[email protected])([email protected]([email protected]))([email protected])':+ '@graphiql/[email protected](@types/[email protected])([email protected]([email protected]))([email protected])':dependencies:'@n1ru4l/push-pull-async-iterable-iterator': 3.2.0- graphql: 16.8.1+ graphql: 16.10.0meros: 1.3.0(@types/[email protected])optionalDependencies:- graphql-ws: 5.14.1([email protected])+ graphql-ws: 5.14.1([email protected])transitivePeerDependencies:- '@types/node'@@ -21894,13 +21902,13 @@ snapshots:estree-walker: 3.0.3periscopic: 3.1.0dependencies:'@codemirror/language': 6.11.0'@types/codemirror': 0.0.90codemirror: 5.65.15- graphql: 16.8.1- graphql-language-service: 5.2.0([email protected])+ graphql: 16.10.0+ graphql-language-service: 5.2.0([email protected])@@ -23719,15 +23727,17 @@ snapshots:dependencies:- graphql: 16.8.1+ graphql: 16.10.0nullthrows: 1.1.1vscode-languageserver-types: 3.17.5dependencies:- graphql: 16.8.1+ graphql: 16.10.0++ [email protected]: {}