GraphQL integration in Shopify apps

Sachin Gevariya


Jan 11, 2020

Project summary

Shopify App to manage the combo products. We were creating a single product by adding multiple products to that single combo product and manages the inventory when any combo product or individual product from that combo purchased by any user from any location of inventory.

Problem

We were using ShopifyAPI calls for performing create products and manage them, as ShopifyAPI has some limit to make API calls, it’s 40 requests per 2 seconds and we were making frequent requests for managing resources. And this API call limit was a headache to achieve concurrent results.

Solution

  • To Resolve this time-consuming API Calls we used GraphQL provided by Shopify. These are very lightweight response and quick also easy to structure calls with minimal required fields. With passing only those fields we require it to make calls faster than ever before and gives us freedom from API Rate Limit issue.

Solution Implementation

  • In that multiple combo_product_id  available. So we passed id to product query and after that run query on product query.
def fetch_products
  product_query(combo_product_id)
  client.query(ProductQuery)
end
  • In that, we find out variant inventory item id and also find out metafields’ information using nodes that can indicate specific data. Kernel’s client parses the query and give data to the ProductQuery object.
def product_query(id) query = <<-GRAPHQL
{
  nodes(ids: #{id}) {
       ... on Product {
        variants(first: 1){
          edges{
            node{
             id
             inventoryItem {
               id
             }
           }
         }
       }
     }
     metafields(first: 10){
       edges{
         node{
           key
           value
         }
       }
     }
   }
 }
GRAPHQL
  Kernel.const_set(:ProductQuery, client.parse(query))
end
  • For finding client

def client
  ShopifyAPI::GraphQL.new
end
Sachin Gevariya, Founder And Technical Director @ Essence Solusoft
Sachin Gevariya is a Founder and Technical Director at Essence Solusoft. He is dedicated to making the best use of modern technologies to craft end-to-end solutions. He also has a vast knowledge of Cloud management. He loves to do coding so still doing the coding. Also, help employees for quality based solutions to clients. Always eager to learn new technology and implement for best solutions.
Ruby On Rails

CONTACT US

We are open for new challenges and we help you reach your goals!