Skip to content

Commit

Permalink
Types and signature cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
trevor-scheer committed Nov 29, 2021
1 parent c705f2e commit 7714c80
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
10 changes: 4 additions & 6 deletions gateway-js/src/__tests__/gateway/supergraphSdl.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApolloGateway } from '@apollo/gateway';
import { ApolloGateway, SupergraphSdlUpdateFunction } from '@apollo/gateway';
import { fixturesWithUpdate } from 'apollo-federation-integration-testsuite';
import { createHash } from 'crypto';
import { ApolloServer } from 'apollo-server';
Expand Down Expand Up @@ -112,9 +112,9 @@ describe('Using supergraphSdl dynamic configuration', () => {
const [userPromise, resolveSupergraph] =
waitUntil<{ supergraphSdl: string }>();

let userUpdateFn: (updatedSupergraphSdl: string) => Promise<void>;
let userUpdateFn: SupergraphSdlUpdateFunction;
const gateway = new ApolloGateway({
async supergraphSdl(update) {
async supergraphSdl({ update }) {
userUpdateFn = update;
return userPromise;
},
Expand Down Expand Up @@ -179,9 +179,7 @@ describe('Using supergraphSdl dynamic configuration', () => {

it('gracefully handles Promise rejections from user `cleanup` function', async () => {
const rejectionMessage = 'thrown from cleanup function';
const cleanup = jest.fn(() =>
Promise.reject(rejectionMessage),
);
const cleanup = jest.fn(() => Promise.reject(rejectionMessage));
const gateway = new ApolloGateway({
async supergraphSdl() {
return {
Expand Down
11 changes: 9 additions & 2 deletions gateway-js/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,16 @@ export function isManuallyManagedSupergraphSdlGatewayConfig(
'supergraphSdl' in config && typeof config.supergraphSdl === 'function'
);
}

export interface SupergraphSdlUpdateFunction {
(updatedSupergraphSdl: string): Promise<void>
}
export interface SupergraphSdlUpdateOptions {
update: SupergraphSdlUpdateFunction;
}
interface ManuallyManagedSupergraphSdlGatewayConfig extends GatewayConfigBase {
supergraphSdl: (
update: (updatedSupergraphSdl: string) => Promise<void>,
opts: SupergraphSdlUpdateOptions,
) => Promise<{ supergraphSdl: string; cleanup?: () => Promise<void> }>;
}

Expand Down Expand Up @@ -231,7 +238,7 @@ export function isManuallyManagedConfig(
config: GatewayConfig,
): config is ManuallyManagedGatewayConfig {
return (
('supergraphSdl' in config && typeof config.supergraphSdl === 'function') ||
isManuallyManagedSupergraphSdlGatewayConfig(config) ||
'experimental_updateServiceDefinitions' in config ||
'experimental_updateSupergraphSdl' in config
);
Expand Down
18 changes: 11 additions & 7 deletions gateway-js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ export class ApolloGateway implements GraphQLService {
this.config.experimental_updateServiceDefinitions;
} else if (isManuallyManagedSupergraphSdlGatewayConfig(this.config)) {
this.manualConfigPromise = this.config
.supergraphSdl(this.updateWithSupergraphSdl.bind(this))
.supergraphSdl({ update: this.updateWithSupergraphSdl.bind(this) })
.catch((e) => {
// Not swallowing the error here results in an uncaught rejection.
// An error will be thrown when this promise resolves to nothing.
Expand Down Expand Up @@ -613,12 +613,6 @@ export class ApolloGateway implements GraphQLService {
private async updateWithSupergraphSdl(
result: SupergraphSdlUpdate | string,
): Promise<void> {
if (typeof result === 'string') {
} else if (result.id === this.compositionId) {
this.logger.debug('No change in composition since last check.');
return;
}

const supergraphSdl =
typeof result === 'string' ? result : result.supergraphSdl;

Expand All @@ -627,6 +621,11 @@ export class ApolloGateway implements GraphQLService {
? createHash('sha256').update(result).digest('hex')
: result.id;

if (id === this.compositionId) {
this.logger.debug('No change in composition since last check.');
return;
}

// TODO(trevor): #580 redundant parse
// This may throw, so we'll calculate early (specifically before making any updates)
// In the case that it throws, the gateway will:
Expand Down Expand Up @@ -1413,3 +1412,8 @@ export {
};

export * from './datasources';

export {
SupergraphSdlUpdateOptions,
SupergraphSdlUpdateFunction,
} from './config';

0 comments on commit 7714c80

Please sign in to comment.