diff --git a/example/index.js b/example/index.js new file mode 100644 index 0000000..91f2e9e --- /dev/null +++ b/example/index.js @@ -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'); +}); diff --git a/package.json b/package.json index bb4bfbb..ed083d0 100644 --- a/package.json +++ b/package.json @@ -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" + } } diff --git a/src/config.js b/src/config.js new file mode 100644 index 0000000..7b0dfa1 --- /dev/null +++ b/src/config.js @@ -0,0 +1,3 @@ +var config = {}; + +module.exports = config; diff --git a/src/express/index.js b/src/express/index.js index e24a742..1ea3406 100644 --- a/src/express/index.js +++ b/src/express/index.js @@ -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; diff --git a/src/express/index.spec.js b/src/express/index.spec.js new file mode 100644 index 0000000..01a43f7 --- /dev/null +++ b/src/express/index.spec.js @@ -0,0 +1,7 @@ +var expect = require('chai').expect; + +describe('graffiti express', function() { + + var express = require('./'); + +}); diff --git a/src/util/index.js b/src/util/index.js index 600a0b0..1f1258b 100644 --- a/src/util/index.js +++ b/src/util/index.js @@ -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'); } diff --git a/src/util/index.spec.js b/src/util/index.spec.js index 409dc66..4a40592 100644 --- a/src/util/index.spec.js +++ b/src/util/index.spec.js @@ -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; @@ -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'