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
}
}
1

1 Answer 1

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});
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.