Skip to content

Commit

Permalink
Handle variable parse error (#131)
Browse files Browse the repository at this point in the history
* error handling for parsing query variables
* update changelog
  • Loading branch information
nnance authored Sep 10, 2016
1 parent fe9564d commit 00394c6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
[PR #127](https://github.com/apollostack/apollo-server/pull/127)
* Camelcase Hapi. Issue #129. ([@nnance](https://github.com/nnance)) in
[PR #132](https://github.com/apollostack/apollo-server/pull/132)
* Fix error handling when parsing variables parameter. Issue #130. ([@nnance](https://github.com/nnance)) in
[PR #131](https://github.com/apollostack/apollo-server/pull/131)

### v0.2.6
* Expose the OperationStore as part of the public API. ([@nnance](https://github.com/nnance))
Expand Down
10 changes: 8 additions & 2 deletions src/integrations/expressApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,14 @@ export function apolloExpress(options: ApolloOptions | ExpressApolloOptionsFunct
let variables = requestParams.variables;

if (typeof variables === 'string') {
// TODO: catch errors
variables = JSON.parse(variables);
try {
variables = JSON.parse(variables);
} catch (error) {
res.statusCode = 400;
res.write('Variables are invalid JSON.');
res.end();
return;
}
}

let params = {
Expand Down
14 changes: 14 additions & 0 deletions src/integrations/integrations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,20 @@ export default (createApp: CreateAppFunc, destroyApp?: DestroyAppFunc) => {
});
});

it('can handle a request with variables as an invalid string', () => {
app = createApp();
const req = request(app)
.post('/graphql')
.send({
query: 'query test($echo: String!){ testArgument(echo: $echo) }',
variables: '{ echo: "world" }',
});
return req.then((res) => {
expect(res.status).to.equal(400);
return expect(res.error.text).to.contain('Variables are invalid JSON.');
});
});

it('can handle a request with operationName', () => {
app = createApp();
const expected = {
Expand Down
8 changes: 6 additions & 2 deletions src/integrations/koaApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ export function apolloKoa(options: ApolloOptions | KoaApolloOptionsFunction): Ko
let variables = requestParams.variables;

if (typeof variables === 'string') {
// TODO: catch errors
variables = JSON.parse(variables);
try {
variables = JSON.parse(variables);
} catch (error) {
ctx.status = 400;
return ctx.body = 'Variables are invalid JSON.';
}
}

let params = {
Expand Down

0 comments on commit 00394c6

Please sign in to comment.