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

fix(lambda) File uploads #4506

Merged
Merged
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The version headers in this history reflect the versions of Apollo Server itself

- `apollo-server-testing`: types: Allow generic `variables` usage of `query` and `mutate` functions. [PR #4383](https://github.com/apollograpqh/apollo-server/pull/4383)
- `apollo-server-express`: Export the `GetMiddlewareOptions` type. [PR #4599](https://github.com/apollograpqh/apollo-server/pull/4599)

- `apollo-server-lambda`: Fix file uploads - ignore base64 decoding for multipart queries. [PR #4506](https://github.com/apollographql/apollo-server/pull/4506)
## v2.18.2

- `apollo-server-core`: Explicitly include `lru-cache` dependency in `apollo-server-core`'s dependencies. [PR #4600](https://github.com/apollographql/apollo-server/pull/4600)
Expand Down
11 changes: 6 additions & 5 deletions packages/apollo-server-lambda/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,16 @@ export class ApolloServer extends ApolloServerBase {
};

const fileUploadHandler = (next: Function) => {
const contentType =
event.headers["content-type"] || event.headers["Content-Type"];
if (contentType && contentType.startsWith("multipart/form-data")
&& typeof processFileUploads === "function") {
const contentType = (
event.headers['content-type'] || event.headers['Content-Type'] || ''
).toLowerCase();
if (contentType.startsWith('multipart/form-data')
&& typeof processFileUploads === 'function') {
const request = new FileUploadRequest() as IncomingMessage;
request.push(
Buffer.from(
<any>event.body,
event.isBase64Encoded ? "base64" : "ascii"
event.isBase64Encoded ? 'base64' : 'ascii'
)
);
request.push(null);
Expand Down
16 changes: 8 additions & 8 deletions packages/apollo-server-lambda/src/lambdaApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,14 @@ export function graphqlLambda(
callback,
): void => {
context.callbackWaitsForEmptyEventLoop = false;
let { body, isBase64Encoded } = event;
let { body, headers, isBase64Encoded } = event;
let query: Record<string, any> | Record<string, any>[];
const contentType = (
headers['content-type'] || headers['Content-Type'] || ''
).toLowerCase();
const isMultipart = contentType.startsWith('multipart/form-data');

if (body && isBase64Encoded) {
if (body && isBase64Encoded && !isMultipart) {
body = Buffer.from(body, 'base64').toString();
}

Expand All @@ -45,12 +50,7 @@ export function graphqlLambda(
});
}

const contentType = event.headers["content-type"] || event.headers["Content-Type"];
let query: Record<string, any> | Record<string, any>[];

if (body && event.httpMethod === 'POST' &&
contentType && contentType.startsWith("multipart/form-data")
) {
if (body && event.httpMethod === 'POST' && isMultipart) {
query = body as any;
} else if (body && event.httpMethod === 'POST') {
query = JSON.parse(body);
Expand Down