Skip to content

Commit

Permalink
feat(lambda): Add support for isBase64Encoded (#4311)
Browse files Browse the repository at this point in the history
Support AWS Lambda Proxy when `event.isBase64Encoded` is `true` by
decoded from Base64 when that is the case.
  • Loading branch information
styfle committed Jul 8, 2020
1 parent e2accbe commit b830d6e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 10 additions & 5 deletions packages/apollo-server-lambda/src/lambdaApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -43,12 +48,12 @@ export function graphqlLambda(
const contentType = event.headers["content-type"] || event.headers["Content-Type"];
let query: Record<string, any> | Record<string, any>[];

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 || {};
}
Expand Down

0 comments on commit b830d6e

Please sign in to comment.