Skip to content

Commit

Permalink
Rename to IntrospectAndCompose
Browse files Browse the repository at this point in the history
  • Loading branch information
trevor-scheer committed Dec 9, 2021
1 parent c30c19e commit bef9604
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
11 changes: 5 additions & 6 deletions gateway-js/src/legacy/IntrospectAndCompose.ts
Expand Up @@ -19,7 +19,7 @@ import {
} from '../loadServicesFromRemoteEndpoint';
import { waitUntil } from '../utilities/waitUntil';

export interface ServiceListShimOptions {
export interface IntrospectAndComposeOptions {
serviceList: ServiceEndpointDefinition[];
introspectionHeaders?:
| HeadersInit
Expand All @@ -31,12 +31,12 @@ export interface ServiceListShimOptions {
logger?: Logger;
}

type ShimState =
type State =
| { phase: 'initialized' }
| { phase: 'polling'; pollingPromise?: Promise<void> }
| { phase: 'stopped' };

export class ServiceListShim extends CallableInstance<
export class IntrospectAndCompose extends CallableInstance<
Parameters<SupergraphSdlHook>,
ReturnType<SupergraphSdlHook>
> {
Expand All @@ -53,10 +53,10 @@ export class ServiceListShim extends CallableInstance<
private serviceSdlCache: Map<string, string> = new Map();
private pollIntervalInMs?: number;
private timerRef: NodeJS.Timeout | null = null;
private state: ShimState;
private state: State;
private logger?: Logger;

constructor(options: ServiceListShimOptions) {
constructor(options: IntrospectAndComposeOptions) {
super('instanceCallableMethod');
// this.buildService needs to be assigned before this.serviceList is built
this.buildService = options.buildService;
Expand All @@ -74,7 +74,6 @@ export class ServiceListShim extends CallableInstance<
private async instanceCallableMethod(
...[{ update }]: Parameters<SupergraphSdlHook>
) {
debugger;
this.update = update;

const initialSupergraphSdl = await this.updateSupergraphSdl();
Expand Down
30 changes: 15 additions & 15 deletions gateway-js/src/legacy/__tests__/IntrospectAndCompose.test.ts
Expand Up @@ -4,13 +4,13 @@ import {
fixturesWithUpdate,
} from 'apollo-federation-integration-testsuite';
import { RemoteGraphQLDataSource, ServiceEndpointDefinition } from '../..';
import { ServiceListShim } from '../serviceListShim';
import { IntrospectAndCompose } from '../IntrospectAndCompose';
import { mockAllServicesSdlQuerySuccess } from '../../__tests__/integration/nockMocks';
import { wait } from '../../__tests__/execution-utils';
import { waitUntil } from '../../utilities/waitUntil';
import { Logger } from 'apollo-server-types';

describe('ServiceListShim', () => {
describe('IntrospectAndCompose', () => {
beforeEach(() => {
if (!nock.isActive()) nock.activate();
});
Expand All @@ -24,17 +24,17 @@ describe('ServiceListShim', () => {
it('constructs', () => {
expect(
() =>
new ServiceListShim({
new IntrospectAndCompose({
serviceList: fixtures,
}),
).not.toThrow();
});

it('is instance callable (simulating the gateway calling it)', async () => {
mockAllServicesSdlQuerySuccess();
const shim = new ServiceListShim({ serviceList: fixtures });
const instance = new IntrospectAndCompose({ serviceList: fixtures });
await expect(
shim({ update() {}, async healthCheck() {} }),
instance({ update() {}, async healthCheck() {} }),
).resolves.toBeTruthy();
});

Expand All @@ -52,7 +52,7 @@ describe('ServiceListShim', () => {

const processSpies: jest.Mock[] = [];

const shim = new ServiceListShim({
const instance = new IntrospectAndCompose({
serviceList: fixtures,
buildService(def) {
const { datasource, processSpy } = getDataSourceSpy(def);
Expand All @@ -61,7 +61,7 @@ describe('ServiceListShim', () => {
},
});

await shim({ update() {}, async healthCheck() {} });
await instance({ update() {}, async healthCheck() {} });

expect(processSpies.length).toBe(fixtures.length);
for (const processSpy of processSpies) {
Expand Down Expand Up @@ -90,12 +90,12 @@ describe('ServiceListShim', () => {
.mockImplementationOnce(() => r2())
.mockImplementationOnce(() => r3());

const shim = new ServiceListShim({
const instance = new IntrospectAndCompose({
serviceList: fixtures,
pollIntervalInMs: 10,
});

const { cleanup } = await shim({
const { cleanup } = await instance({
update(supergraphSdl) {
updateSpy(supergraphSdl);
},
Expand All @@ -113,7 +113,7 @@ describe('ServiceListShim', () => {

// ensure we cancelled the timer
// @ts-ignore
expect(shim.timerRef).toBe(null);
expect(instance.timerRef).toBe(null);
});

// TODO: useFakeTimers (though I'm struggling to get this to work as expected)
Expand All @@ -129,7 +129,7 @@ describe('ServiceListShim', () => {
mockAllServicesSdlQuerySuccess();
mockAllServicesSdlQuerySuccess();

const shim = new ServiceListShim({
const instance = new IntrospectAndCompose({
serviceList: fixtures,
pollIntervalInMs: 100,
buildService(service) {
Expand All @@ -141,14 +141,14 @@ describe('ServiceListShim', () => {
});

const updateSpy = jest.fn();
const { cleanup } = await shim({
const { cleanup } = await instance({
update(supergraphSdl) {
updateSpy(supergraphSdl);
},
async healthCheck() {},
});

// let the shim poll through all the active mocks
// let the instance poll through all the active mocks
// wouldn't need to do this if I could get fakeTimers working as expected
while (nock.activeMocks().length > 0) {
await wait(0);
Expand Down Expand Up @@ -179,7 +179,7 @@ describe('ServiceListShim', () => {
// mock first update
mockAllServicesSdlQuerySuccess(fixturesWithUpdate);

const shim = new ServiceListShim({
const instance = new IntrospectAndCompose({
serviceList: fixtures,
pollIntervalInMs: 1000,
logger,
Expand All @@ -191,7 +191,7 @@ describe('ServiceListShim', () => {
throw new Error(thrownErrorMessage);
});

const { cleanup } = await shim({
const { cleanup } = await instance({
update: updateSpy,
async healthCheck() {},
});
Expand Down

0 comments on commit bef9604

Please sign in to comment.