Skip to content
Open
Show file tree
Hide file tree
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
20 changes: 20 additions & 0 deletions src/__tests__/koa.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ApolloServer } from '@apollo/server';
import koa from 'koa';
import bodyParser from 'koa-bodyparser';
import request from 'supertest';
import { it, expect } from '@jest/globals';
import { koaMiddleware } from '..';
Expand All @@ -19,6 +20,25 @@ it('gives helpful error if body-parser middleware is not installed', async () =>
await server.stop();
});

it('calls middlewares defined after it', async () => {
const server = new ApolloServer({ typeDefs: 'type Query {f: ID}' });
const app = new koa();
const spy = jest.fn();

await server.start();

app.use(bodyParser());
app.use(koaMiddleware(server));
app.use((_, next) => {
spy();
return next();
});

await request(app.callback()).post('/').send({ query: '{f}' }).expect(200);
expect(spy).toBeCalled();
await server.stop();
});

it('not calling start causes a clear error', async () => {
const server = new ApolloServer({ typeDefs: 'type Query {f: ID}' });
expect(() => koaMiddleware(server)).toThrow(
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export function koaMiddleware<
TContext
> = options?.context ?? defaultContext;

return async (ctx) => {
return async (ctx, next) => {
if (!ctx.request.body) {
// The json koa-bodyparser *always* sets ctx.request.body to {} if it's unset (even
// if the Content-Type doesn't match), so if it isn't set, you probably
Expand Down Expand Up @@ -136,5 +136,7 @@ export function koaMiddleware<
for (const [key, value] of headers) {
ctx.set(key, value);
}

return next();
};
}