freeCodeCamp / open-api

freeCodeCamp's open-api Intiative
BSD 3-Clause "New" or "Revised" License
89 stars 28 forks source link

Handle empty body #174

Open ojongerius opened 6 years ago

ojongerius commented 6 years ago

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?',
        );
      }
sivakar12 commented 6 years ago

'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;
}