From e214efa5444f3fa3a1b7c9f2020a2a25f811ea26 Mon Sep 17 00:00:00 2001 From: Trevor Scheer Date: Tue, 5 Mar 2019 16:27:40 -0800 Subject: [PATCH 1/2] JSON flag for service:check output Add a --json flag as an option for output from service:check. This makes the output more easily parsed by other tools. --- package-lock.json | 59 +++++++--- packages/apollo/src/commands/service/check.ts | 108 ++++++++++-------- 2 files changed, 101 insertions(+), 66 deletions(-) diff --git a/package-lock.json b/package-lock.json index bc5a9c787f..a5fa498352 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2215,7 +2215,8 @@ "dependencies": { "@babel/generator": { "version": "7.3.4", - "bundled": true, + "resolved": "https://registry.npmjs.org/@babel/generator/-/generator-7.3.4.tgz", + "integrity": "sha512-8EXhHRFqlVVWXPezBW5keTiQi/rJMQTg/Y9uVCEZ0CAF3PKtCCaVRnp64Ii1ujhkoDhhF1fVsImoN4yJ2uz4Wg==", "requires": { "@babel/types": "^7.3.4", "jsesc": "^2.5.1", @@ -2226,7 +2227,8 @@ }, "@babel/types": { "version": "7.3.4", - "bundled": true, + "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.3.4.tgz", + "integrity": "sha512-WEkp8MsLftM7O/ty580wAmZzN1nDmCACc5+jFzUt+GUFNNIi3LdRlueYz0YIlmJhlZx1QYDMZL5vdWCL0fNjFQ==", "requires": { "esutils": "^2.0.2", "lodash": "^4.17.11", @@ -2249,7 +2251,8 @@ "dependencies": { "@babel/generator": { "version": "7.3.4", - "bundled": true, + "resolved": "https://registry.npmjs.org/@babel/generator/-/generator-7.3.4.tgz", + "integrity": "sha512-8EXhHRFqlVVWXPezBW5keTiQi/rJMQTg/Y9uVCEZ0CAF3PKtCCaVRnp64Ii1ujhkoDhhF1fVsImoN4yJ2uz4Wg==", "requires": { "@babel/types": "^7.3.4", "jsesc": "^2.5.1", @@ -2260,7 +2263,8 @@ }, "@babel/types": { "version": "7.3.4", - "bundled": true, + "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.3.4.tgz", + "integrity": "sha512-WEkp8MsLftM7O/ty580wAmZzN1nDmCACc5+jFzUt+GUFNNIi3LdRlueYz0YIlmJhlZx1QYDMZL5vdWCL0fNjFQ==", "requires": { "esutils": "^2.0.2", "lodash": "^4.17.11", @@ -2303,7 +2307,8 @@ "dependencies": { "@babel/generator": { "version": "7.3.4", - "bundled": true, + "resolved": "https://registry.npmjs.org/@babel/generator/-/generator-7.3.4.tgz", + "integrity": "sha512-8EXhHRFqlVVWXPezBW5keTiQi/rJMQTg/Y9uVCEZ0CAF3PKtCCaVRnp64Ii1ujhkoDhhF1fVsImoN4yJ2uz4Wg==", "requires": { "@babel/types": "^7.3.4", "jsesc": "^2.5.1", @@ -2314,7 +2319,8 @@ }, "@babel/types": { "version": "7.3.4", - "bundled": true, + "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.3.4.tgz", + "integrity": "sha512-WEkp8MsLftM7O/ty580wAmZzN1nDmCACc5+jFzUt+GUFNNIi3LdRlueYz0YIlmJhlZx1QYDMZL5vdWCL0fNjFQ==", "requires": { "esutils": "^2.0.2", "lodash": "^4.17.11", @@ -6125,7 +6131,8 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "aproba": { "version": "1.2.0", @@ -6146,12 +6153,14 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, + "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -6166,17 +6175,20 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "core-util-is": { "version": "1.0.2", @@ -6293,7 +6305,8 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "ini": { "version": "1.3.5", @@ -6305,6 +6318,7 @@ "version": "1.0.0", "bundled": true, "dev": true, + "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -6319,6 +6333,7 @@ "version": "3.0.4", "bundled": true, "dev": true, + "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -6326,12 +6341,14 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "minipass": { "version": "2.3.5", "bundled": true, "dev": true, + "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -6350,6 +6367,7 @@ "version": "0.5.1", "bundled": true, "dev": true, + "optional": true, "requires": { "minimist": "0.0.8" } @@ -6430,7 +6448,8 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "object-assign": { "version": "4.1.1", @@ -6442,6 +6461,7 @@ "version": "1.4.0", "bundled": true, "dev": true, + "optional": true, "requires": { "wrappy": "1" } @@ -6527,7 +6547,8 @@ "safe-buffer": { "version": "5.1.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "safer-buffer": { "version": "2.1.2", @@ -6563,6 +6584,7 @@ "version": "1.0.2", "bundled": true, "dev": true, + "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -6582,6 +6604,7 @@ "version": "3.0.1", "bundled": true, "dev": true, + "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -6625,12 +6648,14 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "yallist": { "version": "3.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true } } }, diff --git a/packages/apollo/src/commands/service/check.ts b/packages/apollo/src/commands/service/check.ts index 44de02318b..890f803ca7 100644 --- a/packages/apollo/src/commands/service/check.ts +++ b/packages/apollo/src/commands/service/check.ts @@ -2,24 +2,16 @@ import { flags } from "@oclif/command"; import { table } from "heroku-cli-util"; import { introspectionFromSchema } from "graphql"; import chalk from "chalk"; -import { gitInfo } from "../../git"; +import { gitInfo, GitContext } from "../../git"; import { ProjectCommand } from "../../Command"; import { validateHistoricParams } from "../../utils"; +import { + CheckSchema_service_checkSchema, + CheckSchema_service_checkSchema_diffToPrevious_changes as Change, + ChangeType +} from "apollo-language-server/lib/graphqlTypes"; -// TODO These types can/should be generated from the engine schema when we -// dogfood codegen inside of the apollo-tooling repo -interface SchemaChange { - type: ChangeType; - code: string; - description: string; -} -enum ChangeType { - FAILURE = "FAILURE", - WARNING = "WARNING", - NOTICE = "NOTICE" -} - -const formatChange = (change: SchemaChange) => { +const formatChange = (change: Change) => { let color = (x: string): string => x; if (change.type === ChangeType.FAILURE) { color = chalk.red; @@ -56,49 +48,67 @@ export default class ServiceCheck extends ProjectCommand { queryCountThresholdPercentage: flags.integer({ description: "Number of requests within the requested time window for a query to be considered, relative to total request count. Expected values are between 0 and 0.05 (minimum 5% of total request volume)" + }), + json: flags.boolean({ + description: + "Output result in json, which can then be parsed by CLI tools such as jq." }) }; async run() { - const { gitContext, checkSchemaResult }: any = await this.runTasks( - ({ config, flags, project }) => [ - { - title: "Checking service for changes", - task: async ctx => { - if (!config.name) { - throw new Error("No service found to link to Engine"); - } + const { + gitContext, + checkSchemaResult, + shouldOutputJson + } = await this.runTasks<{ + gitContext?: GitContext; + checkSchemaResult: CheckSchema_service_checkSchema; + shouldOutputJson: boolean; + }>(({ config, flags, project }) => [ + { + title: "Checking service for changes", + task: async ctx => { + if (!config.name) { + throw new Error("No service found to link to Engine"); + } - const tag = flags.tag || config.tag || "current"; - const schema = await project.resolveSchema({ tag }); - ctx.gitContext = await gitInfo(); + const tag = flags.tag || config.tag || "current"; + const schema = await project.resolveSchema({ tag }); + ctx.gitContext = await gitInfo(); - const historicParameters = validateHistoricParams({ - validationPeriod: flags.validationPeriod, - queryCountThreshold: flags.queryCountThreshold, - queryCountThresholdPercentage: flags.queryCountThresholdPercentage - }); + const historicParameters = validateHistoricParams({ + validationPeriod: flags.validationPeriod, + queryCountThreshold: flags.queryCountThreshold, + queryCountThresholdPercentage: flags.queryCountThresholdPercentage + }); - ctx.checkSchemaResult = await project.engine.checkSchema({ - id: config.name, - // @ts-ignore - // XXX Looks like TS should be generating ReadonlyArrays instead - schema: introspectionFromSchema(schema).__schema, - tag: flags.tag, - gitContext: ctx.gitContext, - frontend: flags.frontend || config.engine.frontend, - ...(historicParameters && { historicParameters }) - }); - } + ctx.checkSchemaResult = await project.engine.checkSchema({ + id: config.name, + // @ts-ignore + // XXX Looks like TS should be generating ReadonlyArrays instead + schema: introspectionFromSchema(schema).__schema, + tag: flags.tag, + gitContext: ctx.gitContext, + frontend: flags.frontend || config.engine.frontend, + ...(historicParameters && { historicParameters }) + }); + + ctx.shouldOutputJson = flags.json; } - ] - ); + } + ]); - const { targetUrl, diffToPrevious } = checkSchemaResult; - const { changes /*, type, validationConfig */ } = diffToPrevious; - const failures = changes.filter( - ({ type }: SchemaChange) => type === ChangeType.FAILURE - ); + const { + targetUrl, + diffToPrevious: { changes, validationConfig } + } = checkSchemaResult; + const failures = changes.filter(({ type }) => type === ChangeType.FAILURE); + + if (shouldOutputJson) { + return this.log( + JSON.stringify({ targetUrl, changes, validationConfig }, null, 2) + ); + } if (changes.length === 0) { return this.log("\nNo changes present between schemas\n"); From 560f7d7bf919b61f828bedd4537b4409ea8ae909 Mon Sep 17 00:00:00 2001 From: Trevor Scheer Date: Tue, 5 Mar 2019 16:32:39 -0800 Subject: [PATCH 2/2] update changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 99dc982a10..9fc3e0a515 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## Upcoming +- apollo + - JSON flag for service:check output [#1079](https://github.com/apollographql/apollo-tooling/pull/1079) + ## `apollo@2.6.0`, `apollo-env@0.3.4`, `apollo-language-server@1.5.3`, `apollo-codegen-flow@0.32.9`, `apollo-codegen-scala@0.33.5`, `apollo-codegen-swift@0.32.9`, `apollo-codegen-typescript@0.32.10`, `apollo-graphql@0.1.2` - `apollo@2.6.0`