Skip to content

Commit

Permalink
feat(hapi): add Hapi plugin
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 cdecd28 commit fac1791
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 4 deletions.
26 changes: 26 additions & 0 deletions example/hapi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var Hapi = require('hapi');
var graffiti = require('../');
var graffitiMongoose = require('@risingstack/graffiti-mongoose');

var server = new Hapi.Server();
server.connection({ port: 3000 });

server.register({
register: graffiti.hapi,
options: {
adapter: graffitiMongoose,
models: []
}
}, {
routes: {
prefix: '/graphql'
}
}, function (err) {
if (err) {
console.error('Failed to load plugin:', err);
}

server.start(function () {
console.log('Server running at:', server.info.uri);
});
});
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"chai-subset": "^1.0.1",
"eslint": "1.0.0-rc-3",
"express": "^4.13.1",
"hapi": "^8.8.0",
"koa": "^0.21.0",
"mocha": "^2.2.5",
"pre-commit": "^1.0.10",
Expand All @@ -34,6 +35,7 @@
"test"
],
"dependencies": {
"boom": "^2.8.0",
"graphql": "^0.1.8"
}
}
1 change: 0 additions & 1 deletion src/express/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var config = require('../config');
var checkDep = require('../util').checkDep;

function isGet(request) {
Expand Down
41 changes: 39 additions & 2 deletions src/hapi/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,45 @@
function create() {
var checkDep = require('../util').checkDep;
var boom = require('boom');

return function hapi() {
function create(options) {

var graphql = checkDep(options, 'graphql');

var plugin = {
register: function(server, options, next) {

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

var schema = adapter.getSchema(models);

server.route({
method: 'GET',
path: '/',
handler: function(request, reply) {

var query = request.query.q;

return graphql(schema, query)
.then(function(result) {
reply(result);
})
.catch(function(err) {
reply(boom.badImplementation(err));
});
}
});

next();
}
};

plugin.register.attributes = {
name: 'graffiti-hapi',
version: '1.0.0'
};

return plugin;
}

module.exports.create = create;
1 change: 0 additions & 1 deletion src/koa/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var config = require('../config');
var checkDep = require('../util').checkDep;

function isGet(request) {
Expand Down

0 comments on commit fac1791

Please sign in to comment.