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

Updating the lambda handler to handle invalid JSON body #3108

Closed
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ const createAzureFunction = (options: CreateAppOptions = {}) => {
req.on('data', chunk => (body += chunk));
req.on('end', () => {
const urlObject = url.parse(req.url, true);
try {
body = JSON.parse(body);
} catch (e) {}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should something be logged here for debugging purposes? Maybe at a low log level?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only saw one other place in the tests where console logs are being used and it seemed to be more for debugging a failed 'expect' validation. I don't think it makes sense to add logging for a case that will always be hit for a specific test case

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I misunderstood what was happening. I did not realize this was a testcase. 🤦‍♂

const request = {
method: req.method,
body: body && JSON.parse(body),
body: body,
path: req.url,
query: urlObject.query,
headers: req.headers,
Expand Down
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 @@ -246,6 +246,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