11import { flags } from "@oclif/command" ;
2- import { introspectionFromSchema } from "graphql" ;
2+ import { introspectionFromSchema , printSchema } from "graphql" ;
33import { writeFileSync } from "fs" ;
44
55import { ClientCommand } from "../../Command" ;
66
77export default class SchemaDownload extends ClientCommand {
8- static description = "Download a schema from engine or a GraphQL endpoint." ;
8+ static description =
9+ "Download a schema from engine or a GraphQL endpoint in JSON or SDL format" ;
910
1011 static flags = {
1112 ...ClientCommand . flags
@@ -14,7 +15,8 @@ export default class SchemaDownload extends ClientCommand {
1415 static args = [
1516 {
1617 name : "output" ,
17- description : "Path to write the introspection result to" ,
18+ description :
19+ "Path to write the introspection result to. Can be `.graphql`, `.gql`, `.graphqls`, or `.json`" ,
1820 required : true ,
1921 default : "schema.json"
2022 }
@@ -23,17 +25,21 @@ export default class SchemaDownload extends ClientCommand {
2325 async run ( ) {
2426 let result ;
2527 let gitContext ;
26- await this . runTasks ( ( { args, project, flags } ) => [
27- {
28- title : `Saving schema to ${ args . output } ` ,
29- task : async ( ) => {
30- const schema = await project . resolveSchema ( { tag : flags . tag } ) ;
31- writeFileSync (
32- args . output ,
33- JSON . stringify ( introspectionFromSchema ( schema ) , null , 2 )
34- ) ;
28+ await this . runTasks ( ( { args, project, flags } ) => {
29+ const extension = args . output . split ( "." ) . pop ( ) ;
30+ const isSDLFormat = [ "graphql" , "graphqls" , "gql" ] . includes ( extension ) ;
31+ return [
32+ {
33+ title : `Saving schema to ${ args . output } ` ,
34+ task : async ( ) => {
35+ const schema = await project . resolveSchema ( { tag : flags . tag } ) ;
36+ const formattedSchema = isSDLFormat
37+ ? printSchema ( schema )
38+ : JSON . stringify ( introspectionFromSchema ( schema ) , null , 2 ) ;
39+ writeFileSync ( args . output , formattedSchema ) ;
40+ }
3541 }
36- }
37- ] ) ;
42+ ] ;
43+ } ) ;
3844 }
3945}
0 commit comments