BitQuery wrong data/not real time - graphql

Hi I started playing with BitQuery graphql to see if it would help me with querying blockchain data. I love the idea of single API for most of the blockchains.
I have a problem tho. The data that Im getting back is critically not accurate and I don't know why.
{
ethereum(network: bsc) {
dexTrades(
exchangeAddress: {is: "0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73"}
baseCurrency: {is: "0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82"}
quoteCurrency: {is: "0x55d398326f99059ff775485246999027b3197955"}
) {
baseCurrency {
symbol
}
quoteCurrency {
symbol
}
quotePrice
}
}
}
Given this query I want to get the Price of Cake/USDT. Which it returns as
{
"ethereum": {
"dexTrades": [
{
"baseCurrency": {
"symbol": "Cake"
},
"quoteCurrency": {
"symbol": "USDT"
},
"quotePrice": 16.96659680611786
}
]
}
}
But When I check with the PancakeSwap Exchange directly or coinmarketcap the price is 40% lower that BitQuery result...
PancakeSwap Price = 10.70
Coingeko Price = 10.75
Am I doing something wrong? Or BitQUery is broken? Or what can be the case? Any Ideas?

We are doing the same. I am actually playing around also in bitquery coz I'm trying to build something and I also encountered this one.
And what I found out is that "quotePrice" is not the actual price of the coin or token. It is just the average quote price that you can use to get the actual price. You still need to compute for price using an intermediary either WETH, etc..
See more info here: https://bitquery.io/blog/dex-price-index

The data returned from Bitquery seems to be correct as I checked from https://pancakeswap.finance/info/tokens which comes out to be around $8.60.
Your GraphQL query returns a different value somehow. I have tried to change the GraphQL query you made which you can check from this link
https://graphql.bitquery.io/ide/httpsstackoverflowcomquestions70784272bitquery-wrong-data-not-real-time
The GraphQL query is as follows:-
{
ethereum(network: bsc) {
dexTrades(
exchangeAddress: {is: "0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73"}
baseCurrency: {is: "0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82"}
quoteCurrency: {is: "0x55d398326f99059ff775485246999027b3197955"}
) {
baseCurrency {
symbol
}
quoteCurrency {
symbol
}
quotePrice(calculate: any)
}
}
}

Related

Shopify Functions Query Input

Im having trouble with my Shopify Function where I want to give a discount on the products if the client chooses pickup. Is there a way to get if the client picked PICK_UP as the delivery option ? I tried looking into this option HERE, but the response from shopify comes back empty does not matter if I choose shipping option or a pickup option. Here is my input query:
query Input {
cart {
lines {
quantity
merchandise {
__typename
...on ProductVariant {
id
}
}
}
deliveryGroups {
selectedDeliveryOption {
deliveryMethodType
}
}
}
discountNode {
metafield(namespace: "this", key: "that") {
value
}
}
}
Am I doing anything wrong? Any ideas what I could try?

Performing A GraphQL Selection On A Union for shopify SellingPlanPricingPolicy

I am trying to get union data using the Graphql APIfor Shopify and am unable to find any documentation on selecting data in unions.
Here is my current query:
query subscriptionPlans {
sellingPlanGroup(id: 1234) {
id
name
sellingPlans(first: 1) {
edges {
node {
id
billingPolicy {
... on SellingPlanRecurringBillingPolicy {
interval
intervalCount
}
}
pricingPolicies {
on
SellingPlanRecurringPricingPolicy {
afterCycle
adjustmentType
adjustmentValue {
on
SellingPlanPricingPolicyPercentageValue {
percentage
}
on
MoneyV2 {
amount
currencyCode
}
}
}
}
}
}
}
}
}
I am able to retrieve the sellingPlans in this scenario. When I get to the SellingPlanPricingPolicy portion I run into issues. I am receiving the error:
Selections can't be made directly on unions (see selections on SellingPlanPricingPolicy)
However, I am unable to find any documentation on making selections using the documentation at: https://shopify.dev/docs/admin-api/graphql/reference/products-and-collections/sellingplanpricingpoli...
Any help with this would be appreciated. I just need an example of how to select this information.
Found the answer in https://graphql.org/learn/queries/#meta-fields. In case of sponsorEntity it looks like this:
sponsorEntity {
... on User {
name
}
}
The solution is to select the attributes on the respective sub-type of the union.

Apollo mixes two different arrays of the same query seemingly at random

