diff --git a/README.md b/README.md index 82b29701f2d..fb36f2e7eb6 100644 --- a/README.md +++ b/README.md @@ -132,10 +132,11 @@ server.start((err) => { }); ``` -### Koa +### Koa ( POST ) ```js import koa from 'koa'; import koaRouter from 'koa-router'; +import * as koaBody from 'koa-bodyparser'; import { apolloKoa } from 'apollo-server'; const app = new koa(); @@ -150,6 +151,22 @@ app.use(router.allowedMethods()); app.listen(PORT); ``` +### Koa ( GET ) +```js +import koa from 'koa'; +import koaRouter from 'koa-router'; +import { apolloKoa } from 'apollo-server'; + +const app = new koa(); +const router = new koaRouter(); +const PORT = 3000; + +router.get('/graphql', apolloKoa({ schema: myGraphQLSchema })); +app.use(router.routes()); +app.use(router.allowedMethods()); +app.listen(PORT); +``` + ## Options Apollo Server can be configured with an options object with the the following fields: diff --git a/src/integrations/integrations.test.ts b/src/integrations/integrations.test.ts index 79633a40740..b77b30c5296 100644 --- a/src/integrations/integrations.test.ts +++ b/src/integrations/integrations.test.ts @@ -161,7 +161,7 @@ export default (createApp: CreateAppFunc, destroyApp?: DestroyAppFunc) => { .head('/graphql') .send(); return req.then((res) => { - expect(res.status).to.be.oneOf([404, 405]); + expect(res.status).to.equal(405); // Hapi doesn't return allow header, so we can't test this. // return expect(res.headers['allow']).to.equal('POST'); }); diff --git a/src/integrations/koaApollo.test.ts b/src/integrations/koaApollo.test.ts index af7953e03a6..5802bfe009b 100644 --- a/src/integrations/koaApollo.test.ts +++ b/src/integrations/koaApollo.test.ts @@ -20,6 +20,7 @@ function createApp(options: CreateAppOptions = {}) { if (options.graphiqlOptions ) { router.get('/graphiql', graphiqlKoa( options.graphiqlOptions )); } + router.get('/graphql', apolloKoa( options.apolloOptions )); router.post('/graphql', apolloKoa( options.apolloOptions )); app.use(router.routes()); app.use(router.allowedMethods()); diff --git a/src/integrations/koaApollo.ts b/src/integrations/koaApollo.ts index 2f42ecec42a..5675249024a 100644 --- a/src/integrations/koaApollo.ts +++ b/src/integrations/koaApollo.ts @@ -35,21 +35,38 @@ export function apolloKoa(options: ApolloOptions | KoaApolloOptionsFunction): Ko } const formatErrorFn = optionsObject.formatError || graphql.formatError; + let buffer; - if (!ctx.request.body) { - ctx.status = 500; - return ctx.body = 'POST body missing. Did you forget "app.use(koaBody())"?'; + switch ( ctx.request.method ) { + case 'GET': + if (!ctx.request.query) { + ctx.status = 500; + return ctx.body = 'GET query missing'; + } + + buffer = ctx.request.query; + break; + case 'POST': + if (!ctx.request.body) { + ctx.status = 500; + return ctx.body = 'POST body missing. Did you forget "app.use(koaBody())"?'; + } + + buffer = ctx.request.body; + break; + default: + ctx.status = 405; + return ctx.body = 'Apollo Server supports only GET/POST requests.'; } - let b = ctx.request.body; let isBatch = true; - if (!Array.isArray(b)) { + if (!Array.isArray(buffer)) { isBatch = false; - b = [b]; + buffer = [buffer]; } let responses: Array = []; - for (let requestParams of b) { + for (let requestParams of buffer) { try { const query = requestParams.query; const operationName = requestParams.operationName;