Skip to content

Lambda: isBase64encoded conversion breaks file upload #4430

@lassesteffen

Description

@lassesteffen

General
apollo-server-lambda 2.16.0
Related: #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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions