CodeGenieApp / CodeGenieApp/serverless-express

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

Abierto
#436 1 comentario 0 reacciones 0 asignados Ver en GitHub
Lenguaje dominante
JavaScript
Estrellas
5.3k
Forks
676
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

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](https://github.com/apollographql/apollo-server/issues/5504)

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):

```javascript
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...

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

...to this:

```javascript
...
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 ???

Guía de contribución

Abrir la guía de contribución

Línea de trabajo

Comienza con utils.js, especialmente con getEventBody y getRequestValuesFromEvent, y compara la representación del body esperada por el controlador de Apollo Server Lambda con el valor pasado por serverless-express. El issue se resuelve cuando las invocaciones programáticas de GraphQL gestionan correctamente el body de la solicitud sin romper el comportamiento existente de base64 y content-length, y se verifica el comportamiento relevante.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
aws, express, graphql, javascript, nodejs
Área
api, backend, cloud
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Necesita aclaración
Aptitud para principiantes
25/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.