Skip to content

Commit

Permalink
feat(context): allow user to create context ofor subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
bikov committed Sep 11, 2019
1 parent 5154755 commit 7e78143
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions src/server/graphql-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ export interface GraphQLServer {
stop(): void;
}

export type contextCreator = (ctx: Koa.Context) => object;
export type ContextCreator = (ctx: Koa.Context) => object;
export type SubscriptionContextCreator = (connection: any) => object;

@injectable()
export class PolarisGraphQLServer implements GraphQLServer {
server: ApolloServer;
private app: Koa;
private polarisProperties: PolarisProperties;
private customContexts: contextCreator[] = [];
private customContexts: ContextCreator[] = [];
private customSubscriptionContexts: SubscriptionContextCreator[] = [];
private httpServer?: http.Server;

constructor(
Expand All @@ -46,7 +48,7 @@ export class PolarisGraphQLServer implements GraphQLServer {
private realitiesHolderValidator: RealitiesHolderValidator,
@inject(POLARIS_TYPES.SoftDeleteConfiguration)
private softDeleteConfiguration?: SoftDeleteConfiguration,
@inject(POLARIS_TYPES.ApolloConfig) @optional() private userApolloConfig: Config = {},
@inject(POLARIS_TYPES.ApolloConfig) @optional() private userApolloConfig?: Config,
) {
const executableSchemaWithMiddleware = applyMiddleware(
schema,
Expand All @@ -63,7 +65,7 @@ export class PolarisGraphQLServer implements GraphQLServer {
() => new IrrelevantEntitiesExtension(),
() => new ResponseHeadersExtension(),
],
...userApolloConfig,
...(userApolloConfig || {}),
};
this.server = new ApolloServer(config);
this.app = new Koa();
Expand All @@ -85,7 +87,7 @@ export class PolarisGraphQLServer implements GraphQLServer {
this.polarisLogger.info(
`🚀 Subscriptions ready at ws://localhost:${port}${
this.server.subscriptionsPath
}`,
}`,
);
resolve();
});
Expand All @@ -101,10 +103,14 @@ export class PolarisGraphQLServer implements GraphQLServer {
}
}

addContextCreator(...contextCreators: contextCreator[]) {
addContextCreator(...contextCreators: ContextCreator[]) {
this.customContexts.push(...contextCreators);
}

addSubscriptionContextCreator(...contextCreators: SubscriptionContextCreator[]) {
this.customSubscriptionContexts.push(...contextCreators);
}

private formatResponse(response: any) {
if (response.data && !response.data.__schema && !response.data.__type) {
this.polarisLogger.info(`Finished response, answer is ${JSON.stringify(response)}`);
Expand Down Expand Up @@ -132,7 +138,7 @@ export class PolarisGraphQLServer implements GraphQLServer {
this.realitiesHolderValidator.validateRealitySupport(context);
return context;
} else {
return { body: {}, pubSub: new PubSub() } as any;
return this.getSubscriptionContext(connection);
}
} catch (e) {
this.polarisLogger.error('Context error', {
Expand All @@ -157,7 +163,23 @@ export class PolarisGraphQLServer implements GraphQLServer {
return context;
}

private getCustomContext(ctx: Koa.Context): object[] {
return this.customContexts.map(creator => creator(ctx));
private getSubscriptionContext(connection: any): PolarisContext {
return {
body: {},
pubSub: new PubSub(),
...this.getCustomSubscriptionContext(connection),
} as any;
}

private getCustomContext(ctx: Koa.Context): object {
return this.customContexts.reduce((previousValue, customContext) => ({
...customContext(ctx), ...previousValue,
}), {});
}

private getCustomSubscriptionContext(connection: any): object {
return this.customSubscriptionContexts.reduce((previousValue, customContext) => ({
...customContext(connection), ...previousValue,
}), {});
}
}

0 comments on commit 7e78143

Please sign in to comment.