From 539dd641bf88831639dd433baf9e49d9313f87ef Mon Sep 17 00:00:00 2001 From: Alan Christensen Date: Mon, 26 Dec 2016 15:39:44 -0600 Subject: [PATCH] Use graphiql option variables if query variables are not supplied --- CHANGELOG.md | 2 ++ .../src/expressApollo.ts | 3 +- .../src/hapiApollo.test.ts | 4 +-- .../graphql-server-hapi/src/hapiApollo.ts | 4 +-- .../src/index.ts | 30 +++++++++++++++++++ packages/graphql-server-koa/src/koaApollo.ts | 3 +- 6 files changed, 37 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 12357ac0bfd..8b710567df8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # Changelog ### VNEXT +* Fix GraphiQL options variables. Issue #193. ([@alanchristensen](https://github.com/alanchristensen)) on +[PR #255](https://github.com/apollostack/apollo-server/pull/255) ### v0.5.1 * add support for HTTP GET Method ([@DxCx](https://github.com/DxCx)) on [#180](https://github.com/apollostack/graphql-server/pull/180) diff --git a/packages/graphql-server-express/src/expressApollo.ts b/packages/graphql-server-express/src/expressApollo.ts index 048f6df6128..d3ed376f9fd 100644 --- a/packages/graphql-server-express/src/expressApollo.ts +++ b/packages/graphql-server-express/src/expressApollo.ts @@ -68,13 +68,12 @@ export function graphiqlExpress(options: GraphiQL.GraphiQLData) { return (req: express.Request, res: express.Response) => { 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, }); diff --git a/packages/graphql-server-hapi/src/hapiApollo.test.ts b/packages/graphql-server-hapi/src/hapiApollo.test.ts index 7540b68833a..b0299aca061 100644 --- a/packages/graphql-server-hapi/src/hapiApollo.test.ts +++ b/packages/graphql-server-hapi/src/hapiApollo.test.ts @@ -24,9 +24,7 @@ function createApp(options: CreateAppOptions) { register: graphiqlHapi, options: { path: '/graphiql', - graphiqlOptions: { - endpointURL: '/graphql', - }, + graphiqlOptions: (options && options.graphiqlOptions) || { endpointURL: '/graphql' }, }, }); diff --git a/packages/graphql-server-hapi/src/hapiApollo.ts b/packages/graphql-server-hapi/src/hapiApollo.ts index 0973707077e..84cd4dc470e 100644 --- a/packages/graphql-server-hapi/src/hapiApollo.ts +++ b/packages/graphql-server-hapi/src/hapiApollo.ts @@ -112,7 +112,7 @@ graphiqlHapi.attributes = { function getGraphiQLParams(request, reply) { const q = request.query || {}; const query = q.query || ''; - const variables = q.variables || '{}'; + const variables = q.variables; const operationName = q.operationName || ''; reply({ query, variables, operationName}); } @@ -122,7 +122,7 @@ function renderGraphiQL(route, graphiqlParams: any, reply) { const graphiQLString = GraphiQL.renderGraphiQL({ endpointURL: graphiqlOptions.endpointURL, query: graphiqlParams.query || graphiqlOptions.query, - variables: JSON.parse(graphiqlParams.variables) || graphiqlOptions.variables, + variables: graphiqlParams.variables && JSON.parse(graphiqlParams.variables) || graphiqlOptions.variables, operationName: graphiqlParams.operationName || graphiqlOptions.operationName, passHeader: graphiqlOptions.passHeader, }); diff --git a/packages/graphql-server-integration-testsuite/src/index.ts b/packages/graphql-server-integration-testsuite/src/index.ts index 0ab809014b7..ec738207d54 100644 --- a/packages/graphql-server-integration-testsuite/src/index.ts +++ b/packages/graphql-server-integration-testsuite/src/index.ts @@ -693,6 +693,36 @@ export default (createApp: CreateAppFunc, destroyApp?: DestroyAppFunc) => { expect(response.text).to.include('graphiql.min.js'); }); }); + + it('presents options variables', () => { + app = createApp({graphiqlOptions: { + endpointURL: '/graphql', + variables: {key: 'optionsValue'}, + }}); + + const req = request(app) + .get('/graphiql') + .set('Accept', 'text/html'); + return req.then((response) => { + expect(response.status).to.equal(200); + expect(response.text.replace(/\s/g, '')).to.include('variables:"{\\n\\"key\\":\\"optionsValue\\"\\n}"'); + }); + }); + + it('presents query variables over options variables', () => { + app = createApp({graphiqlOptions: { + endpointURL: '/graphql', + variables: {key: 'optionsValue'}, + }}); + + const req = request(app) + .get('/graphiql?variables={"key":"queryValue"}') + .set('Accept', 'text/html'); + return req.then((response) => { + expect(response.status).to.equal(200); + expect(response.text.replace(/\s/g, '')).to.include('variables:"{\\n\\"key\\":\\"queryValue\\"\\n}"'); + }); + }); }); describe('stored queries', () => { diff --git a/packages/graphql-server-koa/src/koaApollo.ts b/packages/graphql-server-koa/src/koaApollo.ts index 759c6492f85..3bdeab0e901 100644 --- a/packages/graphql-server-koa/src/koaApollo.ts +++ b/packages/graphql-server-koa/src/koaApollo.ts @@ -49,13 +49,12 @@ export function graphiqlKoa(options: GraphiQL.GraphiQLData) { const q = ctx.request.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, });