From d456ff5b2b1c4a70fe399594182ec0fbe4f7bbe4 Mon Sep 17 00:00:00 2001 From: Joel Griffith Date: Mon, 24 Oct 2016 11:04:18 -0700 Subject: [PATCH 1/4] Adding in restify integration, docs, tests more... --- CHANGELOG.md | 2 + DESIGN.md | 1 + README.md | 26 ++- packages/graphql-server-restify/.npmignore | 5 + packages/graphql-server-restify/README.md | 3 + packages/graphql-server-restify/package.json | 47 +++++ .../graphql-server-restify/src/index.test.ts | 56 ++++++ packages/graphql-server-restify/src/index.ts | 178 ++++++++++++++++++ packages/graphql-server-restify/tsconfig.json | 26 +++ test/tests.js | 1 + 10 files changed, 342 insertions(+), 3 deletions(-) create mode 100644 packages/graphql-server-restify/.npmignore create mode 100644 packages/graphql-server-restify/README.md create mode 100644 packages/graphql-server-restify/package.json create mode 100644 packages/graphql-server-restify/src/index.test.ts create mode 100644 packages/graphql-server-restify/src/index.ts create mode 100644 packages/graphql-server-restify/tsconfig.json diff --git a/CHANGELOG.md b/CHANGELOG.md index de339e4117f..2e4e153d17d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### VNEXT +* Restify integration and documents/tests. + ### v0.4.2 * **Restructure Apollo Server into 6 new packages, and rename to GraphQL Server** ([@DxCx](https://github.com/DxCx)) and ([@stubailo](https://github.com/stubailo)) in [#183](https://github.com/apollostack/graphql-server/pull/183) and [#164](https://github.com/apollostack/graphql-server/pull/183). diff --git a/DESIGN.md b/DESIGN.md index 79675f503b6..0e0cfa98e18 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -27,6 +27,7 @@ GraphQL Server should come with a set of integrations for different Node.js serv - Hapi - Connect - Koa +- Restify - ... Framework integrations take care of parsing requests, submitting them to GraphQL Server’s core runQuery function, and sending the response back to the client. These integrations should accept requests over HTTP, websockets or other means, then invoke `runQuery` as appropriate, and return the result. They should be written in such a way that makes it easy to add features, such as batched queries, subscriptions etc. diff --git a/README.md b/README.md index ac80a9f5827..12be1cbe7bf 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@ -# GraphQL Server for Express, Connect, Hapi and Koa +# GraphQL Server for Express, Connect, Hapi, Koa, and Restify [![npm version](https://badge.fury.io/js/graphql-server.svg)](https://badge.fury.io/js/graphql-server) [![Build Status](https://travis-ci.org/apollostack/graphql-server.svg?branch=master)](https://travis-ci.org/apollostack/graphql-server) [![Coverage Status](https://coveralls.io/repos/github/apollostack/graphql-server/badge.svg?branch=master)](https://coveralls.io/github/apollostack/graphql-server?branch=master) [![Get on Slack](https://img.shields.io/badge/slack-join-orange.svg)](http://www.apollostack.com/#slack) -GraphQL Server is a community-maintained open-source GraphQL server. It works with all Node.js HTTP server frameworks: Express, Connect, Hapi and Koa. +GraphQL Server is a community-maintained open-source GraphQL server. It works with all Node.js HTTP server frameworks: Express, Connect, Hapi, Koa and Restify. ## Principles @@ -122,6 +122,26 @@ app.use(router.allowedMethods()); app.listen(PORT); ``` +### Restify +```js +import restify from 'restify'; +import { graphqlRestify, graphiqlRestify } from 'graphql-server-restify'; + +const PORT = 3000; + +const server = restify.createServer({ + title: 'GraphQL Server' +}); + +server.use(restify.bodyParser()); + +server.post('/graphql', graphqlRestify({ schema: myGraphQLSchema })); + +sever.get('/graphiql', graphiqlRestify({ endpointURL: '/graphql' })); + +server.listen(PORT, () => console.log(`Listening on ${PORT}`)); +``` + ## Options GraphQL Server can be configured with an options object with the the following fields: @@ -156,7 +176,7 @@ graphqlOptions = { GraphQL Server and express-graphql are more or less the same thing (GraphQL middleware for Node.js), but there are a few key differences: -* express-graphql works with Express and Connect, GraphQL Server supports Express, Connect, Hapi and Koa. +* express-graphql works with Express and Connect, GraphQL Server supports Express, Connect, Hapi, Koa and Restify. * express-graphql's main goal is to be a minimal reference implementation, whereas GraphQL Server's goal is to be a complete production-ready GraphQL server. * Compared to express-graphql, GraphQL Server has a simpler interface and supports exactly one way of passing queries. * GraphQL Server separates serving GraphiQL (GraphQL UI) from responding to GraphQL requests. diff --git a/packages/graphql-server-restify/.npmignore b/packages/graphql-server-restify/.npmignore new file mode 100644 index 00000000000..063364e2b5d --- /dev/null +++ b/packages/graphql-server-restify/.npmignore @@ -0,0 +1,5 @@ +* +!dist +!dist/**/* +dist/**/*.test.* +!package.json diff --git a/packages/graphql-server-restify/README.md b/packages/graphql-server-restify/README.md new file mode 100644 index 00000000000..91fc1659492 --- /dev/null +++ b/packages/graphql-server-restify/README.md @@ -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) diff --git a/packages/graphql-server-restify/package.json b/packages/graphql-server-restify/package.json new file mode 100644 index 00000000000..9c9fc783c7b --- /dev/null +++ b/packages/graphql-server-restify/package.json @@ -0,0 +1,47 @@ +{ + "name": "graphql-server-restify", + "version": "0.4.3", + "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 ", + "license": "MIT", + "bugs": { + "url": "https://github.com/apollostack/graphql-server/issues" + }, + "homepage": "https://github.com/apollostack/graphql-server#readme", + "dependencies": { + "graphql-server-core": "^0.4.3", + "graphql-server-module-graphiql": "^0.4.3" + }, + "devDependencies": { + "@types/restify": "^2.0.33", + "graphql-server-integration-testsuite": "^0.4.3", + "restify": "^4.1.1", + "typed-graphql": "^1.0.2" + }, + "peerDependencies": { + "graphql": "^0.6.1 || ^0.7.0" + }, + "optionalDependencies": { + "@types/restify": "^2.0.33" + }, + "typings": "dist/index.d.ts", + "typescript": { + "definition": "dist/index.d.ts" + } +} diff --git a/packages/graphql-server-restify/src/index.test.ts b/packages/graphql-server-restify/src/index.test.ts new file mode 100644 index 00000000000..84c60afe109 --- /dev/null +++ b/packages/graphql-server-restify/src/index.test.ts @@ -0,0 +1,56 @@ +import 'mocha'; +import * as restify from 'restify'; +import { expect } from 'chai'; +import testSuite, { Schema, CreateAppOptions } from 'graphql-server-integration-testsuite'; +import { GraphQLOptions } from 'graphql-server-core'; + +import { graphqlRestify, graphiqlRestify } from './'; + +// tslint:disable-next-line +const request = require('supertest-as-promised'); + +function createApp(options: CreateAppOptions = {}) { + const server = restify.createServer({ + name: 'Restify Test Server', + }); + + options.graphqlOptions = options.graphqlOptions || { schema: Schema }; + if (!options.excludeParser) { + server.use(restify.bodyParser()); + } + if (options.graphiqlOptions ) { + server.get('/graphiql', graphiqlRestify( options.graphiqlOptions )); + } + 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(() => (graphqlRestify)({}, 'x')).to.throw( + 'Apollo Server expects exactly one argument, got 2'); + }); + + it('generates a function if the options are ok', () => { + expect(() => graphqlRestify({ schema: Schema })).to.be.a('function'); + }); + + it('throws an error if POST body is not an object or array', () => { + const app = createApp(); + const req = request(app) + .post('/graphql') + .send('123'); + return req.then((res) => { + expect(res.status).to.equal(500); + return expect(res.error.text).to.contain('Invalid POST body sent'); + }); + }); +}); + +describe('integration:Restify', () => { + testSuite(createApp); +}); diff --git a/packages/graphql-server-restify/src/index.ts b/packages/graphql-server-restify/src/index.ts new file mode 100644 index 00000000000..470fc0abd0f --- /dev/null +++ b/packages/graphql-server-restify/src/index.ts @@ -0,0 +1,178 @@ +import * as restify from 'restify'; +import * as graphql from 'graphql'; +import * as url from 'url'; +import { GraphQLOptions, runQuery } from 'graphql-server-core'; +import * as GraphiQL from 'graphql-server-module-graphiql'; + +export interface RestifyGraphQLOptionsFunction { + (req?: restify.Request, res?: restify.Response): GraphQLOptions | Promise; +} + +// 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 graphqlRestify(options: GraphQLOptions | RestifyGraphQLOptionsFunction): 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: GraphQLOptions; + 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('graphql-server-restify 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; + let payloadType = typeof b; + + // Only arrays and object types are allowed + if (payloadType !== 'object') { + res.statusCode = 500; + res.write('Invalid POST body sent, expected Array or Object, but saw: ' + payloadType); + res.end(); + return; + } + + if (!Array.isArray(b)) { + isBatch = false; + b = [b]; + } + + let responses: Array = []; + + 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; + } + } + + // Shallow clone context for queries in batches. This allows + // users to distinguish multiple queries in the batch and to + // modify the context object without interfering with each other. + let context = optionsObject.context; + if (isBatch) { + context = Object.assign({}, context || {}); + } + + let params = { + schema: optionsObject.schema, + query: query, + variables: variables, + context: 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: GraphQLOptions | RestifyGraphQLOptionsFunction): arg is RestifyGraphQLOptionsFunction { + 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(); + }; +} diff --git a/packages/graphql-server-restify/tsconfig.json b/packages/graphql-server-restify/tsconfig.json new file mode 100644 index 00000000000..400efae72bf --- /dev/null +++ b/packages/graphql-server-restify/tsconfig.json @@ -0,0 +1,26 @@ +{ + "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": [ + "typed-graphql", + "@types/node" + ] + }, + "exclude": [ + "node_modules", + "dist" + ] +} diff --git a/test/tests.js b/test/tests.js index be146790761..f99345e58da 100644 --- a/test/tests.js +++ b/test/tests.js @@ -10,4 +10,5 @@ require('../packages/graphql-server-express/dist/expressApollo.test'); require('../packages/graphql-server-express/dist/connectApollo.test'); require('../packages/graphql-server-hapi/dist/hapiApollo.test'); require('../packages/graphql-server-koa/dist/koaApollo.test'); +require('../packages/graphql-server-restify/dist/index.test'); require('../packages/graphql-server-express/dist/apolloServerHttp.test'); From 77e1f41223d8d924b0cbb174cbfe6289207613b3 Mon Sep 17 00:00:00 2001 From: Joel Griffith Date: Mon, 24 Oct 2016 12:39:17 -0700 Subject: [PATCH 2/4] Testing failures on PUT calls --- packages/graphql-server-restify/src/index.test.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/packages/graphql-server-restify/src/index.test.ts b/packages/graphql-server-restify/src/index.test.ts index 84c60afe109..6ee7e944c6f 100644 --- a/packages/graphql-server-restify/src/index.test.ts +++ b/packages/graphql-server-restify/src/index.test.ts @@ -22,6 +22,8 @@ function createApp(options: CreateAppOptions = {}) { server.get('/graphiql', graphiqlRestify( options.graphiqlOptions )); } server.post('/graphql', graphqlRestify( options.graphqlOptions )); + server.put('/graphql', graphqlRestify( options.graphqlOptions )); + return server; } @@ -49,6 +51,17 @@ describe('graphqlRestify', () => { return expect(res.error.text).to.contain('Invalid POST body sent'); }); }); + + it('throws an error on PUT calls', () => { + const app = createApp(); + const req = request(app) + .put('/graphql') + .send(); + return req.then((res) => { + expect(res.status).to.equal(405); + return expect(res.error.text).to.contain('supports only POST requests'); + }); + }); }); describe('integration:Restify', () => { From 30a98ab310de1bcabd7824acae236c8a1862f829 Mon Sep 17 00:00:00 2001 From: Joel Griffith Date: Mon, 23 Jan 2017 09:43:58 -0800 Subject: [PATCH 3/4] Fixes for Restify packages, test updates, README --- README.md | 6 ++- packages/graphql-server-restify/package.json | 16 ++++---- .../src/restifyApollo.test.ts | 38 ++++--------------- .../src/restifyApollo.ts | 10 +---- packages/graphql-server-restify/tsconfig.json | 1 - test/tests.js | 2 +- 6 files changed, 24 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index 552e209e2ff..8e5be9d97e9 100644 --- a/README.md +++ b/README.md @@ -140,9 +140,13 @@ const server = restify.createServer({ title: 'GraphQL Server' }); +const graphQLOptions = { schema: myGraphQLSchema }; + server.use(restify.bodyParser()); +server.use(restify.queryParser()); -server.use('/graphql', graphqlRestify({ schema: myGraphQLSchema })); +server.post('/graphql', graphqlRestify(graphQLOptions)); +server.get('/graphql', graphqlRestify(graphQLOptions)); server.get('/graphiql', graphiqlRestify({ endpointURL: '/graphql' })); diff --git a/packages/graphql-server-restify/package.json b/packages/graphql-server-restify/package.json index 2c90e168a20..5c6b8b9262e 100644 --- a/packages/graphql-server-restify/package.json +++ b/packages/graphql-server-restify/package.json @@ -1,6 +1,6 @@ { "name": "graphql-server-restify", - "version": "0.4.3", + "version": "0.5.1", "description": "Production-ready Node.js GraphQL server for Restify", "main": "dist/index.js", "scripts": { @@ -25,20 +25,20 @@ }, "homepage": "https://github.com/apollostack/graphql-server#readme", "dependencies": { - "graphql-server-core": "^0.4.3", - "graphql-server-module-graphiql": "^0.4.3" + "graphql-server-core": "^0.5.1", + "graphql-server-module-graphiql": "^0.4.4" }, "devDependencies": { - "@types/restify": "^2.0.33", - "graphql-server-integration-testsuite": "^0.4.3", - "restify": "^4.1.1", - "typed-graphql": "^1.0.2" + "@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.33" + "@types/restify": "^2.0.38", + "@types/graphql": "^0.8.6" }, "typings": "dist/index.d.ts", "typescript": { diff --git a/packages/graphql-server-restify/src/restifyApollo.test.ts b/packages/graphql-server-restify/src/restifyApollo.test.ts index 6ee7e944c6f..6ba0adee9a6 100644 --- a/packages/graphql-server-restify/src/restifyApollo.test.ts +++ b/packages/graphql-server-restify/src/restifyApollo.test.ts @@ -1,13 +1,10 @@ import 'mocha'; import * as restify from 'restify'; -import { expect } from 'chai'; +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 { graphqlRestify, graphiqlRestify } from './'; - -// tslint:disable-next-line -const request = require('supertest-as-promised'); +import 'mocha'; function createApp(options: CreateAppOptions = {}) { const server = restify.createServer({ @@ -17,12 +14,15 @@ function createApp(options: CreateAppOptions = {}) { options.graphqlOptions = options.graphqlOptions || { schema: Schema }; if (!options.excludeParser) { server.use(restify.bodyParser()); + server.use(restify.queryParser()); } + if (options.graphiqlOptions ) { server.get('/graphiql', graphiqlRestify( options.graphiqlOptions )); } - server.post('/graphql', graphqlRestify( options.graphqlOptions )); - server.put('/graphql', graphqlRestify( options.graphqlOptions )); + + server.get('/graphql', graphqlRestify(options.graphqlOptions)); + server.post('/graphql', graphqlRestify(options.graphqlOptions)); return server; } @@ -40,28 +40,6 @@ describe('graphqlRestify', () => { it('generates a function if the options are ok', () => { expect(() => graphqlRestify({ schema: Schema })).to.be.a('function'); }); - - it('throws an error if POST body is not an object or array', () => { - const app = createApp(); - const req = request(app) - .post('/graphql') - .send('123'); - return req.then((res) => { - expect(res.status).to.equal(500); - return expect(res.error.text).to.contain('Invalid POST body sent'); - }); - }); - - it('throws an error on PUT calls', () => { - const app = createApp(); - const req = request(app) - .put('/graphql') - .send(); - return req.then((res) => { - expect(res.status).to.equal(405); - return expect(res.error.text).to.contain('supports only POST requests'); - }); - }); }); describe('integration:Restify', () => { diff --git a/packages/graphql-server-restify/src/restifyApollo.ts b/packages/graphql-server-restify/src/restifyApollo.ts index 5a9194d50e3..0bcdfb09fd0 100644 --- a/packages/graphql-server-restify/src/restifyApollo.ts +++ b/packages/graphql-server-restify/src/restifyApollo.ts @@ -1,7 +1,6 @@ import * as restify from 'restify'; -import * as graphql from 'graphql'; import * as url from 'url'; -import { GraphQLOptions, runHttpQuery, HttpQueryError } from 'graphql-server-core'; +import { GraphQLOptions, HttpQueryError, runHttpQuery } from 'graphql-server-core'; import * as GraphiQL from 'graphql-server-module-graphiql'; export interface RestifyGraphQLOptionsFunction { @@ -23,11 +22,10 @@ export function graphqlRestify(options: GraphQLOptions | RestifyGraphQLOptionsFu } 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) => { + return (req: restify.Request, res: restify.Response, next): void => { runHttpQuery([req, res], { method: req.method, options: options, @@ -54,10 +52,6 @@ export function graphqlRestify(options: GraphQLOptions | RestifyGraphQLOptionsFu }; } -function isOptionsFunction(arg: GraphQLOptions | RestifyGraphQLOptionsFunction): arg is RestifyGraphQLOptionsFunction { - return typeof arg === 'function'; -} - /* This middleware returns the html for the GraphiQL interactive query UI * * GraphiQLData arguments diff --git a/packages/graphql-server-restify/tsconfig.json b/packages/graphql-server-restify/tsconfig.json index 400efae72bf..1107d54506b 100644 --- a/packages/graphql-server-restify/tsconfig.json +++ b/packages/graphql-server-restify/tsconfig.json @@ -15,7 +15,6 @@ "node_modules/@types" ], "types": [ - "typed-graphql", "@types/node" ] }, diff --git a/test/tests.js b/test/tests.js index f99345e58da..be143063a54 100644 --- a/test/tests.js +++ b/test/tests.js @@ -10,5 +10,5 @@ require('../packages/graphql-server-express/dist/expressApollo.test'); require('../packages/graphql-server-express/dist/connectApollo.test'); require('../packages/graphql-server-hapi/dist/hapiApollo.test'); require('../packages/graphql-server-koa/dist/koaApollo.test'); -require('../packages/graphql-server-restify/dist/index.test'); +require('../packages/graphql-server-restify/dist/restifyApollo.test'); require('../packages/graphql-server-express/dist/apolloServerHttp.test'); From ffe3b983be8dd86dffca6d55f0dde3a1313f0524 Mon Sep 17 00:00:00 2001 From: Hagai Cohen Date: Tue, 24 Jan 2017 12:54:34 +0200 Subject: [PATCH 4/4] test(restify): fix tests for restify while rebasing --- packages/graphql-server-restify/src/restifyApollo.test.ts | 6 +++--- packages/graphql-server-restify/src/restifyApollo.ts | 3 +-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/graphql-server-restify/src/restifyApollo.test.ts b/packages/graphql-server-restify/src/restifyApollo.test.ts index 6ba0adee9a6..6ed9442ca0d 100644 --- a/packages/graphql-server-restify/src/restifyApollo.test.ts +++ b/packages/graphql-server-restify/src/restifyApollo.test.ts @@ -1,7 +1,7 @@ import 'mocha'; import * as restify from 'restify'; import { graphiqlRestify, graphqlRestify } from './restifyApollo'; -import testSuite, { Schema, CreateAppOptions } from 'graphql-server-integration-testsuite'; +import testSuite, { schema, CreateAppOptions } from 'graphql-server-integration-testsuite'; import { expect } from 'chai'; import { GraphQLOptions } from 'graphql-server-core'; import 'mocha'; @@ -11,7 +11,7 @@ function createApp(options: CreateAppOptions = {}) { name: 'Restify Test Server', }); - options.graphqlOptions = options.graphqlOptions || { schema: Schema }; + options.graphqlOptions = options.graphqlOptions || { schema }; if (!options.excludeParser) { server.use(restify.bodyParser()); server.use(restify.queryParser()); @@ -38,7 +38,7 @@ describe('graphqlRestify', () => { }); it('generates a function if the options are ok', () => { - expect(() => graphqlRestify({ schema: Schema })).to.be.a('function'); + expect(() => graphqlRestify({ schema })).to.be.a('function'); }); }); diff --git a/packages/graphql-server-restify/src/restifyApollo.ts b/packages/graphql-server-restify/src/restifyApollo.ts index 0bcdfb09fd0..16953171a95 100644 --- a/packages/graphql-server-restify/src/restifyApollo.ts +++ b/packages/graphql-server-restify/src/restifyApollo.ts @@ -67,13 +67,12 @@ 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, + variables: q.variables && JSON.parse(q.variables) || options.variables, operationName: operationName || options.operationName, passHeader: options.passHeader, });