I am new to Uniswap and GraphQL... I am trying to fetch the price of ETH using node.js and have the following call. The body of the response is unfortunately undefined
for some reason. Could you please let me know what I might be doing wrong here?
const uniSwapResponse = await fetch("https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v2", {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify({ query: '{ bundle(id: "1" ) {ethPrice} }' })
});
console.log(uniSwapResponse.data);
EDIT:
The below did the trick for me:
fetch("https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v2", {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify({ query: '{ bundle(id: "1") {ethPrice} }' })
}).then(r => r.json())
.then(data => console.log("ETH price:", data.data.bundle.ethPrice))
Thanks,
Doug
You can use await
in async
function only.
The simplest approach would be:
fetch("https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v2", {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify({ query: 'ethPrice { bundle(id: "1") { ethPrice } }' })
})
.then((response) => { console.log(response) });
Also check this post.