download-graphql-schema.mts45 lines · main
| 1 | import { stripIndent } from 'common-tags' |
| 2 | import { writeFileSync } from 'node:fs' |
| 3 | import path from 'node:path' |
| 4 | import { fileURLToPath } from 'node:url' |
| 5 | |
| 6 | const __dirname = path.dirname(fileURLToPath(import.meta.url)) |
| 7 | |
| 8 | // Note: This is a build-time script, so we use the fallback URL directly |
| 9 | const DOCS_URL = process.env.NEXT_PUBLIC_DOCS_URL || 'https://supabase.com/docs' |
| 10 | |
| 11 | async 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 | |
| 43 | if (process.argv[1] === fileURLToPath(import.meta.url)) { |
| 44 | downloadGraphQLSchema() |
| 45 | } |