Uploading Image in Apollo Server >2.0 with Express integration not conclusive

Viewed 412

I was trying to upload image using nodejs apollo-server-express ^2.18.1 with express integration. The problem is I am always stuck as the sever never returns to the client. I tried using Altair and react application as client; the result is the same. Altair just keeps on showing loading. The only feed back I get from the server is console "(node:7537) [DEP0135] DeprecationWarning: ReadStream.prototype.open() is deprecated (Use node --trace-deprecation ... to show where the warning was created)" My resolver setup is as follows:

        let uploadProcess = async (file)=>{
        let {createReadStream, filename, mimetype, encoding} = await file;
        let stream = createReadStream();
        console.log(stream);
        let path = "uploads/" + uuid() + filename;
        return new Promise((resolve,reject)=>
            stream
            .on("error", (error)=>{
                if (error) fs.unlinkSync(path);
                    reject({
                        success: false,
                        message: "Some Error"
               });
            })
            .pipe(fs.createWriteStream(path))
            .on("finish", ()=> {
                console.log("FINISH");
                resolve({
                    filename, mimetype, encoding, location: path,
                    success: true, message: "Successfully Uploaded!"
                })
            })
        )
    }

    let uploadProcessToS3 = async (file) =>{
        let {createReadStream, mimetype, filename, encoding} = await file;
        let stream = createReadStream();
        let upload = await s3.upload({
            Body: stream,
            Key: `${uuid()}${filename}`,
            ContentType: mimetype
        }).promise();
        return {
            filename, mimetype, encoding, location: upload.Location
        }
    }

    let typeCheck = async (item)=>{
        let {mimetype} = await item;
        if(!(mimetype === "image/png" || mimetype === "image/jpeg")){
            return false;
        }else{
            return true;
        }
    }

    const resolvers = {
        Mutation: {
            singleUpload : async (_, args)=>{
                let t = await typeCheck(args.file);
                if (t){
                    return uploadProcess(args.file);
                }else{
                    return {
                        success: false,
                        message: "Type Error"
                    }
                }
            
            }
        }
      }

The dependencies installed as listed in my package.json file is :

```
    {
      "name": "apollouploadmultiple",
      "version": "1.0.0",
      "description": "Uploading Multiple Files on apollo-server-express ",
      "main": "app.js",
      "scripts": {
            "start": "node app.js",
            "dev": "nodemon app.js"
      },
      "keywords": [
        "Express",
        "Apollo",
        "apollo-server-express",
        "GraphQL",
        "Multiple-File-Upload"
      ],
      "license": "ISC",
      "dependencies": {
        "apollo-server-express": "^2.18.1",
        "aws-sdk": "^2.761.0",
        "dotenv": "^8.2.0",
        "express": "^4.17.1",
        "graphql": "^15.3.0",
        "nodemon": "^2.0.4",
        "uuid": "^8.3.0"
      }
    }
``` 

The upload folder contains the the uploaded file BUT ONLY AS AN EMPTY TEXT FILE even though the file extension is correct as "png"

Please I will appreciate whatever help. I have been stuck on this for weeks

0 Answers
Related