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

Expose composed middleware via getMiddleware(). #2435

Merged
merged 28 commits into from
Jun 30, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
46dd9d3
Expose koa middleware via server.getMiddleware() function
Mar 12, 2019
824e7b0
Update changelog
Mar 12, 2019
1e56d56
Merge branch 'master' into master
ganemone Mar 12, 2019
bd6f5bd
Merge branch 'master' into master
ganemone Mar 13, 2019
dc3be30
Merge branch 'master' into master
ganemone Mar 13, 2019
ea1722e
Merge branch 'master' into master
ganemone Mar 14, 2019
2e053bc
Merge branch 'master' into master
ganemone Mar 15, 2019
158ccca
Merge branch 'master' into master
ganemone Mar 18, 2019
f571731
Merge branch 'master' into master
ganemone Mar 20, 2019
5f49ed2
Merge branch 'master' into master
ganemone Apr 2, 2019
a557937
Merge branch 'master' into master
ganemone Apr 8, 2019
50f7ba4
savepoint
abernix Apr 10, 2019
f2d9c04
savepoint: req required.
abernix Apr 10, 2019
5582308
express
abernix Apr 12, 2019
5cca1e2
koa
abernix Apr 12, 2019
bfde5d7
Add getMiddleware functions for apollo-server-express and apollo-serv…
Jun 28, 2019
9d8e170
Update linting and deps
Jun 28, 2019
ed13205
Update lockfile
Jun 28, 2019
e248a03
Revert "Update lockfile"
Jun 28, 2019
ab6e2d3
Revert "Revert "Update lockfile""
Jun 28, 2019
7ea0d53
Update lockfile
Jun 28, 2019
027b002
Merge branch 'master' into master
Jun 28, 2019
ba740af
Adjust install location of `parseurl` to be within `apollo-server-exp…
abernix Jun 30, 2019
240c02b
docs: Adjust formatting of `applyMiddleware` header to be in gave acc…
abernix Jun 30, 2019
4c8c3fb
docs: Add documentation for `getMiddleware`.
abernix Jun 30, 2019
254971e
Merge remote-tracking branch 'origin/master' into HEAD
abernix Jun 30, 2019
e68cf95
Merge branch 'release-2.7.0' into HEAD
abernix Jun 30, 2019
c3a23e7
Add CHANGELOG.md for #2435.
abernix Jun 30, 2019
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

### vNEXT

### v2.4.9

- `apollo-server-koa`: Expose `server.getMiddleware()` function for getting access to koa middleware directly. [PR #2435](https://github.com/apollographql/apollo-server/pull/2435)

### v2.4.8

- No functional changes in this version. The patch version has been bumped to fix the `README.md` displayed on the [npm package for `apollo-server`](https://npm.im/apollo-server) as a result of a broken publish. Apologies for the additional noise!
Expand Down
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