I’m attempting to return data from a GraphQL endpoint via Axios and capture that data in a variable outside of the Axios function (available globally). I can console.log
the returned data within the axios function, but I need it available outside the function – here is my attempt at achieving this, but I get an empty set when the data is returned.
Here is my code:
var axios = require("axios");
var data = JSON.stringify({
query: `{
customers(first: 200) {
pageInfo {
startCursor,
endCursor,
hasNextPage
}
edges {
cursor
node {
id: id,
displayName: displayName,
email: email,
rep_name: metafield(
namespace: "hbsp"
key: "rep_name"
) {
value
}
rep_phone: metafield(
namespace: "hbsp"
key: "rep_phone"
) {
value
}
rep_email: metafield(
namespace: "hbsp"
key: "rep_email"
) {
value
}
}
}
}
}`,
variables: {},
});
var config = {
method: "post",
url: "<endpoint>",
headers: {
"Token": "xxxxxxxxxxxxxxxxxxxxxxxx",
"Content-Type": "application/json",
},
data: data,
};
const dataObj = async function(response) {
axios(config)
.then(function(response) {
return response
})
.catch(function(error) {
console.log(error);
});
}
dataObj(data).then(function(data) {
console.log(JSON.stringify(dataObj(data)));
});