Skip to content

Commit

Permalink
Expose koa middleware via server.getMiddleware() function
Browse files Browse the repository at this point in the history
  • Loading branch information
Giancarlo Anemone committed Jun 17, 2019
1 parent c883224 commit 4eef4be
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
2 changes: 2 additions & 0 deletions packages/apollo-server-koa/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const server = new ApolloServer({ typeDefs, resolvers });

const app = new Koa();
server.applyMiddleware({ app });
// alternatively you can get a composed middleware from the apollo server
// app.use(server.getMiddleware());

app.listen({ port: 4000 }, () =>
console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`),
Expand Down
37 changes: 23 additions & 14 deletions packages/apollo-server-koa/src/ApolloServer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Koa from 'koa';
import Koa, { Middleware } from 'koa';
import corsMiddleware from '@koa/cors';
import bodyParser from 'koa-bodyparser';
import compose from 'koa-compose';
Expand All @@ -19,15 +19,18 @@ import { graphqlKoa } from './koaApollo';
export { GraphQLOptions, GraphQLExtension } from 'apollo-server-core';
import { GraphQLOptions, FileUploadOptions } from 'apollo-server-core';

export interface ServerRegistration {
app: Koa;
export interface GetMiddlewareOptions {
path?: string;
cors?: corsMiddleware.Options | boolean;
bodyParserConfig?: bodyParser.Options | boolean;
onHealthCheck?: (ctx: Koa.Context) => Promise<any>;
disableHealthCheck?: boolean;
}

export interface ServerRegistration extends GetMiddlewareOptions {
app: Koa;
}

const fileUploadMiddleware = (
uploadsConfig: FileUploadOptions,
server: ApolloServerBase,
Expand Down Expand Up @@ -80,18 +83,22 @@ export class ApolloServer extends ApolloServerBase {
return true;
}

public applyMiddleware({ app, ...rest }: ServerRegistration) {
const middleware = this.getMiddleware(rest);
app.use(middleware);
}

// TODO: While Koa is Promise-aware, this API hasn't been historically, even
// though other integration's (e.g. Hapi) implementations of this method
// are `async`. Therefore, this should become `async` in a major release in
// order to align the API with other integrations.
public applyMiddleware({
app,
public getMiddleware({
path,
cors,
bodyParserConfig,
disableHealthCheck,
onHealthCheck,
}: ServerRegistration) {
}: GetMiddlewareOptions): Middleware {
if (!path) path = '/graphql';

// Despite the fact that this `applyMiddleware` function is `async` in
Expand All @@ -107,15 +114,16 @@ export class ApolloServer extends ApolloServerBase {
// work has finished. Any errors will be surfaced to Koa through its own
// native Promise-catching facilities.
const promiseWillStart = this.willStart();
app.use(
const middlewares = [];
middlewares.push(
middlewareFromPath(path, async (_ctx: Koa.Context, next: Function) => {
await promiseWillStart;
return next();
}),
);

if (!disableHealthCheck) {
app.use(
middlewares.push(
middlewareFromPath(
'/.well-known/apollo/server-health',
(ctx: Koa.Context) => {
Expand Down Expand Up @@ -147,22 +155,22 @@ export class ApolloServer extends ApolloServerBase {
this.graphqlPath = path;

if (cors === true) {
app.use(middlewareFromPath(path, corsMiddleware()));
middlewares.push(middlewareFromPath(path, corsMiddleware()));
} else if (cors !== false) {
app.use(middlewareFromPath(path, corsMiddleware(cors)));
middlewares.push(middlewareFromPath(path, corsMiddleware(cors)));
}

if (bodyParserConfig === true) {
app.use(middlewareFromPath(path, bodyParser()));
middlewares.push(middlewareFromPath(path, bodyParser()));
} else if (bodyParserConfig !== false) {
app.use(middlewareFromPath(path, bodyParser(bodyParserConfig)));
middlewares.push(middlewareFromPath(path, bodyParser(bodyParserConfig)));
}

if (uploadsMiddleware) {
app.use(middlewareFromPath(path, uploadsMiddleware));
middlewares.push(middlewareFromPath(path, uploadsMiddleware));
}

app.use(
middlewares.push(
middlewareFromPath(path, (ctx: Koa.Context, next: Function) => {
if (ctx.request.method === 'OPTIONS') {
ctx.status = 204;
Expand Down Expand Up @@ -199,6 +207,7 @@ export class ApolloServer extends ApolloServerBase {
})(ctx, next);
}),
);
return compose(middlewares);
}
}

Expand Down

0 comments on commit 4eef4be

Please sign in to comment.