Skip to content

Commit

Permalink
chore(get-support): support for GET method for koa
Browse files Browse the repository at this point in the history
  • Loading branch information
DxCx committed Oct 20, 2016
1 parent dd7fa7f commit 956540a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/integrations/integrations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
Expand Down
1 change: 1 addition & 0 deletions src/integrations/koaApollo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
31 changes: 24 additions & 7 deletions src/integrations/koaApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<graphql.GraphQLResult> = [];
for (let requestParams of b) {
for (let requestParams of buffer) {
try {
const query = requestParams.query;
const operationName = requestParams.operationName;
Expand Down

0 comments on commit 956540a

Please sign in to comment.