jaydenseric / graphql-upload

Middleware and an Upload scalar to add support for GraphQL multipart requests (file uploads via queries and mutations) to various Node.js GraphQL servers.
https://npm.im/graphql-upload
MIT License
1.43k stars 131 forks source link

Issue with uploading PDF to S3 with apollo-server-lambda #303

Closed yevhenii-kas-oppizi closed 2 years ago

yevhenii-kas-oppizi commented 2 years ago

I try to upload a pdf file to S3, and files are uploading, but files have blank pages or are even not open.

I tried to apply the same logic with uploading on the express server without the serverless framework, and it works. I don't understand how I should upload my pdf's to S3 from apollo-server-lambda serverless server, So maybe you can help with some tip

Here is my code, how i run the server:

export const handler = server.createHandler({
  expressAppFromMiddleware(middleware) {
    const app = express();
    app.use(graphqlUploadExpress());
    app.use(middleware);
    return app;
  },
})

Here is my code, how i save the file to S3:

const createUploadStream = (key) => {
  const pass = new stream.PassThrough();
  return {
    writeStream: pass,
    promise: s3
      .upload({
        Bucket: 'my-bucket',
        Key: key,
        Body: pass
      })
      .promise()
  };
}

const resolvers = {
  Upload: GraphQLUpload,
  Mutation: {
    singleUpload: async (parent, { file }) => {
      const { createReadStream, filename, mimetype, encoding } = await file;
      const stream = createReadStream()

      const filePath = filename

      const uploadStream = createUploadStream(filePath);

      stream.pipe(uploadStream.writeStream);

      const result = await uploadStream.promise;

    },
  },
};
jaydenseric commented 2 years ago

graphql-upload is designed to work with a multipart request stream, which is not the way AWS Lambda works. Your request for assistance is better directed at the apollo-server-lambda maintainers and community. You can track if we ever produce an AWS Lambda implementation here which we could then support at https://github.com/jaydenseric/graphql-upload/issues/155 .

I tried to apply the same logic with uploading on the express server without the serverless framework, and it works.

That's is indeed a clue that maybe your resolver code is fine, and there is something wrong with the server infrastructure or apollo-server-lambda, but I'm not in a good position to speculate.