Skip to content
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

Use graphiql option variables if query variables are not supplied #255

Merged
merged 1 commit into from
Jan 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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