Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add polling support #2914

Merged
merged 1 commit into from
Jun 27, 2019
Merged
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
46 changes: 45 additions & 1 deletion packages/apollo-gateway/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,18 @@ function isLocalConfig(config: GatewayConfig): config is LocalGatewayConfig {
return 'localServiceList' in config;
}

export type SchemaChangeCallback = (schema: GraphQLSchema) => void;
export type Unsubscriber = () => void;

export class ApolloGateway implements GraphQLService {
public schema?: GraphQLSchema;
public isReady: boolean = false;
protected serviceMap: ServiceMap = Object.create(null);
protected config: GatewayConfig;
protected logger: Logger;
protected queryPlanStore?: InMemoryLRUCache<QueryPlan>;
private pollingTimer?: NodeJS.Timer;
private onSchemaChangeListeners = new Set<SchemaChangeCallback>();

constructor(config: GatewayConfig) {
this.config = {
Expand Down Expand Up @@ -124,11 +129,43 @@ export class ApolloGateway implements GraphQLService {
this.isReady = true;
}

public onSchemaChange(value: SchemaChangeCallback): Unsubscriber {
// TODO: if (!isRemoteGatewayConfig(this.config)) { throw new Error('onSchemaChange requires an Apollo Engine hosted service list definition.'); } (dependant on #2915)
this.onSchemaChangeListeners.add(value);
if (!this.pollingTimer) this.startPollingServices();

return () => {
this.onSchemaChangeListeners.delete(value);
if (this.onSchemaChangeListeners.size === 0 && this.pollingTimer) {
clearInterval(this.pollingTimer!);
this.pollingTimer = undefined;
}
};
}

private startPollingServices() {
if (this.pollingTimer) clearInterval(this.pollingTimer);

this.pollingTimer = setInterval(async () => {
const [services, isNewSchema] = await this.loadServiceDefinitions(
this.config,
);
if (!isNewSchema) {
this.logger.debug('No changes to gateway config');
return;
}
if (this.queryPlanStore) this.queryPlanStore.flush();
this.logger.debug('Gateway config has changed, updating schema');
this.createSchema(services);
this.onSchemaChangeListeners.forEach(listener => listener(this.schema!));
}, 10 * 1000);
}

protected createServices(services: ServiceEndpointDefinition[]) {
for (const serviceDef of services) {
if (!serviceDef.url && !isLocalConfig(this.config)) {
throw new Error(
`Service defintion for service ${serviceDef.name} is missing a url`,
`Service definition for service ${serviceDef.name} is missing a url`,
);
}
this.serviceMap[serviceDef.name] = this.config.buildService
Expand Down Expand Up @@ -234,6 +271,13 @@ export class ApolloGateway implements GraphQLService {
sizeCalculator: approximateObjectSize,
});
}

public async stop() {
if (this.pollingTimer) {
clearInterval(this.pollingTimer);
this.pollingTimer = undefined;
}
}
}

function approximateObjectSize<T>(obj: T): number {
Expand Down