download-graphql-schema.mts 1.2 KB

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