Skip to content

Commit a83a1f5

Browse files
author
Adam Zionts
committed
Update both keys set to warn
If both APOLLO_KEY and ENGINE_API_KEY are set, this will now warn and prefer APOLLO_KEY, in accordance with changes to apollo-tooling. Imp personally not very opinionated on this front, so happy to take direction from the reviewer.
1 parent 493c4aa commit a83a1f5

3 files changed

Lines changed: 26 additions & 16 deletions

File tree

packages/apollo-engine-reporting/src/agent.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,20 @@ export type GenerateClientInfo<TContext> = (
4747
requestContext: GraphQLRequestContext<TContext>,
4848
) => ClientInfo;
4949

50-
export function getEngineApiKey(engine: EngineReportingOptions<any> | boolean | undefined) {
51-
if (typeof engine === 'object' && engine.apiKey) {
52-
return engine.apiKey;
50+
export function getEngineApiKey(engine: EngineReportingOptions<any> | boolean | undefined, skipWarn: boolean = false) {
51+
if (typeof engine === 'object') {
52+
if (engine.apiKey) {
53+
return engine.apiKey;
54+
}
5355
}
5456
const legacyApiKeyFromEnv = process.env.ENGINE_API_KEY;
5557
const apiKeyFromEnv = process.env.APOLLO_KEY;
5658

57-
if(legacyApiKeyFromEnv && apiKeyFromEnv) {
58-
throw new Error(`Cannot set both APOLLO_KEY and ENGINE_API_KEY. Please only set APOLLO_KEY.`);
59+
if(legacyApiKeyFromEnv && apiKeyFromEnv && !skipWarn) {
60+
console.warn(`Both ENGINE_API_KEY (deprecated) and APOLLO_KEY are set; defaulting to APOLLO_KEY.`);
5961
}
60-
if(legacyApiKeyFromEnv && !warnedOnDeprecatedApiKey) {
61-
console.warn(`[Deprecation warning] Setting the key via ENGINE_API_KEY is deprecated and will not be supported in future versions.`);
62+
if(legacyApiKeyFromEnv && !warnedOnDeprecatedApiKey && !skipWarn) {
63+
console.warn(`[deprecated] Setting the key via ENGINE_API_KEY is deprecated and will not be supported in future versions.`);
6264
warnedOnDeprecatedApiKey = true;
6365
}
6466
return apiKeyFromEnv || legacyApiKeyFromEnv || ''

packages/apollo-server-core/src/ApolloServer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ function getEngineGraphVariant(engine: Config['engine']): string | undefined {
9090
return engine.graphVariant || engine.schemaTag;
9191
} else {
9292
if (process.env.ENGINE_SCHEMA_TAG) {
93-
console.warn('[Deprecation warning] Usage of ENGINE_SCHEMA_TAG is deprecated. Please use APOLLO_GRAPH_VARIANT instead.');
93+
console.warn('[deprecated] Usage of ENGINE_SCHEMA_TAG is deprecated. Please use APOLLO_GRAPH_VARIANT instead.');
9494
}
9595
if (process.env.ENGINE_SCHEMA_TAG && process.env.APOLLO_GRAPH_VARIANT) {
9696
throw new Error('Cannot set both ENGINE_SCHEMA_TAG and APOLLO_GRAPH_VARIANT. Please use APOLLO_GRAPH_VARIANT.')
@@ -100,7 +100,7 @@ function getEngineGraphVariant(engine: Config['engine']): string | undefined {
100100
}
101101

102102
function getEngineServiceId(engine: Config['engine']): string | undefined {
103-
const engineApiKey = getEngineApiKey(engine);
103+
const engineApiKey = getEngineApiKey(engine, true);
104104
if (engineApiKey) {
105105
return engineApiKey.split(':', 2)[1];
106106
}
@@ -304,7 +304,7 @@ export class ApolloServerBase {
304304
// The truthyness of this value can also be used in other forks of logic
305305
// related to Engine, as is the case with EngineReportingAgent just below.
306306
this.engineServiceId = getEngineServiceId(engine);
307-
const apiKey = getEngineApiKey(engine);
307+
const apiKey = getEngineApiKey(engine, true);
308308
if (apiKey) {
309309
this.engineApiKeyHash = createSHA('sha512')
310310
.update(apiKey)
@@ -314,7 +314,7 @@ export class ApolloServerBase {
314314
if (this.engineServiceId) {
315315
const { EngineReportingAgent } = require('apollo-engine-reporting');
316316
this.engineReportingAgent = new EngineReportingAgent(
317-
typeof engine === 'object' ? engine : Object.create(null),
317+
typeof engine === 'object' ? engine : Object.create(null)
318318
);
319319
// Don't add the extension here (we want to add it later in generateSchemaDerivedData).
320320
}

packages/apollo-server-core/src/__tests__/ApolloServerBase.test.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,29 @@ describe('environment variables', () => {
6969
const spyConsoleWarn = jest.spyOn(console, 'warn').mockImplementation();
7070

7171
const server = new ApolloServerBase({
72-
schema: buildServiceDefinition([{ typeDefs, resolvers }]).schema,
72+
typeDefs,
73+
resolvers
7374
});
7475

7576
await server.stop();
7677
expect(spyConsoleWarn).toHaveBeenCalledTimes(1);
7778
spyConsoleWarn.mockReset();
7879
});
7980

80-
it('throws an error with both the legacy env var and new env var set', async () => {
81+
it('warns with both the legacy env var and new env var set', async () => {
8182
// set the variables
8283
process.env.ENGINE_API_KEY = 'just:fake:stuff';
8384
process.env.APOLLO_KEY = 'also:fake:stuff';
85+
const spyConsoleWarn = jest.spyOn(console, 'warn').mockImplementation();
86+
87+
const server = new ApolloServerBase({
88+
typeDefs,
89+
resolvers
90+
});
8491

85-
expect( () => new ApolloServerBase({
86-
schema: buildServiceDefinition([{ typeDefs, resolvers }]).schema,
87-
})).toThrow();
92+
await server.stop();
93+
// Once for deprecation, once for double-set
94+
expect(spyConsoleWarn).toHaveBeenCalledTimes(2);
95+
spyConsoleWarn.mockReset();
8896
});
8997
});

0 commit comments

Comments
 (0)