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

Lambda: isBase64encoded conversion breaks file upload #4430

Closed
lassesteffen opened this issue Jul 30, 2020 · 5 comments · Fixed by #4506
Closed

Lambda: isBase64encoded conversion breaks file upload #4430

lassesteffen opened this issue Jul 30, 2020 · 5 comments · Fixed by #4506

Comments

@lassesteffen
Copy link

lassesteffen commented Jul 30, 2020

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

@krailler
Copy link

krailler commented Aug 2, 2020

Same here

@krailler
Copy link

krailler commented Aug 2, 2020

I switch back to 2.15.1

@mathix420
Copy link
Contributor

@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.

@lassesteffen
Copy link
Author

Thanks! When is this going to be released?

@lassesteffen
Copy link
Author

This is released, can this be closed?

@abernix abernix closed this as completed Dec 31, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants