Invoke Graphql query from Adobe IO

Shashi_Mulugu

MVP

06-10-2020

Hello Team,

 

Can anyone help us on how to invoke a Graphql query from Adobe IO runtime environment.

 

Basically we are trying to build an api layer in Adobe io which acts as middle layer between magento and other consuming applications such as AEM.

 

Any reference invocation or Pseudo code will help.

 

@mhaack @Mihai_Corlan 

View Entire Topic

Dragosche

07-10-2020

@Nikhil-Kumar it seems to me like a javascript/Promise (miss)handling in the code. Using "return" directly won't wait for the Promise to complete. Using Promise's "resolve" is the right way to return a response. 

Please also make sure that your action returns a Promise. If it doesn't return a Promise, while invoking another javascript module which uses Promises, then javascript finishes your action before the other Promise finishes, hence returning a null object. 

Hoping this helps ! 

@Dragosche

@mhaack 

We are using something like below

async customer(){

let output;
let promise = new Promise(function(resolve,reject){
try {
output = client.mutate({
mutation: `My Graphql query`,
variables
});

}
catch(err){
console.log('Error',err);
}

resolve(output);


});

let result;
try{
result = await promise;
console.log('Returned data',result);
}
catch(error){
console.log('error',error);
}

let strData = JSON.stringify(result);
let fvalue;
if(strData){
let prse = JSON.parse(strData);
fvalue = prse.data.createCustomer.customer.email;
console.log('Logged ',fvalue);
}

let obj = {
email:fvalue,
firstname:"Nikhil",
lastname:"Kumar"
}

console.log('Object ',obj);

return Promise.resolve(obj);

}

 

 

If we see the logs, data gets properly logged in order and in the last object it before returning it contains the fetched email from the magento in the object as fvalue. 


Expected data in the final object when seen in logs should be :

{
    "data": {
        "createCustomer": {
            "customer": {
                "email""ndata45111f2581@gm.com",       ----> My fetched email from magento
                "firstname""Nikhil",
                "lastname""Kumar"
            }
        }
    }
}

But the returned response is: 

{
    "data": {
        "createCustomer": {
            "customer": {
                "email"null,                              -----> Response returned is different compared to logged object.
                "firstname""Nikhil",
                "lastname""Kumar"
            }
        }
    }
}



Test same scenario with REST api call, that works absolutely fine.(Thats also a async call).