Search code examples
rubyshopify

How to decrease latency when getting product.metafields through the shopify Ruby gem?


Consider the following lines that interacts with the shopify Ruby gem.

First we fetch a bunch of products through Shopify's REST API.

products = ShopifyAPI::Product.find(:all, :params => {})
product  = products.first

Then we try getting the metafields of a product.

puts Benchmark.measure { 100.times { |s| product.metafields } }
# => 0.375952   0.083343   0.459295 ( 26.157793)

Any idea why getting metafields takes this long time? As far as I understand they are part of the product object and does not require a separate API request.


Solution

  • Forget using Rest API for this. Instead, switch to GraphQL. Like magic, your product query can now ask for and extract metafields. Wonderful! What progress! Use RestAPI only when you need basic information not otherwise serviced by GraphQL as they are not at parity.