Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## Upcoming

- `apollo`
- https://github.com/apollographql/apollo-tooling/pull/1851
- Support APOLLO_KEY and deprecate ENGINE_API_KEY for .env support
- https://github.com/apollographql/apollo-tooling/pull/1849
- Update all commands that supported --tag to prefer --variant and indicate a deprecation warning for --tag
- All usages of --tag will continue to work
Expand Down
48 changes: 41 additions & 7 deletions packages/apollo-language-server/src/config/__tests__/loadConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
DefaultServiceConfig,
DefaultEngineConfig
} from "../config";
import { Debug } from "../../utilities";

const makeNestedDir = dir => {
if (fs.existsSync(dir)) return;
Expand Down Expand Up @@ -279,7 +280,7 @@ describe("loadConfig", () => {
it("finds .env in config path & parses for key", async () => {
writeFilesToDir(dir, {
"my.config.js": `module.exports = { client: { name: 'hello' } }`,
".env": `ENGINE_API_KEY=service:harambe:54378950jn`
".env": `APOLLO_KEY=service:harambe:54378950jn`
});

const config = await loadConfig({
Expand All @@ -293,7 +294,7 @@ describe("loadConfig", () => {
it("finds .env.local in config path & parses for key", async () => {
writeFilesToDir(dir, {
"my.config.js": `module.exports = { client: { name: 'hello' } }`,
".env.local": `ENGINE_API_KEY=service:harambe:54378950jn`
".env.local": `APOLLO_KEY=service:harambe:54378950jn`
});

const config = await loadConfig({
Expand All @@ -307,23 +308,56 @@ describe("loadConfig", () => {
it("finds .env and .env.local in config path & parses for key, preferring .env.local", async () => {
writeFilesToDir(dir, {
"my.config.js": `module.exports = { client: { name: 'hello' } }`,
".env": `ENGINE_API_KEY=service:hamato:54378950jn`,
".env": `APOLLO_KEY=service:hamato:54378950jn`,
".env.local": `APOLLO_KEY=service:yoshi:65489061ko`
});

const config = await loadConfig({
configPath: dirPath,
configFileName: "my.config.js"
});

expect(config.client.service).toEqual("yoshi");
});

it("Allows setting ENGINE_API_KEY with a deprecation warning", async () => {
writeFilesToDir(dir, {
"my.config.js": `module.exports = { client: { name: 'hello' } }`,
".env.local": `ENGINE_API_KEY=service:yoshi:65489061ko`
});

const spy = jest.spyOn(Debug, "warning");

const config = await loadConfig({
configPath: dirPath,
configFileName: "my.config.js"
});

expect(config.client.service).toEqual("yoshi");
expect(spy).toHaveBeenCalledWith(
expect.stringMatching(/Deprecation warning/i)
);
});

it("Throws when .env defined legacy and new key", async () => {
writeFilesToDir(dir, {
"my.config.js": `module.exports = { client: { name: 'hello' } }`,
".env.local": `ENGINE_API_KEY=service:yoshi:65489061ko\nAPOLLO_KEY=service:yoshi:65489061ko`
});

const loadConfigPromise = loadConfig({
configPath: dirPath,
configFileName: "my.config.js"
});

await expect(loadConfigPromise).rejects.toThrow(/Cannot set both/);
});

// this doesn't work right now :)
xit("finds .env in cwd & parses for key", async () => {
writeFilesToDir(dir, {
"dir/my.config.js": `module.exports = { client: { name: 'hello' } }`,
".env": `ENGINE_API_KEY=service:harambe:54378950jn`
".env": `APOLLO_KEY=service:harambe:54378950jn`
});
process.chdir(dir);
const config = await loadConfig({
Expand Down Expand Up @@ -382,7 +416,7 @@ describe("loadConfig", () => {
it("lets config service name take precedence for client project", async () => {
writeFilesToDir(dir, {
"my.config.js": `module.exports = { client: { service: 'hello' } }`,
".env": `ENGINE_API_KEY=service:harambe:54378950jn`
".env": `APOLLO_KEY=service:harambe:54378950jn`
});

const config = await loadConfig({
Expand All @@ -397,7 +431,7 @@ describe("loadConfig", () => {
it("lets name passed in take precedence over env var", async () => {
writeFilesToDir(dir, {
"my.config.js": `module.exports = { client: { } }`,
".env": `ENGINE_API_KEY=service:harambe:54378950jn`
".env": `APOLLO_KEY=service:harambe:54378950jn`
});

const config = await loadConfig({
Expand All @@ -412,7 +446,7 @@ describe("loadConfig", () => {
it("uses env var to determine service name when no other options", async () => {
writeFilesToDir(dir, {
"my.config.js": `module.exports = { client: { } }`,
".env": `ENGINE_API_KEY=service:harambe:54378950jn`
".env": `APOLLO_KEY=service:harambe:54378950jn`
});

const config = await loadConfig({
Expand Down
17 changes: 15 additions & 2 deletions packages/apollo-language-server/src/config/loadConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ const loaders = {
}
};

export const legacyKeyEnvVar = "ENGINE_API_KEY";
export const keyEnvVar = "APOLLO_KEY";

export interface LoadConfigSettings {
// the current working directory to start looking for the config
// config loading only works on node so we default to
Expand Down Expand Up @@ -123,9 +126,19 @@ export async function loadConfig({
const env: { [key: string]: string } = require("dotenv").parse(
readFileSync(dotEnvPath)
);
if (env["ENGINE_API_KEY"]) {
apiKey = env["ENGINE_API_KEY"];
const legacyKey = env[legacyKeyEnvVar];
const key = env[keyEnvVar];
if (legacyKey && key) {
throw new Error(
`Cannot set both ${legacyKeyEnvVar} and ${keyEnvVar}. Please only set ${keyEnvVar}`
);
}
if (legacyKey) {
Debug.warning(
`[Deprecation warning] Setting the key via ${legacyKeyEnvVar} is deprecated and will not be supported in future versions.`
);
}
apiKey = key || legacyKey;
}
});

Expand Down
14 changes: 10 additions & 4 deletions packages/apollo-language-server/src/graphqlTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ export interface CheckPartialSchema_service_checkPartialSchema_checkSchemaResult
/**
* delta in seconds from current time that determines the start of the window
* for reported metrics included in a schema diff. A day window from the present
* day would have a \`from\` value of -86400. In rare cases, this could be an ISO
* day would have a `from` value of -86400. In rare cases, this could be an ISO
* timestamp if the user passed one in on diff creation
*/
from: any | null;
/**
* delta in seconds from current time that determines the end of the
* window for reported metrics included in a schema diff. A day window
* from the present day would have a \`to\` value of -0. In rare
* from the present day would have a `to` value of -0. In rare
* cases, this could be an ISO timestamp if the user passed one in on diff
* creation
*/
Expand Down Expand Up @@ -207,14 +207,14 @@ export interface CheckSchema_service_checkSchema_diffToPrevious_validationConfig
/**
* delta in seconds from current time that determines the start of the window
* for reported metrics included in a schema diff. A day window from the present
* day would have a \`from\` value of -86400. In rare cases, this could be an ISO
* day would have a `from` value of -86400. In rare cases, this could be an ISO
* timestamp if the user passed one in on diff creation
*/
from: any | null;
/**
* delta in seconds from current time that determines the end of the
* window for reported metrics included in a schema diff. A day window
* from the present day would have a \`to\` value of -0. In rare
* from the present day would have a `to` value of -0. In rare
* cases, this could be an ISO timestamp if the user passed one in on diff
* creation
*/
Expand Down Expand Up @@ -520,7 +520,13 @@ export interface SchemaTagsAndFieldStats_service_stats_fieldStats_metrics {

export interface SchemaTagsAndFieldStats_service_stats_fieldStats {
__typename: "ServiceFieldStatsRecord";
/**
* Dimensions of ServiceFieldStats that can be grouped by.
*/
groupBy: SchemaTagsAndFieldStats_service_stats_fieldStats_groupBy;
/**
* Metrics of ServiceFieldStats that can be aggregated over.
*/
metrics: SchemaTagsAndFieldStats_service_stats_fieldStats_metrics;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/apollo-language-server/src/project/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { GraphQLDocument, extractGraphQLDocuments } from "../document";

import { LoadingHandler } from "../loadingHandler";
import { FileSet } from "../fileSet";
import { ApolloConfig } from "../config";
import { ApolloConfig, keyEnvVar } from "../config";
import {
schemaProviderFromConfig,
GraphQLSchemaProvider,
Expand Down Expand Up @@ -136,7 +136,7 @@ export abstract class GraphQLProject implements GraphQLSchemaProvider {
// handle error states for missing engine config
// all in the same place :tada:
if (!this.engineClient) {
throw new Error("Unable to find ENGINE_API_KEY");
throw new Error(`Unable to find ${keyEnvVar}`);
}
return this.engineClient!;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { NotificationHandler } from "vscode-languageserver";
import gql from "graphql-tag";
import { GraphQLSchema, buildClientSchema } from "graphql";
import { ApolloEngineClient, ClientIdentity } from "../../engine";
import { ClientConfig, parseServiceSpecifier } from "../../config";
import { ClientConfig, keyEnvVar, parseServiceSpecifier } from "../../config";
import { getServiceFromKey, isServiceKey } from "../../config";
import {
GraphQLSchemaProvider,
Expand Down Expand Up @@ -36,7 +36,9 @@ export class EngineSchemaProvider implements GraphQLSchemaProvider {
// create engine client
if (!this.client) {
if (!engine.apiKey) {
throw new Error("ENGINE_API_KEY not found");
throw new Error(
`No API key found. Please set ${keyEnvVar} or use --key`
);
}
this.client = new ApolloEngineClient(
engine.apiKey,
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo/src/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export abstract class ProjectCommand extends Command {
key: flags.string({
description:
"The API key to use for authentication to Apollo Graph Manager",
default: () => process.env.ENGINE_API_KEY
default: () => process.env.APOLLO_KEY || process.env.ENGINE_API_KEY
}),
engine: flags.string({
description: "URL for a custom Apollo Graph Manager deployment",
Expand Down
20 changes: 10 additions & 10 deletions packages/apollo/src/commands/client/__tests__/check.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ it("is turned on after summit", () => {});

// import { fs as mockFS, vol } from "apollo-codegen-core/lib/localfs";
// const test = setup.do(() => mockConsole());
// const ENGINE_API_KEY = "service:test:1234";
// const APOLLO_KEY = "service:test:1234";
// const hash = "12345";

// const dummyOperations = [
Expand All @@ -26,7 +26,7 @@ it("is turned on after summit", () => {});

// const engineSuccess = ({ operations, tag, results } = {}) => nock => {
// nock
// .matchHeader("x-api-key", ENGINE_API_KEY)
// .matchHeader("x-api-key", APOLLO_KEY)
// .post("/", {
// operationName: "CheckOperations",
// variables: {
Expand Down Expand Up @@ -85,7 +85,7 @@ it("is turned on after summit", () => {});
// test
// .do(() => vol.fromJSON(files))
// .nock(ENGINE_URI, engineSuccess())
// .env({ ENGINE_API_KEY })
// .env({ APOLLO_KEY })
// .stdout()
// .command(["queries:check"])
// .exit(1)
Expand All @@ -103,7 +103,7 @@ it("is turned on after summit", () => {});
// "apollo": {
// "schemas": {
// "default": {
// "engineKey": "${ENGINE_API_KEY}"
// "engineKey": "${APOLLO_KEY}"
// }
// }
// }
Expand Down Expand Up @@ -132,7 +132,7 @@ it("is turned on after summit", () => {});
// "apollo": {
// "schemas": {
// "default": {
// "engineKey": "${ENGINE_API_KEY}"
// "engineKey": "${APOLLO_KEY}"
// }
// }
// }
Expand All @@ -156,7 +156,7 @@ it("is turned on after summit", () => {});
// .do(() => vol.fromJSON(files))
// .nock(ENGINE_URI, engineSuccess())
// .stdout()
// .command(["queries:check", `--key=${ENGINE_API_KEY}`])
// .command(["queries:check", `--key=${APOLLO_KEY}`])
// .exit(1)
// .it("allows custom api key", () => {
// expect(stdout).toContain("FAILURE");
Expand All @@ -166,7 +166,7 @@ it("is turned on after summit", () => {});
// test
// .do(() => vol.fromJSON(files))
// .nock(ENGINE_URI, engineSuccess({ results: [] }))
// .env({ ENGINE_API_KEY })
// .env({ APOLLO_KEY })
// .stdout()
// .command(["queries:check"])
// .it(
Expand All @@ -187,7 +187,7 @@ it("is turned on after summit", () => {});
// vol.fromJSON(nested);
// })
// .nock(ENGINE_URI, engineSuccess())
// .env({ ENGINE_API_KEY })
// .env({ APOLLO_KEY })
// .stdout()
// .command(["queries:check", "--queries=./client/*.graphql"])
// .exit(1)
Expand All @@ -203,7 +203,7 @@ it("is turned on after summit", () => {});
// "https://engine.example.com",
// engineSuccess({ engine: "https://engine.example.com" })
// )
// .env({ ENGINE_API_KEY })
// .env({ APOLLO_KEY })
// .command(["queries:check", "--engine=https://engine.example.com"])
// .exit(1)
// .it("compares against a schema from a custom registry", std => {
Expand All @@ -214,7 +214,7 @@ it("is turned on after summit", () => {});
// test
// .do(() => vol.fromJSON(files))
// .nock(ENGINE_URI, engineSuccess())
// .env({ ENGINE_API_KEY })
// .env({ APOLLO_KEY })
// .stdout()
// .command(["queries:check", "--json"])
// .exit(1)
Expand Down
Loading