Skip to content

Commit

Permalink
feat(express): add Express middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
gergelyke authored and Peter Marton committed Jul 27, 2015
1 parent 2cfb447 commit 80b5ddc
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 6 deletions.
19 changes: 19 additions & 0 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var express = require('express');
var graffiti = require('../');
var graffitiMongoose = require('@risingstack/graffiti-mongoose');

var app = express();

app.use(graffiti.express({
prefix: '/graphql',
adapter: graffitiMongoose,
models: []
}));

app.listen(3000, function (err) {
if (err) {
throw err
};

console.log('server is listening');
});
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@
"devDependencies": {
"chai": "^3.2.0",
"eslint": "1.0.0-rc-3",
"express": "^4.13.1",
"mocha": "^2.2.5",
"pre-commit": "^1.0.10"
},
"pre-commit": [
"eslint",
"test"
]
],
"dependencies": {
"graphql": "^0.1.8"
}
}
3 changes: 3 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var config = {};

module.exports = config;
36 changes: 35 additions & 1 deletion src/express/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
function express() {
var config = require('../config');
var graphql = require('graphql').graphql;
var checkDep = require('../util').checkDep;

function isGet(request) {
return request.method === 'GET';
}

function isPrefixed(request, prefix) {
return request.path.indexOf(prefix) === 0;
}

function express(options) {

var models = checkDep(options, 'models');
var adapter = checkDep(options, 'adapter');
var prefix = checkDep(options, 'prefix');

var schema = adapter.getSchema(models);

return function(request, respone, next) {

var query = request.query.q;

if (isGet(request) && isPrefixed(request, prefix)) {
return graphql(schema, query)
.then(function(result) {
respone.json(result);
})
.catch(function(err) {
next(err);
});
}

return next();
};
}

module.exports = express;
7 changes: 7 additions & 0 deletions src/express/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var expect = require('chai').expect;

describe('graffiti express', function() {

var express = require('./');

});
4 changes: 2 additions & 2 deletions src/util/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
function checkDep (obj, propName) {
function checkDep(obj, propName) {
if (!obj) {
throw new Error('Object cannot be undefined');
}

if(!obj[propName]) {
if (!obj[propName]) {
throw new Error(propName + ' cannot be undefined');
}

Expand Down
4 changes: 2 additions & 2 deletions src/util/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('graffiti util', function() {
it('throws if first argument is undefined', function() {

try {
util.checkDep()
util.checkDep();
} catch (ex) {
expect(ex.message).to.eql('Object cannot be undefined');
return;
Expand All @@ -30,7 +30,7 @@ describe('graffiti util', function() {
throw new Error('Error should have been thrown');
});

it('returns the property', function () {
it('returns the property', function() {

var prop = util.checkDep({
colour: 'red'
Expand Down

0 comments on commit 80b5ddc

Please sign in to comment.