-
-
Notifications
You must be signed in to change notification settings - Fork 503
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add middlewares for express, koa, hapi (#28)
* chore: add middlewares for express, koa, hapi * chore: add middlewares for express, koa, hapi * chore: add middlewares for express, koa, hapi * chore: minor gramma changes after code review * chore: changes after code review
- Loading branch information
1 parent
ba35ed1
commit 9758c6d
Showing
14 changed files
with
354 additions
and
4 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 |
---|---|---|
|
@@ -8,3 +8,4 @@ stats.json | |
npm-debug.log | ||
|
||
lib | ||
/middleware/ |
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 |
---|---|---|
|
@@ -5,4 +5,4 @@ demo-dist | |
stats.json | ||
npm-debug.log | ||
|
||
build | ||
build |
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,16 @@ | ||
const express = require('express'); | ||
const graphqlHTTP = require('express-graphql'); | ||
const { express: voyagerMiddleware } = require('graphql-voyager/middleware'); | ||
const schema = require('../schema'); | ||
|
||
const app = express(); | ||
const PORT = 3001; | ||
|
||
app.use('/graphql', graphqlHTTP(() => ({ schema }))); | ||
app.use('/voyager', voyagerMiddleware({ endpointUrl: '/graphql' })); | ||
|
||
app.listen(PORT, function() { | ||
const port = this.address().port; | ||
|
||
console.log(`Started on http://localhost:${port}/`); | ||
}); |
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,39 @@ | ||
const Hapi = require('hapi'); | ||
const { graphqlHapi } = require('graphql-server-hapi'); | ||
const { hapi: voyagerMiddleware } = require('graphql-voyager/middleware'); | ||
const schema = require('../schema'); | ||
|
||
const server = new Hapi.Server(); | ||
|
||
server.connection({ | ||
port: 3001 | ||
}); | ||
|
||
server.register([ | ||
{ | ||
register: graphqlHapi, | ||
options: { | ||
path: '/graphql', | ||
graphqlOptions: { | ||
schema, | ||
}, | ||
route: { | ||
cors: true | ||
} | ||
} | ||
}, | ||
{ | ||
register: voyagerMiddleware, | ||
options: { | ||
path: '/voyager', | ||
route: { | ||
cors: true | ||
}, | ||
endpointUrl: '/graphql' | ||
}, | ||
} | ||
],() => { | ||
server.start(() => { | ||
console.log(`Started on ${server.info.uri}`); | ||
}) | ||
}); |
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,22 @@ | ||
const Koa = require('koa'); | ||
const KoaRouter = require('koa-router'); | ||
const graphqlHTTP = require('koa-graphql'); | ||
const { koa: voyagerMiddleware } = require('graphql-voyager/middleware'); | ||
const schema = require('../schema'); | ||
|
||
const app = new Koa(); | ||
const router = new KoaRouter(); | ||
const PORT = 3001; | ||
|
||
router.all('/graphql', graphqlHTTP({ schema })); | ||
router.all('/voyager', voyagerMiddleware({ | ||
endpointUrl: '/graphql' | ||
})); | ||
|
||
app.use(router.routes()); | ||
app.use(router.allowedMethods()); | ||
app.listen(PORT, function() { | ||
const port = this.address().port; | ||
|
||
console.log(`Started on http://localhost:${port}/`); | ||
}); |
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
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,28 @@ | ||
import { Request, Response } from 'express'; | ||
import renderVoyagerPage, { MiddlewareOptions } from './render-voyager-page'; | ||
|
||
const { version } = require('../package.json'); | ||
|
||
interface ExpressVoyagerMiddleware { | ||
(_req: Request, res: Response, next: () => void): void; | ||
} | ||
|
||
interface Register { | ||
(options): ExpressVoyagerMiddleware | ||
} | ||
|
||
const voyagerExpress: Register = function voyagerExpress(options) { | ||
const middlewareOptions: MiddlewareOptions = { | ||
...options, | ||
version | ||
}; | ||
|
||
return (_req, res, next) => { | ||
res.setHeader('Content-Type', 'text/html'); | ||
res.write(renderVoyagerPage(middlewareOptions)); | ||
res.end(); | ||
next(); | ||
} | ||
}; | ||
|
||
export default voyagerExpress; |
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,45 @@ | ||
import { Server } from 'hapi'; | ||
import renderVoyagerPage, { MiddlewareOptions } from './render-voyager-page'; | ||
|
||
const pkg = require('../package.json'); | ||
|
||
interface Register { | ||
(server: Server, options: any, next: any): void; | ||
attributes?: any; | ||
} | ||
|
||
const hapi: Register = function(server, options, next) { | ||
if (arguments.length !== 3) { | ||
throw new Error(`Voyager middleware expects exactly 3 arguments, got ${arguments.length}`); | ||
} | ||
|
||
const { | ||
path, | ||
route: config = {}, | ||
...voyagerOptions | ||
} = options; | ||
|
||
const middlewareOptions: MiddlewareOptions = { | ||
...voyagerOptions, | ||
version: pkg.version | ||
}; | ||
|
||
server.route({ | ||
method: 'GET', | ||
path, | ||
config, | ||
handler: (_request, reply) => { | ||
reply(renderVoyagerPage(middlewareOptions)) | ||
.header('Content-Type', 'text/html') | ||
}, | ||
}); | ||
|
||
return next(); | ||
}; | ||
|
||
hapi.attributes = { | ||
pkg, | ||
multiple: false | ||
}; | ||
|
||
export default hapi; |
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,11 @@ | ||
import { default as express } from './express'; | ||
import { default as hapi } from './hapi'; | ||
import { default as koa } from './koa'; | ||
import { default as renderVoyagerPage } from './render-voyager-page'; | ||
|
||
export { | ||
express, | ||
hapi, | ||
koa, | ||
renderVoyagerPage | ||
} |
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,31 @@ | ||
import { Context } from 'koa'; | ||
import renderVoyagerPage, { MiddlewareOptions } from './render-voyager-page'; | ||
|
||
const { version } = require('../package.json'); | ||
|
||
interface KoaVoyagerMiddleware { | ||
(ctx: Context, next: () => void): void; | ||
} | ||
|
||
interface Register { | ||
(options): KoaVoyagerMiddleware | ||
} | ||
|
||
const koa: Register = function (options) { | ||
const middlewareOptions: MiddlewareOptions = { | ||
...options, | ||
version | ||
}; | ||
|
||
return async function voyager(ctx, next) { | ||
try { | ||
ctx.body = renderVoyagerPage(middlewareOptions); | ||
await next(); | ||
} catch (err) { | ||
ctx.body = {message: err.message}; | ||
ctx.status = err.status || 500 | ||
} | ||
} | ||
}; | ||
|
||
export default koa; |
Oops, something went wrong.