CodeGenieApp / CodeGenieApp/serverless-express

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

Aperta
#436 1 commento 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
JavaScript
Stelle
5.3k
Fork
676
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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 ???

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Inizia da utils.js, in particolare getEventBody e getRequestValuesFromEvent, e confronta la rappresentazione del body prevista dal gestore Apollo Server Lambda con il valore passato da serverless-express. L’issue è risolta quando le invocazioni programmatiche di GraphQL gestiscono correttamente il body della richiesta senza compromettere il comportamento esistente di base64 e content-length, e il comportamento rilevante è verificato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
aws, express, graphql, javascript, nodejs
Ambito
api, backend, cloud
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.