diff --git a/CHANGELOG.md b/CHANGELOG.md index c24dbb3ceb1..412bbb16068 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ The version headers in this history reflect the versions of Apollo Server itself > The changes noted within this `vNEXT` section have not been released yet. New PRs and commits which introduce changes should include an entry in this `vNEXT` section as part of their development. With few exceptions, the format of the entry should follow convention (i.e., prefix with package name, use markdown `backtick formatting` for package names and code, suffix with a link to the change-set à la `[PR #YYY](https://link/pull/YYY)`, etc.). When a release is being prepared, a new header will be (manually) created below and the appropriate changes within that release will be moved into the new section. -- _Nothing yet! Stay tuned!_ +- `apollo-server-lamdbda`: Automatically decode payloads which are Base64-encoded when the `isBase64Encoded` boolean is present on Lambda `event` payloads. [PR #4311](https://github.com/apollographql/apollo-server/pull/4311) ## v2.15.1 diff --git a/packages/apollo-server-lambda/src/lambdaApollo.ts b/packages/apollo-server-lambda/src/lambdaApollo.ts index 934c2959593..fad594122e9 100644 --- a/packages/apollo-server-lambda/src/lambdaApollo.ts +++ b/packages/apollo-server-lambda/src/lambdaApollo.ts @@ -32,8 +32,13 @@ export function graphqlLambda( callback, ): void => { context.callbackWaitsForEmptyEventLoop = false; + let { body, isBase64Encoded } = event; - if (event.httpMethod === 'POST' && !event.body) { + if (body && isBase64Encoded) { + body = Buffer.from(body, 'base64').toString(); + } + + if (event.httpMethod === 'POST' && !body) { return callback(null, { body: 'POST body missing.', statusCode: 500, @@ -43,12 +48,12 @@ export function graphqlLambda( const contentType = event.headers["content-type"] || event.headers["Content-Type"]; let query: Record | Record[]; - if (event.body && event.httpMethod === 'POST' && + if (body && event.httpMethod === 'POST' && contentType && contentType.startsWith("multipart/form-data") ) { - query = event.body as any; - } else if (event.body && event.httpMethod === 'POST') { - query = JSON.parse(event.body); + query = body as any; + } else if (body && event.httpMethod === 'POST') { + query = JSON.parse(body); } else { query = event.queryStringParameters || {}; }