-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(express): add Express middleware
- Loading branch information
Showing
7 changed files
with
73 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var config = {}; | ||
|
||
module.exports = config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('./'); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters