Skip to content

Commit

Permalink
Updating the lambda handler to handle invalid JSON bodies instead of …
Browse files Browse the repository at this point in the history
…throwing errors
  • Loading branch information
Dan Perkins committed Jul 30, 2019
1 parent 293e28d commit babc688
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
11 changes: 11 additions & 0 deletions packages/apollo-server-integration-testsuite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,17 @@ export default (createApp: CreateAppFunc, destroyApp?: DestroyAppFunc) => {
});
});

it('throws an error if POST body contains invalid JSON', async () => {
app = await createApp();
const req = request(app)
.post('/graphql')
.send('**_NOT_JSON_**');
return req.then(res => {
expect(res.status).toBeGreaterThanOrEqual(400);
expect(res.status).toBeLessThanOrEqual(500);
});
});

it('throws an error if GET query is missing', async () => {
app = await createApp();
const req = request(app).get(`/graphql`);
Expand Down
18 changes: 14 additions & 4 deletions packages/apollo-server-lambda/src/lambdaApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,23 @@ export function graphqlLambda(
statusCode: 500,
});
}

let query: any = event.queryStringParameters;
if (event.httpMethod === 'POST' && event.body) {
try {
query = JSON.parse(event.body);
} catch (error) {
return callback(null, {
body: 'Error parsing JSON POST body',
statusCode: 415,
});
}
}

runHttpQuery([event, context], {
method: event.httpMethod,
options: options,
query:
event.httpMethod === 'POST' && event.body
? JSON.parse(event.body)
: event.queryStringParameters,
query: query,
request: {
url: event.path,
method: event.httpMethod,
Expand Down

0 comments on commit babc688

Please sign in to comment.