Skip to content

Commit

Permalink
feat: --pretty and --sudo
Browse files Browse the repository at this point in the history
  • Loading branch information
lennyburdette committed Jul 14, 2022
1 parent 55aee5b commit 57c345a
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/commands/default.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { buildOperationContext } from "@apollo/gateway";
import { buildComposedSchema, QueryPlanner } from "@apollo/query-planner";
import {
buildComposedSchema,
QueryPlanner,
prettyFormatQueryPlan,
} from "@apollo/query-planner";
import { Command, Option } from "clipanion";
import { parse } from "graphql";
import { readFile } from "fs/promises";
Expand All @@ -13,6 +17,10 @@ export class DefaultCommand extends Command {

operation = Option.String("--operation", { required: true });

pretty = Option.Boolean("--pretty");

sudo = Option.Boolean("--sudo");

async execute() {
if (this.supergraph && this.graphref) {
this.context.stderr.write(
Expand All @@ -24,7 +32,7 @@ export class DefaultCommand extends Command {
const schema = this.supergraph
? await fetchSupergraphFromFile(this.supergraph)
: this.graphref
? await fetchSupergraphFromStudio(this.graphref)
? await fetchSupergraphFromStudio(this.graphref, this.sudo ?? false)
: null;

if (!schema) {
Expand All @@ -36,7 +44,12 @@ export class DefaultCommand extends Command {

const queryPlan = await generateQueryPlan(schema, operation);

this.context.stdout.write(JSON.stringify(queryPlan, null, 2));
if (this.pretty) {
this.context.stdout.write(prettyFormatQueryPlan(queryPlan));
} else {
this.context.stdout.write(JSON.stringify(queryPlan, null, 2));
}

this.context.stdout.write("\n");
}
}
Expand Down Expand Up @@ -68,8 +81,9 @@ async function fetchSupergraphFromFile(file) {

/**
* @param {string} ref
* @param {boolean} sudo
*/
async function fetchSupergraphFromStudio(ref) {
async function fetchSupergraphFromStudio(ref, sudo) {
const apiKey = process.env.APOLLO_KEY;
if (!apiKey) {
throw new Error("missing APOLLO_KEY");
Expand All @@ -80,6 +94,7 @@ async function fetchSupergraphFromStudio(ref) {
{
headers: {
"x-api-key": apiKey,
...(sudo ? { "apollo-sudo": String(sudo) } : {}),
},
}
);
Expand Down

0 comments on commit 57c345a

Please sign in to comment.