Skip to content

Commit

Permalink
Merge pull request #255 from alanchristensen/master
Browse files Browse the repository at this point in the history
Use graphiql option variables if query variables are not supplied
  • Loading branch information
helfer authored Jan 23, 2017
2 parents ebe8604 + 539dd64 commit 5d60519
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
3 changes: 1 addition & 2 deletions packages/graphql-server-express/src/expressApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down
4 changes: 1 addition & 3 deletions packages/graphql-server-hapi/src/hapiApollo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ function createApp(options: CreateAppOptions) {
register: graphiqlHapi,
options: {
path: '/graphiql',
graphiqlOptions: {
endpointURL: '/graphql',
},
graphiqlOptions: (options && options.graphiqlOptions) || { endpointURL: '/graphql' },
},
});

Expand Down
4 changes: 2 additions & 2 deletions packages/graphql-server-hapi/src/hapiApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}
Expand All @@ -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,
});
Expand Down
30 changes: 30 additions & 0 deletions packages/graphql-server-integration-testsuite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
3 changes: 1 addition & 2 deletions packages/graphql-server-koa/src/koaApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down

0 comments on commit 5d60519

Please sign in to comment.