Presentment price support in new GraphQL Product APIs

Solved

Presentment price support in new GraphQL Product APIs

tejasvyas
Shopify Partner
10 0 2

Hello, We want to share that we are using presentment-prices on Rest product API to get the currency prices, As we need to move to new GraphQL APIs. what is alternate in this new GraphQL product APIs to fetch this data?

 

Regards

Tejas

Accepted Solution (1)

AlexAckerman
Shopify Staff
2 1 1

This is an accepted solution.

Hi @tejasvyas ,

To achieve this in Admin GraphQL API, you can use the contextualPricing field on ProductVariant or Product types.

- Product docs

- Variant docs 


To achieve this in the Storefront API, you can use the @inContext directive - Docs

 

One thing to note is that you'll now need to pass country code as the context input argument, rather than currency code. The reason is that you can have multiple countries that share the same currency (for example France and Spain), but that have different final prices. You'll be able to see the currency code off of the price. Hope that helps!

 

Example queries:

 

Admin

query ProductVariants {
  productVariants(first: 10) {
    nodes {
      id
      contextualPricing(context: {
        country: CA
      }) {
        price { amount currencyCode }
        compareAtPrice { amount currencyCode }
      }
    }
  }
}

query Products {
  products(first: 10) {
    nodes {
      id
      contextualPricing(context: {
        country: CA
      }) {
        minVariantPricing {
          price { amount currencyCode }
        }
        maxVariantPricing {
          price { amount currencyCode }
        }
      }
    }
  }
}

 

Storefront

query Products @inContext(country: CA)  {
  products(first: 10) {
    nodes {
      id
      priceRange {
        minVariantPrice { amount currencyCode }
        maxVariantPrice { amount currencyCode }
      }
      variants(first: 10) {
        nodes {
          id
          price { amount currencyCode }
          compareAtPrice { amount currencyCode }
        }
      }
    }
  }
}

 

View solution in original post

Replies 3 (3)

AlexAckerman
Shopify Staff
2 1 1

This is an accepted solution.

Hi @tejasvyas ,

To achieve this in Admin GraphQL API, you can use the contextualPricing field on ProductVariant or Product types.

- Product docs

- Variant docs 


To achieve this in the Storefront API, you can use the @inContext directive - Docs

 

One thing to note is that you'll now need to pass country code as the context input argument, rather than currency code. The reason is that you can have multiple countries that share the same currency (for example France and Spain), but that have different final prices. You'll be able to see the currency code off of the price. Hope that helps!

 

Example queries:

 

Admin

query ProductVariants {
  productVariants(first: 10) {
    nodes {
      id
      contextualPricing(context: {
        country: CA
      }) {
        price { amount currencyCode }
        compareAtPrice { amount currencyCode }
      }
    }
  }
}

query Products {
  products(first: 10) {
    nodes {
      id
      contextualPricing(context: {
        country: CA
      }) {
        minVariantPricing {
          price { amount currencyCode }
        }
        maxVariantPricing {
          price { amount currencyCode }
        }
      }
    }
  }
}

 

Storefront

query Products @inContext(country: CA)  {
  products(first: 10) {
    nodes {
      id
      priceRange {
        minVariantPrice { amount currencyCode }
        maxVariantPrice { amount currencyCode }
      }
      variants(first: 10) {
        nodes {
          id
          price { amount currencyCode }
          compareAtPrice { amount currencyCode }
        }
      }
    }
  }
}

 

tejasvyas
Shopify Partner
10 0 2

Thanks @AlexAckerman But the issue is, We need to execute the API call for each context which will lead to lot of API calls and delay in fetching the data when there is large Catalog and lot of context available. 
We need a solution which works like in rest API i.e. we execute one API call which will return all the currency prices with product data. 

AlexAckerman
Shopify Staff
2 1 1

Thanks for that feedback. One workaround could be to use GraphQL aliases to fetch multiple prices for different countries in the same request. This approach is more expensive from a query cost perspective, so depending on how many countries you have the query would need to be broken up. 

query ProductVariant {
  productVariants(first: 10) {
    nodes {
      id
      caPrices: contextualPricing(context: {
        country: CA
      }) {
        price { amount currencyCode }
        compareAtPrice { amount currencyCode }
      }
      usPrices: contextualPricing(context: {
        country: US
      }) {
        price { amount currencyCode }
        compareAtPrice { amount currencyCode }
      }
    }
  }
}