Skip to content

Commit fe4d46c

Browse files
essajiJakeDawkins
authored andcommitted
Add sdl download ability to client:download-schema (#1470)
1 parent 96488d2 commit fe4d46c

3 files changed

Lines changed: 24 additions & 15 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@ vscode-apollo-*.vsix
2828
# files generated from tests
2929
__tmp__*
3030
.vscode-test
31+
32+
#idea folder
33+
.idea/

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## Upcoming
44

55
- `apollo`
6-
- <First `apollo` related entry goes here>
6+
- Add sdl download ability to `client:download-schema`
77
- `apollo-codegen-flow`
88
- <First `apollo-codegen-flow` related entry goes here>
99
- `apollo-codegen-scala`
Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { flags } from "@oclif/command";
2-
import { introspectionFromSchema } from "graphql";
2+
import { introspectionFromSchema, printSchema } from "graphql";
33
import { writeFileSync } from "fs";
44

55
import { ClientCommand } from "../../Command";
66

77
export 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

Comments
 (0)