Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[apollo-server-lambda] Add support for isBase64Encoded #4311

Merged
merged 4 commits into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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