1

I'm building a graphql server and one of the resolvers should return an url that is fetched from the aws api. I have tried in hours with promises, async await but nothing have worked yet.

What happens in the code i the following: 1) i make a call to aws api, and get a signed url in the callback. 2) i want to return that url in the graphql resolver function - getSignedURL

My question is: How can i make a resolver function return a result that i've got in another functions callback?

I will appreciate any help!

IN CLASS S3Store

var s3 = new aws.S3();

newSignedUrl(callback){

 var params = {
    Bucket: 'testBucket28993746',
    Key: uuid.v4(),
    Expires: 100,
    ContentType: 'image/jpg'
    };

 s3.getSignedUrl('putObject', params, (err, signedURL)=>{

     callback(err,signedURL);
 });
}

Graphql resolver

getSignedURL(){//TODO: more secure, require auth first

let newURL = null;
s3store = new S3Store();

  s3store.newSignedUrl((err,result)=>{
    if(err){
      console.log(err)
     newURL  = {}
     } else{
        console.log(result);
        newURL = result;
      }
    });

  return newURL;
},

When i make a call to the graphql endpoint, i get following:

{
"data": {
"getSignedURL": null
}
}
YousefM
  • 185
  • 9
  • Please show us what you tried with promises, otherwise this is just a duplicate of [How do I convert an existing callback API to promises?](https://stackoverflow.com/q/22519784/1048572) – Bergi Nov 05 '17 at 14:43

1 Answers1

1

This woked for me:

IN CLASS S3Store

getSignedUrl(){

var params = {
    Bucket: 'testBucket28993746',
    Key: uuid.v4(),
    Expires: 100,
    ContentType: 'image/jpg'
    };

    return  new Promise ((resolve, reject)=> { s3.getSignedUrl('putObject',params, (err, signedURL)=>{
            if(err){
             reject(err);
            } else {
               resolve( signedURL);
               // console.log(signedURL);
                console.log("in else ")


            }


            })
        })
    }

Graphql resolver

getSignedURL(){

    return new S3Store().getSignedUrl().then((url)=> {return url});
  }
YousefM
  • 185
  • 9