download-graphql-schema.mts45 lines · main
1import { stripIndent } from 'common-tags'
2import { writeFileSync } from 'node:fs'
3import path from 'node:path'
4import { fileURLToPath } from 'node:url'
5
6const __dirname = path.dirname(fileURLToPath(import.meta.url))
7
8// Note: This is a build-time script, so we use the fallback URL directly
9const DOCS_URL = process.env.NEXT_PUBLIC_DOCS_URL || 'https://supabase.com/docs'
10
11async function downloadGraphQLSchema() {
12 const schemaEndpoint = `${DOCS_URL}/api/graphql`
13 const outputPath = path.join(__dirname, './schema.graphql')
14
15 const schemaQuery = stripIndent`
16 query SchemaQuery {
17 schema
18 }
19 `
20
21 try {
22 const response = await fetch(schemaEndpoint, {
23 method: 'POST',
24 body: JSON.stringify({
25 query: schemaQuery.trim(),
26 }),
27 })
28 const { data, errors } = await response.json()
29
30 if (errors) {
31 throw errors
32 }
33
34 writeFileSync(outputPath, data.schema, 'utf8')
35
36 console.log(`✅ Successfully downloaded GraphQL schema to ${outputPath}`)
37 } catch (error) {
38 console.error('🚨 Error generating GraphQL schema:', error)
39 process.exit(1)
40 }
41}
42
43if (process.argv[1] === fileURLToPath(import.meta.url)) {
44 downloadGraphQLSchema()
45}