CodeGenieApp / serverless-express

Run Express and other Node.js frameworks on AWS Serverless technologies such as Lambda, API Gateway, Lambda@Edge, and more.
https://codegenie.codes
Apache License 2.0
5.15k stars 669 forks source link

Body data is not being parsed when programmatically invoking graphql handler in apollo-server-lambda v3 #436

Open tiagocpeixoto opened 3 years ago

tiagocpeixoto commented 3 years ago

When programmatically invoking graphql handler in apollo-server-lambda v3, I receive the following error message: "POST body missing, invalid Content-Type, or JSON object has no keys.".

After reviewing the issue, I posted an issue @ apollo-server

But it looks like the solution is to change the @vendia/serverless-express code as described below.

The body data is extracted by this code from utils.js (@vendia/serverless-express):

function getEventBody ({
  event,
  body = event.body,
  isBase64Encoded = event.isBase64Encoded
}) {
  return Buffer.from(body, isBase64Encoded ? 'base64' : 'utf8')
}

function getRequestValuesFromEvent ({
  ...
  if (event.body) {
    body = getEventBody({ event })
    const { isBase64Encoded } = event
    headers['content-length'] = Buffer.byteLength(body, isBase64Encoded ? 'base64' : 'utf8')
  }
...
}

But where is the code that JSON.parse the body data? Should it?

As an experiment, I changed the code from this...

  ...
  if (event.body) {
    body = getEventBody({ event })
    const { isBase64Encoded } = event
    headers['content-length'] = Buffer.byteLength(body, isBase64Encoded ? 'base64' : 'utf8')
  }
  ...

...to this:

  ...
  if (event.body) {
    const decodedBody = getEventBody({ event })
    const { isBase64Encoded } = event
    headers['content-length'] = Buffer.byteLength(decodedBody, isBase64Encoded ? 'base64' : 'utf8')
    body = JSON.parse(decodedBody);
  }
  ...

and it worked as expected!

Should I submit a PR to @vendia/serverless-express ???

tiagocpeixoto commented 3 years ago

This issue can be closed. See this comment to understand why.