With a schema like
schema {
query: QueryRoot
}
scalar MyBigUint
type Order {
id: Int!
data: OrderCommons!
kind: OrderType!
}
type OrderBook {
bids(limit: Int): [Order!]!
asks(limit: Int): [Order!]!
}
type OrderCommons {
quantity: Int!
price: MyBigUint! // where it doesn't matter whether it's MyBigUint or a simple Int - the issue occurs anyways
}
enum OrderType {
BUY
SELL
}
type QueryRoot {
orderbook: OrderBook!
}
And a query query { orderbook { bids { data { price } }, asks { data { price } } } }
In a graphql playground of my graphql API (and on the network level of my Apollo app too) I receive a result like
{
"data": {
"orderbook": {
"bids": [
{
"data": {
"price": "127"
}
},
{
"data": {
"price": "74"
}
},
...
],
"asks": [
{
"data": {
"price": "181"
}
},
{
"data": {
"price": "187"
}
},
...
]
}
}
}
where, for the purpose of this question, the bids are ordered in descending order by price like ["127", "74", "73", "72"], etc, and asks are ordered in ascending order, accordingly.
However, in Apollo, after a query is done, I notice that one of the arrays gets seemingly random data.
For the purpose of the question, useQuery react hook is used, but the same happens when I query imperatively from a freshly initialized ApolloClient.
const { data, subscribeToMore, ...rest } = useQuery<OrderbookResponse>(GET_ORDERBOOK_QUERY);
console.log(data?.orderbook?.bids?.map(r => r.data.price));
console.log(data?.orderbook?.asks?.map(r => r.data.price));
Here, corrupted data of Bids gets printed i.e. ['304', '306', '298', '309', '277', '153', '117', '108', '87', '76'] (notice the order being wrong, at the least), whereas Asks data looks just fine. Inspecting the network, I find that Bids are not only properly ordered there, but also have different (correct, from DB) values!
Therefore, it seems something's getting corrupted on the way while Apollo delivers the data.
What could be the issue here I wonder, and where to start debugging such kind of an issue? There seem to be no warnings from Apollo either, it seems to just silently corrupt the data.
I'm clearly doing something wrong, but what?
The issue seems to stem from how Apollo caches data.
My Bids and Asks could have the same numeric IDs but share the same Order graphql type. Apollo rightfully assumes a Bid and an Ask with the same ID are the same things and the resulting data gets wrecked as a consequence.
An easy fix is to show Apollo that there's a complex key to the Order type on cache initialization:
cache: new InMemoryCache({
typePolicies: {
Order: {
keyFields: ['id', 'kind'],
}
}
})
This way it'll understand that the Order entities Ask and Bid with the same ID are different pieces of data indeed.
Note that the field kind should be also added to the query strings accordingly.

How to use Shopify Graphql ProductVariantsBulkInput

I am trying to bulk update the price on multiple product variants at once. I'm just copying the "interactive" example from Shopify's page
All I did was copy and paste the code and put in my own id's. And of course it does not work.
It looks like this:
mutation productVariantsBulkUpdate($variants: [ProductVariantsBulkInput!]!, $productId: ID!) {
productVariantsBulkUpdate(variants: $variants, productId: $productId) {
product {
cursor
}
productVariants {
cursor
}
userErrors {
code
field
message
}
}
}
With Variables like this:
{
"variants": [
{
id: "gid://shopify/ProductVariant/39369514385591",
price: "50.00"
}
],
"productId": "gid://shopify/Product/6591908577463"
}
I'm getting this error:
Variables are invalid JSON: Unexpected token i in JSON at position 30.
It's OK for me. (with some quick tweaks)
I tweaked the request a little since the cursor is not present in the product/variant object, don't know why Shopify has not updated the example in their docs.
mutation productVariantsBulkUpdate($variants: [ProductVariantsBulkInput!]!, $productId: ID!) {
productVariantsBulkUpdate(variants: $variants, productId: $productId) {
product {
id
}
productVariants {
id
price
}
userErrors {
code
field
message
}
}
}
So try to fix the query and remove the cursor object and check if you are using the proper end-point since the bulk operation is available in the unstable version only if I'm not mistaken.
See the image below showing that the response is OK for me.

How To Query The Price Of A Product Using Shopify Storefront API and GraphQL

I'm totally new to both Shopify Storefront API AND GraphQL. I've been pulling my hair out the last 3-4 hours reading through docs and trying to find and write a query that, in my mind anyways, should be simple.
In a nutsell, I have a storefront that has certain products included on it with custom buy buttons. Each buy button's ID corresponds with the product ID on my actual shopify store, meaning ../products/5854987878567
When the page loads, I need to query my store to pull the updated prices, and then reflect those prices in the button text so the person has an accurate idea of how much they are spending.
To do this, I've pulled all unique button ID's and pushed them to a fresh Array, the idea being I can query all ID's at once using GraphQL and get one nice object back that I can then parse and update the buttons accordingly.
After hours of going in circles, I haven't gotten any closer to succeeding.
I've been reading the Query Root documentation and it seems like they do have the products connection, which I assume serves as the entry point to query all products on the store.
However under the query field itself it seems like they have every field BUT id.
available_for_sale
created_at
product_type
tag
title
updated_at
variants.price
vendor
Are all the fields they have. Since I started writing this I found sortKey, but it failed with the error "Field 'sortKey' doesn't exist on type 'QueryRoot'" sigh.
Here is my query that I'm currently rolling with. I'm not sure this idea will work because if I can only query one id at a time that'll take dozens of API calls per visitor.
$.ajax({
type : 'POST',
url : 'https://xxxxx.myshopify.com/api/2020-10/graphql.json',
headers: {
'X-Shopify-Storefront-Access-Token': 'xxxxx',
'Content-Type': 'application/graphql'
},
data: 'query products { sortKey(id: "5854987878567") }',
success: function(res) {
console.log(res)
},
error: function(status) {
console.log(status)
}
});
Any help would be appreciated!
I don't understand what you mean but the GraphQL below gets "id", "title", "description", "the first image" and "price" for each product(10 products):
{
products(first:10) {
edges {
node {
id
title
description
featuredImage {
url
}
variants(first: 1) {
edges {
node {
priceV2 {
amount
}
}
}
}
}
}
}
}
query = '''
{
productVariants(first:1,query:"title:%s")
{
edges
{
node
{
id
title
}
}
}
}
''' % title
make a function and give 'title' as a argument.
def productvariant(title):
client = shopify.GraphQL()
query = '''
{
productVariants(first:1,query:"title:%s")
{
edges
{
node
{
id
title
}
}
}
}
''' % title
you can get the price from product variant just like this - "{
productVariants(first:10, query:"{apply your condition here}") {
edges
{
node
{
storefrontId
compareAtPrice
price
}
} } } % id"

Resources