Have your say in Community Polls: What was/is your greatest motivation to start your own business?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Presentment price support in new GraphQL Product APIs

Solved

Presentment price support in new GraphQL Product APIs

tejasvyas
Shopify Partner
11 0 5

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
4 1 4

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
4 1 4

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
11 0 5

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
4 1 4

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 }
      }
    }
  }
}