-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #189 from joelgriffith/feature/restify-integration
Adding in restify integration, docs, tests more...
- Loading branch information
Showing
11 changed files
with
248 additions
and
3 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
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,5 @@ | ||
* | ||
!dist | ||
!dist/**/* | ||
dist/**/*.test.* | ||
!package.json |
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 @@ | ||
# graphql-server-restify | ||
|
||
This is the Restify integration for the Apollo community GraphQL Server. [Read the docs.](http://dev.apollodata.com/tools/apollo-server/index.html) |
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,47 @@ | ||
{ | ||
"name": "graphql-server-restify", | ||
"version": "0.5.1", | ||
"description": "Production-ready Node.js GraphQL server for Restify", | ||
"main": "dist/index.js", | ||
"scripts": { | ||
"compile": "tsc", | ||
"prepublish": "npm run compile" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/apollostack/graphql-server/tree/master/packages/graphql-server-restify" | ||
}, | ||
"keywords": [ | ||
"GraphQL", | ||
"Apollo", | ||
"Server", | ||
"Restify", | ||
"Javascript" | ||
], | ||
"author": "Jonas Helfer <[email protected]>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/apollostack/graphql-server/issues" | ||
}, | ||
"homepage": "https://github.com/apollostack/graphql-server#readme", | ||
"dependencies": { | ||
"graphql-server-core": "^0.5.1", | ||
"graphql-server-module-graphiql": "^0.4.4" | ||
}, | ||
"devDependencies": { | ||
"@types/restify": "^2.0.38", | ||
"graphql-server-integration-testsuite": "^0.5.1", | ||
"restify": "^4.1.1" | ||
}, | ||
"peerDependencies": { | ||
"graphql": "^0.6.1 || ^0.7.0 || ^0.8.0" | ||
}, | ||
"optionalDependencies": { | ||
"@types/restify": "^2.0.38", | ||
"@types/graphql": "^0.8.6" | ||
}, | ||
"typings": "dist/index.d.ts", | ||
"typescript": { | ||
"definition": "dist/index.d.ts" | ||
} | ||
} |
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,6 @@ | ||
export { | ||
RestifyGraphQLOptionsFunction, | ||
RestifyHandler, | ||
graphqlRestify, | ||
graphiqlRestify, | ||
} from './restifyApollo'; |
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,47 @@ | ||
import 'mocha'; | ||
import * as restify from 'restify'; | ||
import { graphiqlRestify, graphqlRestify } from './restifyApollo'; | ||
import testSuite, { schema, CreateAppOptions } from 'graphql-server-integration-testsuite'; | ||
import { expect } from 'chai'; | ||
import { GraphQLOptions } from 'graphql-server-core'; | ||
import 'mocha'; | ||
|
||
function createApp(options: CreateAppOptions = {}) { | ||
const server = restify.createServer({ | ||
name: 'Restify Test Server', | ||
}); | ||
|
||
options.graphqlOptions = options.graphqlOptions || { schema }; | ||
if (!options.excludeParser) { | ||
server.use(restify.bodyParser()); | ||
server.use(restify.queryParser()); | ||
} | ||
|
||
if (options.graphiqlOptions ) { | ||
server.get('/graphiql', graphiqlRestify( options.graphiqlOptions )); | ||
} | ||
|
||
server.get('/graphql', graphqlRestify(options.graphqlOptions)); | ||
server.post('/graphql', graphqlRestify(options.graphqlOptions)); | ||
|
||
return server; | ||
} | ||
|
||
describe('graphqlRestify', () => { | ||
it('throws error if called without schema', () => { | ||
expect(() => graphqlRestify(undefined as GraphQLOptions)).to.throw('Apollo Server requires options.'); | ||
}); | ||
|
||
it('throws an error if called with more than one argument', () => { | ||
expect(() => (<any>graphqlRestify)({}, 'x')).to.throw( | ||
'Apollo Server expects exactly one argument, got 2'); | ||
}); | ||
|
||
it('generates a function if the options are ok', () => { | ||
expect(() => graphqlRestify({ schema })).to.be.a('function'); | ||
}); | ||
}); | ||
|
||
describe('integration:Restify', () => { | ||
testSuite(createApp); | ||
}); |
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,83 @@ | ||
import * as restify from 'restify'; | ||
import * as url from 'url'; | ||
import { GraphQLOptions, HttpQueryError, runHttpQuery } from 'graphql-server-core'; | ||
import * as GraphiQL from 'graphql-server-module-graphiql'; | ||
|
||
export interface RestifyGraphQLOptionsFunction { | ||
(req?: restify.Request, res?: restify.Response): GraphQLOptions | Promise<GraphQLOptions>; | ||
} | ||
|
||
// Design principles: | ||
// - You can issue a GET or POST with your query. | ||
// - simple, fast and secure | ||
// | ||
|
||
export interface RestifyHandler { | ||
(req: restify.Request, res: restify.Response, next): void; | ||
} | ||
|
||
export function graphqlRestify(options: GraphQLOptions | RestifyGraphQLOptionsFunction): RestifyHandler { | ||
if (!options) { | ||
throw new Error('Apollo Server requires options.'); | ||
} | ||
|
||
if (arguments.length > 1) { | ||
throw new Error(`Apollo Server expects exactly one argument, got ${arguments.length}`); | ||
} | ||
|
||
return (req: restify.Request, res: restify.Response, next): void => { | ||
runHttpQuery([req, res], { | ||
method: req.method, | ||
options: options, | ||
query: req.method === 'POST' ? req.body : req.query, | ||
}).then((gqlResponse) => { | ||
res.setHeader('Content-Type', 'application/json'); | ||
res.write(gqlResponse); | ||
res.end(); | ||
}, (error: HttpQueryError) => { | ||
if ( 'HttpQueryError' !== error.name ) { | ||
throw error; | ||
} | ||
|
||
if ( error.headers ) { | ||
Object.keys(error.headers).forEach((header) => { | ||
res.setHeader(header, error.headers[header]); | ||
}); | ||
} | ||
|
||
res.statusCode = error.statusCode; | ||
res.write(error.message); | ||
res.end(); | ||
}); | ||
}; | ||
} | ||
|
||
/* This middleware returns the html for the GraphiQL interactive query UI | ||
* | ||
* GraphiQLData arguments | ||
* | ||
* - endpointURL: the relative or absolute URL for the endpoint which GraphiQL will make queries to | ||
* - (optional) query: the GraphQL query to pre-fill in the GraphiQL UI | ||
* - (optional) variables: a JS object of variables to pre-fill in the GraphiQL UI | ||
* - (optional) operationName: the operationName to pre-fill in the GraphiQL UI | ||
* - (optional) result: the result of the query to pre-fill in the GraphiQL UI | ||
*/ | ||
|
||
export function graphiqlRestify(options: GraphiQL.GraphiQLData) { | ||
return (req: restify.Request, res: restify.Response, next) => { | ||
const q = req.url && url.parse(req.url, true).query || {}; | ||
const query = q.query || ''; | ||
const operationName = q.operationName || ''; | ||
|
||
const graphiQLString = GraphiQL.renderGraphiQL({ | ||
endpointURL: options.endpointURL, | ||
query: query || options.query, | ||
variables: q.variables && JSON.parse(q.variables) || options.variables, | ||
operationName: operationName || options.operationName, | ||
passHeader: options.passHeader, | ||
}); | ||
res.setHeader('Content-Type', 'text/html'); | ||
res.write(graphiQLString); | ||
res.end(); | ||
}; | ||
} |
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,25 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es6", | ||
"module": "commonjs", | ||
"moduleResolution": "node", | ||
"sourceMap": true, | ||
"declaration": true, | ||
"noImplicitAny": false, | ||
"rootDir": "./src", | ||
"outDir": "./dist", | ||
"allowSyntheticDefaultImports": false, | ||
"pretty": true, | ||
"removeComments": true, | ||
"typeRoots": [ | ||
"node_modules/@types" | ||
], | ||
"types": [ | ||
"@types/node" | ||
] | ||
}, | ||
"exclude": [ | ||
"node_modules", | ||
"dist" | ||
] | ||
} |
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