|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + |
| 9 | + "github.com/roneli/fastgql/pkg/importer" |
| 10 | + "github.com/roneli/fastgql/pkg/importer/postgres" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + importConnStr string |
| 16 | + importSchemaName string |
| 17 | + importTables []string |
| 18 | + importOutputFile string |
| 19 | + importGenerateQueries bool |
| 20 | + importGenerateFilters bool |
| 21 | +) |
| 22 | + |
| 23 | +var importCmd = &cobra.Command{ |
| 24 | + Use: "import", |
| 25 | + Short: "import GraphQL schema from a PostgreSQL database", |
| 26 | + Long: `Import and generate a GraphQL schema from an existing PostgreSQL database by introspecting the database structure`, |
| 27 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 28 | + // Check for DATABASE_URL environment variable if conn string not provided |
| 29 | + if importConnStr == "" { |
| 30 | + if dbURL := os.Getenv("DATABASE_URL"); dbURL != "" { |
| 31 | + importConnStr = dbURL |
| 32 | + } else { |
| 33 | + return fmt.Errorf("connection string is required (use --conn or set DATABASE_URL)") |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + ctx := context.Background() |
| 38 | + |
| 39 | + // Create PostgreSQL source |
| 40 | + source := postgres.NewSource(nil) |
| 41 | + if err := source.Connect(ctx, importConnStr); err != nil { |
| 42 | + return fmt.Errorf("failed to connect to database: %w", err) |
| 43 | + } |
| 44 | + defer source.Close() |
| 45 | + |
| 46 | + // Build options |
| 47 | + options := importer.IntrospectOptions{ |
| 48 | + SchemaName: importSchemaName, |
| 49 | + Tables: importTables, |
| 50 | + GenerateQueries: importGenerateQueries, |
| 51 | + GenerateFilters: importGenerateFilters, |
| 52 | + } |
| 53 | + |
| 54 | + // Introspect database |
| 55 | + schema, err := source.Introspect(ctx, options) |
| 56 | + if err != nil { |
| 57 | + return fmt.Errorf("failed to introspect database: %w", err) |
| 58 | + } |
| 59 | + |
| 60 | + // Generate GraphQL schema |
| 61 | + astSource, err := importer.GenerateSchema(schema) |
| 62 | + if err != nil { |
| 63 | + return fmt.Errorf("failed to generate schema: %w", err) |
| 64 | + } |
| 65 | + |
| 66 | + // Determine output file |
| 67 | + outputFile := importOutputFile |
| 68 | + if outputFile == "" { |
| 69 | + outputFile = "graph/schema.graphql" |
| 70 | + } |
| 71 | + |
| 72 | + // Create directory if needed |
| 73 | + if err := os.MkdirAll(filepath.Dir(outputFile), 0755); err != nil { |
| 74 | + return fmt.Errorf("failed to create output directory: %w", err) |
| 75 | + } |
| 76 | + |
| 77 | + // Write schema to file |
| 78 | + if err := os.WriteFile(outputFile, []byte(astSource.Input), 0644); err != nil { |
| 79 | + return fmt.Errorf("failed to write schema file: %w", err) |
| 80 | + } |
| 81 | + |
| 82 | + fmt.Printf("Successfully imported schema from PostgreSQL database\n") |
| 83 | + fmt.Printf("Schema written to: %s\n", outputFile) |
| 84 | + fmt.Printf("Found %d object types\n", len(schema.ObjectTypes)) |
| 85 | + if len(schema.QueryFields) > 0 { |
| 86 | + fmt.Printf("Generated %d query fields\n", len(schema.QueryFields)) |
| 87 | + } |
| 88 | + |
| 89 | + return nil |
| 90 | + }, |
| 91 | +} |
| 92 | + |
| 93 | +func init() { |
| 94 | + importCmd.Flags().StringVarP(&importConnStr, "conn", "d", "", "PostgreSQL connection string (or set DATABASE_URL environment variable)") |
| 95 | + importCmd.Flags().StringVarP(&importSchemaName, "schema", "s", "public", "Database schema name to introspect") |
| 96 | + importCmd.Flags().StringSliceVarP(&importTables, "tables", "t", []string{}, "Comma-separated list of table names to import (empty = all tables)") |
| 97 | + importCmd.Flags().StringVarP(&importOutputFile, "output", "o", "", "Output file path for generated schema (default: graph/schema.graphql)") |
| 98 | + importCmd.Flags().BoolVarP(&importGenerateQueries, "queries", "q", false, "Generate query fields for all tables") |
| 99 | + importCmd.Flags().BoolVarP(&importGenerateFilters, "filters", "f", false, "Generate filter inputs for all types") |
| 100 | +} |
0 commit comments