Apollo-server: Lambda: isBase64encoded conversion breaks file upload

Created on 30 Jul 2020  ·  5Comments  ·  Source: apollographql/apollo-server


General
apollo-server-lambda 2.16.0
Related: https://github.com/apollographql/apollo-server/pull/4311

Expected
I can upload images with a multipart form data request

Actual
Image upload does not work anymore since this version because of the base64 decoding. I am getting

{
    "errorType": "Runtime.UnhandledPromiseRejection",
    "errorMessage": "TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received an instance of Object",
    "reason": [
        {
            "errorType": "TypeError",
            "errorMessage": "The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received an instance of Object",
            "message": "The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received an instance of Object",
            "extensions": {
                "code": "INTERNAL_SERVER_ERROR"
            }
        }
    ],
    "promise": {},
    "stack": [
        "Runtime.UnhandledPromiseRejection: TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received an instance of Object",
        "    at process.<anonymous> (/var/runtime/index.js:35:15)",
        "    at process.emit (events.js:310:20)",
        "    at process.EventEmitter.emit (domain.js:482:12)",
        "    at processPromiseRejections (internal/process/promises.js:209:33)",
        "    at processTicksAndRejections (internal/process/task_queues.js:98:32)"
    ]
}

Code

export default async function defaultHandler(
  obj: undefined,
  args: Record<string, any>,
  context: ApolloContext,
  info: GraphQLResolveInfo,
): Promise<any> {
  let { body } = context;
  let { headers } = context;
  if (context.headers['content-type'].startsWith('multipart/form-data')) {
    body = new FormData();

    //  Await the file, we always use content as variable
    const file: Upload = ...;

    const operations = {
      ...context.body,
      variables: { ...context.body.variables, content: null },
    };

    body.append('operations', JSON.stringify(operations));
    body.append('map', JSON.stringify({ '0': ['variables.content'] }));
    body.append('0', file.createReadStream(), file);

  }

Had to switch back to v2.15.1 for now

🖇️ lambda

Most helpful comment

This is released, can this be closed?

All 5 comments

Same here

I switch back to 2.15.1

@krailler and @lassesteffen if you want the latest version of apollo-server-lambda and a fix for file uploads you can use tarballs from here.

Thanks! When is this going to be released?

This is released, can this be closed?

Was this page helpful?
0 / 5 - 0 ratings