Skip to content

Commit

Permalink
feat(package): don't include body parsers in the middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
Andras Toth committed Feb 2, 2016
1 parent 1cf46c5 commit 57f39e6
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 60 deletions.
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ For more info check out RisingStack's [GraphQL tutorial](https://blog.risingstac

## Example server and queries

For a running **example server** and **executable queries**, check out our example repository and play with your GraphQL queries: [graffiti-example](https://github.com/RisingStack/graffiti-mongoose/tree/master/example)
For a running **example server** and **executable queries**, check out our example repository and play with your GraphQL queries: [graffiti-example](https://github.com/RisingStack/graffiti/tree/master/example)

## Adapters

Expand All @@ -42,6 +42,7 @@ npm install @risingstack/graffiti --save

## Usage

0. run MongoDB
1. register the middleware
2. provide a schema (returned by an adapters `getSchema` method or your own `GraphQLSchema` instance)
3. the GraphQL endpoint is available on `/graphql`
Expand All @@ -50,13 +51,18 @@ npm install @risingstack/graffiti --save

```javascript
import express from 'express';
import { json } from 'body-parser';
import graffiti from '@risingstack/graffiti';
import {getSchema} from '@risingstack/graffiti-mongoose';
import { getSchema } from '@risingstack/graffiti-mongoose';

import Cat from './models/Cat';
import User from './models/User';

const app = express();

// parse body as json
app.use(json());

app.use(graffiti.express({
schema: getSchema([User, Cat])
}));
Expand All @@ -67,9 +73,9 @@ app.listen(3000);
### Hapi

```javascript
import {Server} from 'hapi';
import { Server } from 'hapi';
import graffiti from '@risingstack/graffiti';
import {getSchema} from '@risingstack/graffiti-mongoose';
import { getSchema } from '@risingstack/graffiti-mongoose';

const server = new Server();
server.connection({ port: 3000 });
Expand All @@ -94,13 +100,17 @@ server.register({

```javascript
import koa from 'koa';
import parser from 'koa-bodyparser';
import graffiti from '@risingstack/graffiti';
import {getSchema} from '@risingstack/graffiti-mongoose';
import { getSchema } from '@risingstack/graffiti-mongoose';

import Cat from './models/Cat';
import User from './models/User';

const app = koa();

app.use(parser());

app.use(graffiti.koa({
schema: getSchema([User, Cat])
}));
Expand Down
3 changes: 2 additions & 1 deletion example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
### Run

```bash
# MongoDB has to be running
# Install dependencies
cd .. && npm install && cd example && npm install
# Run all servers
node .
npm start
# Express server listening on port 3001
# Hapi server listening on port 3002
# Koa server listening on port 3003
Expand Down
10 changes: 10 additions & 0 deletions example/express.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import express from 'express';
import { json } from 'body-parser';
import graffiti from '../';
import schema from './schema';

const app = express();

app.use(json());

app.use(graffiti.express({
schema
}));

// redirect all requests to /graphql
// to open GraphiQL by default
app.use((req, res) => {
res.redirect('/graphql');
});

app.listen(3001, (err) => {
if (err) {
throw err;
Expand Down
14 changes: 12 additions & 2 deletions example/hapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@ import graffiti from '../';
import schema from './schema';

const server = new Server();
server.connection({ port: 3002 });
server.connection({ port: 3003 });

// redirect all requests to /graphql
// to open GraphiQL by default
server.ext('onRequest', function redirect(request, reply) {
if (request.path === '/graphql') {
return reply.continue();
}
reply.redirect('/graphql');
});

server.register({
register: graffiti.hapi,
options: {
Expand All @@ -15,6 +25,6 @@ server.register({
}

server.start(() => {
console.log('Hapi server is listening on port 3002');
console.log('Hapi server is listening on port 3003');
});
});
2 changes: 1 addition & 1 deletion example/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require('babel-register');
require('babel-polyfill');
require('./express');
require('./hapi');
require('./koa');
require('./hapi');
14 changes: 12 additions & 2 deletions example/koa.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import koa from 'koa';
import parser from 'koa-bodyparser';
import graffiti from '../';
import schema from './schema';

const app = koa();

app.use(parser());

app.use(graffiti.koa({
schema
}));

app.listen(3003, (err) => {
// redirect all requests to /graphql
// to open GraphiQL by default
app.use(function *redirect() {
this.redirect('/graphql');
});

app.listen(3002, (err) => {
if (err) {
throw err;
}

console.log('Koa server is listening on port 3003');
console.log('Koa server is listening on port 3002');
});
10 changes: 6 additions & 4 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
},
"dependencies": {
"@risingstack/graffiti": "../",
"@risingstack/graffiti-mongoose": "^5.0.3",
"express": "^4.13.3",
"@risingstack/graffiti-mongoose": "^5.0.5",
"body-parser": "^1.14.2",
"express": "^4.13.4",
"koa": "^1.1.2",
"koa-static": "^1.5.2",
"mongoose": "^4.3.4"
"koa-bodyparser": "^2.0.1",
"koa-static": "^2.0.0",
"mongoose": "^4.3.7"
}
}
17 changes: 8 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,30 @@
},
"homepage": "https://github.com/RisingStack/graffiti#readme",
"dependencies": {
"boom": "^3.1.1",
"co-body": "^4.0.0"
"boom": "^3.1.2"
},
"peerDependencies": {
"graphql": "^0.4.14"
},
"devDependencies": {
"babel": "^6.3.26",
"babel-cli": "^6.4.0",
"babel-cli": "^6.4.5",
"babel-eslint": "^5.0.0-beta6",
"babel-polyfill": "^6.3.14",
"babel-preset-es2015": "^6.3.13",
"babel-preset-stage-0": "^6.3.13",
"babel-register": "^6.4.3",
"chai": "^3.4.1",
"chai": "^3.5.0",
"chai-subset": "^1.2.0",
"eslint": "1.10.3",
"eslint-config-airbnb": "^3.1.0",
"express": "^4.13.3",
"eslint-config-airbnb": "^4.0.0",
"express": "^4.13.4",
"graphql": "^0.4.14",
"hapi": "^12.1.0",
"hapi": "^13.0.0",
"koa": "^2.0.0-alpha.2",
"mocha": "^2.3.4",
"mocha": "^2.4.5",
"pre-commit": "^1.1.2",
"sinon": "^1.17.2",
"sinon": "^1.17.3",
"sinon-chai": "^2.8.0"
},
"pre-commit": [
Expand Down
42 changes: 18 additions & 24 deletions src/express/express.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { graphql } from 'graphql';
import { json } from 'co-body';
import {
badRequest,
methodNotAllowed
Expand All @@ -20,33 +19,28 @@ function sendError(response, boom) {
export default function middleware({ graphiql = true, schema = required() } = {}) {
return (request, response, next) => {
if (isPath(request) && (isPost(request) || isGet(request))) {
let promise = Promise.resolve(request.body);
if (!(request.body instanceof Object)) {
promise = json(request);
}
return promise.then((body) => {
const { query, variables } = Object.assign({}, body, request.query);
const body = request.body;
const { query, variables } = Object.assign({}, body, request.query);

if (isGet(request) && request.accepts('html') && graphiql) {
return response.send(renderGraphiQL({ query, variables }));
}
if (isGet(request) && request.accepts('html') && graphiql) {
return response.send(renderGraphiQL({ query, variables }));
}

if (isGet(request) && query && query.includes('mutation')) {
const boom = methodNotAllowed('GraphQL mutation only allowed in POST request.');
return sendError(response, boom);
}
if (isGet(request) && query && query.includes('mutation')) {
const boom = methodNotAllowed('GraphQL mutation only allowed in POST request.');
return sendError(response, boom);
}

return graphql(schema, query, request, variables)
.then((result) => {
if (result.errors) {
const message = result.errors.map((error) => error.message).join('\n');
const boom = badRequest(message);
return sendError(response, boom);
}
return graphql(schema, query, request, variables)
.then((result) => {
if (result.errors) {
const message = result.errors.map((error) => error.message).join('\n');
const boom = badRequest(message);
return sendError(response, boom);
}

response.json(result);
});
});
response.json(result);
});
}

return next();
Expand Down
5 changes: 0 additions & 5 deletions src/express/express.spec.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { expect } from 'chai';
import parser from 'co-body';
import express from './';

describe('graffiti express', () => {
const mwFactory = express;

beforeEach(function stub() {
this.sandbox.stub(parser, 'json', (request) => Promise.resolve(request.body));
});

describe('checks for required options', () => {
it('should throw an error if not all met', () => {
// schema is missing
Expand Down
3 changes: 1 addition & 2 deletions src/koa/koa.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { graphql } from 'graphql';
import { json } from 'co-body';
import {
required,
isPath,
Expand All @@ -15,7 +14,7 @@ function accepts(type) {
export default function middleware({ graphiql = true, schema = required() } = {}) {
return function *middleware(next) {
if (isPath(this) && (isPost(this) || isGet(this))) {
const body = yield json(this);
const body = this.body;
const { query, variables } = Object.assign({}, body, this.query);

if (isGet(this) && accepts.call(this, 'html') && graphiql) {
Expand Down
5 changes: 0 additions & 5 deletions src/koa/koa.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { expect } from 'chai';
import parser from 'co-body';
import koa from './';

describe('graffiti koa', () => {
beforeEach(function stub() {
this.sandbox.stub(parser, 'json', (request) => Promise.resolve(request.payload || request.body));
});

describe('checks for required options', () => {
it('should throw an error if not all met', () => {
const mwFactory = koa;
Expand Down

0 comments on commit 57f39e6

Please sign in to comment.