Skip to content

Commit

Permalink
Update env vs code config behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
trevor-scheer committed Mar 15, 2021
1 parent 4cae6cb commit fcf5fc4
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 29 deletions.
50 changes: 50 additions & 0 deletions gateway-js/src/__tests__/integration/configuration.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import gql from 'graphql-tag';
import mockedEnv from 'mocked-env';
import { Logger } from 'apollo-server-types';
import { ApolloGateway } from '../..';
import {
Expand Down Expand Up @@ -187,3 +188,52 @@ describe('gateway startup errors', () => {
`);
});
});

describe('gateway config / env behavior', () => {
let gateway: ApolloGateway | null = null;
afterEach(async () => {
if (gateway) {
await gateway.stop();
gateway = null;
}
});

// TODO(trevor:cloudconfig): this behavior will be updated
describe('schema config delivery endpoint configuration', () => {
it('A code config overrides the env variable', async () => {
let cleanUp = mockedEnv({
APOLLO_SCHEMA_CONFIG_DELIVERY_ENDPOINT: 'env-config',
});

gateway = new ApolloGateway({
logger,
experimental_schemaConfigDeliveryEndpoint: 'code-config',
});

expect(gateway['experimental_schemaConfigDeliveryEndpoint']).toEqual(
'code-config',
);

gateway = null;
cleanUp();
});

it('A code config set to `null` takes precedence over an existing env variable', async () => {
let cleanUp = mockedEnv({
APOLLO_SCHEMA_CONFIG_DELIVERY_ENDPOINT: 'env-config',
});

gateway = new ApolloGateway({
logger,
experimental_schemaConfigDeliveryEndpoint: null,
});

expect(gateway['experimental_schemaConfigDeliveryEndpoint']).toEqual(
null,
);

gateway = null;
cleanUp();
});
});
})
16 changes: 0 additions & 16 deletions gateway-js/src/__tests__/integration/networkRequests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,22 +124,6 @@ it('Fetches CSDL from remote storage using a configured env variable', async ()
cleanUp();
});

it('A configured env variable overrides the code configuration', async () => {
let cleanUp = mockedEnv({
APOLLO_SCHEMA_CONFIG_DELIVERY_ENDPOINT: 'env-override',
});

gateway = new ApolloGateway({
logger,
experimental_schemaConfigDeliveryEndpoint: 'code-config',
});

expect(gateway['experimental_schemaConfigDeliveryEndpoint']).toEqual('env-override');

gateway = null;
cleanUp();
});

it('Updates CSDL from remote storage', async () => {
mockCsdlRequestSuccess();
mockCsdlRequestSuccess(getTestingCsdl(fixturesWithUpdate), 'updatedId-5678');
Expand Down
16 changes: 14 additions & 2 deletions gateway-js/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ interface GatewayConfigBase {
experimental_autoFragmentization?: boolean;
fetcher?: typeof fetch;
serviceHealthCheck?: boolean;
/**
* @deprecated This configuration option shouldn't be used unless by
* recommendation from Apollo staff. This behavior will be
* defaulted in a future release and this option will strictly be
* used as an override.
*/
experimental_schemaConfigDeliveryEndpoint?: null;
}

export interface RemoteGatewayConfig extends GatewayConfigBase {
Expand All @@ -138,8 +145,10 @@ export interface LegacyManagedGatewayConfig extends GatewayConfigBase {
federationVersion?: number;
}

// This Omitted step is needed to "override"
type Omitted = Omit<GatewayConfigBase, 'experimental_schemaConfigDeliveryEndpoint'>;
// TODO(trevor:cloudconfig): This type becomes the only managed config
export interface PrecomposedManagedGatewayConfig extends GatewayConfigBase {
export interface PrecomposedManagedGatewayConfig extends Omitted {
/**
* @deprecated This configuration option shouldn't be used unless by
* recommendation from Apollo staff. This behavior will be
Expand Down Expand Up @@ -223,7 +232,10 @@ export function isManagedConfig(
export function isPrecomposedManagedConfig(
config: GatewayConfig,
): config is PrecomposedManagedGatewayConfig {
return 'experimental_schemaConfigDeliveryEndpoint' in config;
return (
'experimental_schemaConfigDeliveryEndpoint' in config &&
config.experimental_schemaConfigDeliveryEndpoint !== null
);
}

// A static config is one which loads synchronously on start and never updates
Expand Down
20 changes: 9 additions & 11 deletions gateway-js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,21 +215,19 @@ export class ApolloGateway implements GraphQLService {

this.experimental_pollInterval = config?.experimental_pollInterval;


// Do not use this unless advised by Apollo staff to do so
// 1. If config is explicitly set to a `string` or `null` in code, use it
// 2. Else, if the env var is set, use that
// 3. Else, default to `null`
this.experimental_schemaConfigDeliveryEndpoint =
process.env.APOLLO_SCHEMA_CONFIG_DELIVERY_ENDPOINT ?? null;

if (isPrecomposedManagedConfig(this.config)) {
// If the env was already set, it will maintain precedence here.
this.experimental_schemaConfigDeliveryEndpoint =
this.experimental_schemaConfigDeliveryEndpoint ??
this.config.experimental_schemaConfigDeliveryEndpoint ??
null;
}
isPrecomposedManagedConfig(this.config) ||
this.config.experimental_schemaConfigDeliveryEndpoint === null
? // We know at this point the config is either string | null, but TS thinks it can also be undefined
(this.config.experimental_schemaConfigDeliveryEndpoint as string | null)
: process.env.APOLLO_SCHEMA_CONFIG_DELIVERY_ENDPOINT ?? null;

// Use the provided updater function if provided by the user, else default
if (isManuallyManagedConfig(this.config)) {
// Use the provided updater function if provided by the user, else default
if ('experimental_updateCsdl' in this.config) {
this.updateServiceDefinitions = this.config.experimental_updateCsdl;
} else if ('experimental_updateServiceDefinitions' in this.config) {
Expand Down

0 comments on commit fcf5fc4

Please sign in to comment.