-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DO NOT MERGE YET - Feature/restify docs tests #179
Closed
joelgriffith
wants to merge
4
commits into
apollographql:master
from
joelgriffith:feature/restify-docs-tests
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 @@ | ||
import * as restify from 'restify'; | ||
import { apolloRestify, graphiqlRestify } from '../'; | ||
import testSuite, { Schema, CreateAppOptions } from './integrations.test'; | ||
import { expect } from 'chai'; | ||
import ApolloOptions from './apolloOptions'; | ||
|
||
function createApp(options: CreateAppOptions = {}) { | ||
const server = restify.createServer({ | ||
name: 'GraphQL Demo', | ||
}); | ||
|
||
options.apolloOptions = options.apolloOptions || { schema: Schema }; | ||
if (!options.excludeParser) { | ||
server.use(restify.bodyParser()); | ||
} | ||
|
||
if (options.graphiqlOptions ) { | ||
server.get('/graphiql', graphiqlRestify( options.graphiqlOptions )); | ||
} | ||
|
||
server.post('/graphql', apolloRestify( options.apolloOptions )); | ||
|
||
return server; | ||
} | ||
|
||
describe('restifyApollo', () => { | ||
it('throws error if called without schema', function(){ | ||
expect(() => apolloRestify(undefined as ApolloOptions)).to.throw('Apollo Server requires options.'); | ||
}); | ||
|
||
it('throws an error if called with more than one argument', function(){ | ||
expect(() => (<any>apolloRestify)({}, 'x')).to.throw( | ||
'Apollo Server expects exactly one argument, got 2'); | ||
}); | ||
}); | ||
|
||
describe('integration:Restify', () => { | ||
testSuite(createApp); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any help needed to add more tests ? For now, you only ensure the server is not crashing (which is a good start!). |
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,164 @@ | ||
import * as restify from 'restify'; | ||
import * as graphql from 'graphql'; | ||
import * as url from 'url'; | ||
import { runQuery } from '../core/runQuery'; | ||
|
||
import ApolloOptions from './apolloOptions'; | ||
import * as GraphiQL from '../modules/renderGraphiQL'; | ||
|
||
export interface RestifyApolloOptionsFunction { | ||
(req?: restify.Request, res?: restify.Response): ApolloOptions | Promise<ApolloOptions>; | ||
} | ||
|
||
// Design principles: | ||
// - there is just one way allowed: POST request with JSON body. Nothing else. | ||
// - simple, fast and secure | ||
// | ||
|
||
export interface RestifyHandler { | ||
(req: restify.Request, res: restify.Response, next): void; | ||
} | ||
|
||
export function apolloRestify(options: ApolloOptions | RestifyApolloOptionsFunction): RestifyHandler { | ||
if (!options) { | ||
throw new Error('Apollo Server requires options.'); | ||
} | ||
|
||
if (arguments.length > 1) { | ||
// TODO: test this | ||
throw new Error(`Apollo Server expects exactly one argument, got ${arguments.length}`); | ||
} | ||
|
||
return async (req: restify.Request, res: restify.Response, next) => { | ||
let optionsObject: ApolloOptions; | ||
if (isOptionsFunction(options)) { | ||
try { | ||
optionsObject = await options(req, res); | ||
} catch (e) { | ||
res.statusCode = 500; | ||
res.write(`Invalid options provided to ApolloServer: ${e.message}`); | ||
res.end(); | ||
} | ||
} else { | ||
optionsObject = options; | ||
} | ||
|
||
const formatErrorFn = optionsObject.formatError || graphql.formatError; | ||
|
||
if (req.method !== 'POST') { | ||
res.setHeader('Allow', 'POST'); | ||
res.statusCode = 405; | ||
res.write('Apollo Server supports only POST requests.'); | ||
res.end(); | ||
return; | ||
} | ||
|
||
if (!req.body) { | ||
res.statusCode = 500; | ||
res.write('POST body missing. Did you forget "server.use(restify.bodyParser())"?'); | ||
res.end(); | ||
return; | ||
} | ||
|
||
let b = req.body; | ||
let isBatch = true; | ||
// TODO: do something different here if the body is an array. | ||
// Throw an error if body isn't either array or object. | ||
if (!Array.isArray(b)) { | ||
isBatch = false; | ||
b = [b]; | ||
} | ||
|
||
let responses: Array<graphql.GraphQLResult> = []; | ||
for (let requestParams of b) { | ||
try { | ||
const query = requestParams.query; | ||
const operationName = requestParams.operationName; | ||
let variables = requestParams.variables; | ||
|
||
if (typeof variables === 'string') { | ||
try { | ||
variables = JSON.parse(variables); | ||
} catch (error) { | ||
res.statusCode = 400; | ||
res.write('Variables are invalid JSON.'); | ||
res.end(); | ||
return; | ||
} | ||
} | ||
|
||
let params = { | ||
schema: optionsObject.schema, | ||
query: query, | ||
variables: variables, | ||
context: optionsObject.context, | ||
rootValue: optionsObject.rootValue, | ||
operationName: operationName, | ||
logFunction: optionsObject.logFunction, | ||
validationRules: optionsObject.validationRules, | ||
formatError: formatErrorFn, | ||
formatResponse: optionsObject.formatResponse, | ||
debug: optionsObject.debug, | ||
}; | ||
|
||
if (optionsObject.formatParams) { | ||
params = optionsObject.formatParams(params); | ||
} | ||
|
||
responses.push(await runQuery(params)); | ||
} catch (e) { | ||
responses.push({ errors: [formatErrorFn(e)] }); | ||
} | ||
} | ||
|
||
res.setHeader('Content-Type', 'application/json'); | ||
if (isBatch) { | ||
res.write(JSON.stringify(responses)); | ||
res.end(); | ||
} else { | ||
const gqlResponse = responses[0]; | ||
if (gqlResponse.errors && typeof gqlResponse.data === 'undefined') { | ||
res.statusCode = 400; | ||
} | ||
res.write(JSON.stringify(gqlResponse)); | ||
res.end(); | ||
} | ||
|
||
}; | ||
} | ||
|
||
function isOptionsFunction(arg: ApolloOptions | RestifyApolloOptionsFunction): arg is RestifyApolloOptionsFunction { | ||
return typeof arg === 'function'; | ||
} | ||
|
||
/* 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 variables = q.variables || '{}'; | ||
const operationName = q.operationName || ''; | ||
|
||
|
||
const graphiQLString = GraphiQL.renderGraphiQL({ | ||
endpointURL: options.endpointURL, | ||
query: query || options.query, | ||
variables: JSON.parse(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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doing this makes sure that the interfaces are properly exports as opposed to referencing them directly, so tests a few things in one :)