Skip to content

Commit

Permalink
Add new method onSchemaLoadOrUpdate() to replace onSchemaChange(), an…
Browse files Browse the repository at this point in the history
…d revert addition of optional argument and behavior change to loadStatic() for onSchemaChange()
  • Loading branch information
sachindshinde committed May 20, 2021
1 parent 1548498 commit e32bd44
Showing 1 changed file with 55 additions and 15 deletions.
70 changes: 55 additions & 15 deletions gateway-js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,12 @@ export class ApolloGateway implements GraphQLService {
private logger: Logger;
private queryPlanStore: InMemoryLRUCache<QueryPlan>;
private apolloConfig?: ApolloConfig;
private onSchemaChangeListeners = new Set<
(schema: GraphQLSchema, supergraphSdl: string) => void
private onSchemaChangeListeners = new Set<(schema: GraphQLSchema) => void>();
private onSchemaLoadOrUpdateListeners = new Set<
(schemaContext: {
apiSchema: GraphQLSchema;
coreSupergraphSdl: string;
}) => void
>();
private serviceDefinitions: ServiceDefinition[] = [];
private compositionMetadata?: CompositionMetadata;
Expand Down Expand Up @@ -397,7 +401,7 @@ export class ApolloGateway implements GraphQLService {

// TODO(trevor): #580 redundant parse
this.parsedSupergraphSdl = parse(supergraphSdl);
this.updateWithSchemaAndNotify(schema, supergraphSdl);
this.updateWithSchemaAndNotify(schema, supergraphSdl, true);
this.state = { phase: 'loaded' };
}

Expand Down Expand Up @@ -561,22 +565,42 @@ export class ApolloGateway implements GraphQLService {
private updateWithSchemaAndNotify(
schema: GraphQLSchema,
supergraphSdl: string,
// Once we remove the deprecated onSchemaChange() method, we can remove this.
legacyDontNotifyOnSchemaChangeListeners: boolean = false,
): void {
this.schema = schema;
this.queryPlanner = new QueryPlanner(schema);

// Notify the schema listeners of the updated schema
try {
this.onSchemaChangeListeners.forEach((listener) =>
listener(schema, supergraphSdl),
);
} catch (e) {
this.logger.error(
"An error was thrown from an 'onSchemaChange' listener. " +
'The schema will still update: ' +
((e && e.message) || e),
);
// Notify onSchemaChange listeners of the updated schema
if (!legacyDontNotifyOnSchemaChangeListeners) {
this.onSchemaChangeListeners.forEach((listener) => {
try {
listener(schema);
} catch (e) {
this.logger.error(
"An error was thrown from an 'onSchemaChange' listener. " +
'The schema will still update: ' +
((e && e.message) || e),
);
}
});
}

// Notify onSchemaLoadOrUpdate listeners of the updated schema
this.onSchemaLoadOrUpdateListeners.forEach((listener) => {
try {
listener({
apiSchema: schema,
coreSupergraphSdl: supergraphSdl,
});
} catch (e) {
this.logger.error(
"An error was thrown from an 'onSchemaLoadOrUpdate' listener. " +
'The schema will still update: ' +
((e && e.message) || e),
);
}
});
}

private async maybePerformServiceHealthCheck(update: CompositionUpdate) {
Expand Down Expand Up @@ -718,8 +742,11 @@ export class ApolloGateway implements GraphQLService {
};
}

/**
* @deprecated Please use `onSchemaLoadOrUpdate` instead.
*/
public onSchemaChange(
callback: (schema: GraphQLSchema, supergraphSdl: string) => void,
callback: (schema: GraphQLSchema) => void,
): Unsubscriber {
this.onSchemaChangeListeners.add(callback);

Expand All @@ -728,6 +755,19 @@ export class ApolloGateway implements GraphQLService {
};
}

public onSchemaLoadOrUpdate(
callback: (schemaContext: {
apiSchema: GraphQLSchema;
coreSupergraphSdl: string;
}) => void,
): Unsubscriber {
this.onSchemaLoadOrUpdateListeners.add(callback);

return () => {
this.onSchemaLoadOrUpdateListeners.delete(callback);
};
}

// This function waits an appropriate amount, updates composition, and calls itself
// again. Note that it is an async function whose Promise is not actually awaited;
// it should never throw itself other than due to a bug in its state machine.
Expand Down

0 comments on commit e32bd44

Please sign in to comment.