open-api
open-api copied to clipboard
Handle empty body
Hitting Graphql with an empty body is not supported, but we currently expose an error from apollo-server, which we should dress up a little nicer.
▶ curl -X POST https://hxtsoafqna.execute-api.us-east-1.amazonaws.com/stage/graphql
POST body missing. Did you forget use body-parser middleware?%
Find code that generates the error at https://github.com/apollographql/apollo-server/blob/master/packages/apollo-server-core/src/runHttpQuery.ts#L67
switch (request.method) {
case 'POST':
if (!request.query || Object.keys(request.query).length === 0) {
throw new HttpQueryError(
500,
'POST body missing. Did you forget use body-parser middleware?',
);
}
'apollo-server-core' is throwing the error but out 'apollo-server-lambda' hander is catching the error and adding the error message https://github.com/apollographql/apollo-server/blob/master/packages/apollo-server-lambda/src/lambdaApollo.ts
try {
gqlResponse = await runHttpQuery([event, lambdaContext], {
method: event.httpMethod,
options: options,
query: query,
});
headers['Content-Type'] = 'application/json';
statusCode = 200;
} catch (error) {
if ('HttpQueryError' !== error.name) {
throw error;
}
headers = error.headers;
statusCode = error.statusCode;
gqlResponse = error.message;
}