How to fetch shopify products based on multiple varying tags which are given dynamically in python?

Highlighted
Tourist
5 1 1

I am trying to fetch products from shopify by filtering based on tags. Tags will be dynamic, more than one, and will change.

import json
import time
import requests

API_KEY = 'xxxx'
PASSWORD = 'xxxx'
SHOP_NAME = 'xxxx'
API_VERSION = '2020-04' #change to the API version
shop_url = "https://%s:%s@%s.myshopify.com/admin/api/%s" % (API_KEY, PASSWORD, SHOP_NAME, API_VERSION)

def callShopifyGraphQL(GraphQLString, data):
    headers = {
        "X-Shopify-Storefront-Access-Token": 'xxxxxx',
        "accept":"application/json"      
    }
    response = requests.post(shop_url+'/graphql', json={'query': GraphQLString, 'variables': data}, headers=headers)
    answer = json.loads(response.text)
    return answer['data']

str1 = '0-12'
str2 = 'physical'

graphQLquery7 = """ {
  products(first:100, query:"tag:$tags") {
    edges {
      node {
        id
        tags
        title
        onlineStoreUrl
      }
    }
  }
}"""

tag = dict({
  "tags":[str1,str2]
})

resp = callShopifyGraphQL(graphQLquery7, tag)
print json.dumps(resp)


# This query works fine and gives multiple products
# graphQLquery2 = """{
#   products(first:100, query:"tag:[0-12, physical]") {
#     edges {
#       cursor
#       node {
#         id
#         tags
        
#         title
#         onlineStoreUrl
#       }
#     }
#   }
# }"""

The Output that I am getting is basically a JSON with products empty

{u'extensions': {u'cost': {u'requestedQueryCost': 102, u'throttleStatus': {u'restoreRate': 50.0, u'currentlyAvailable': 998, u'maximumAvailable': 1000.0}, u'actualQueryCost': 2}}, u'data': {u'products': {u'edges': []}}}
{"products": {"edges": []}}

I am unable to pass my tags as a variable in the query. I am currently using GraphQl because I couldn't find REST APIs fetch product based on multiple tags which would vary.

 

0 Likes
Highlighted
Explorer
41 9 10

Hey Hey, 

have a go at changing your query to the below. 
looks like your variables are correct so you can just rejig your GraphQL a little.

 

  
query($tags: String){
products(first:100, query:$tags) { edges { node { id tags title onlineStoreUrl } } }
}

 further reference to GraphQL queries in Shopify can be found here:

https://community.shopify.com/c/Shopify-APIs-SDKs/Graphiql-product-queries-with-variables/td-p/58259...

 

 

0 Likes
Highlighted
Tourist
5 1 1

Query works fine using your suggestion. However, I am receiving incorrect results. I am reciving products who have <"tags": []> and some products who don't have any of the specified tags. Neither AND nor OR condition is satisified in the complete result set.  

Here is what I receive - 

{
  "products": {
    "edges": [
      {
        "node": {
          "onlineStoreUrl": "https://kinderpass-wobbl-more.myshopify.com/products/babbl-black-white-cards",
          "title": "Babbl - Black & White Cards (set of 10) 0m+",
          "id": "gid://shopify/Product/4391313342545",
          "tags": [
            "0-12",
            "art cards",
            "black and white cards",
            "flash cards",
            "gifts for babies",
            "learning cards",
            "physical"
          ]
        }
      },
      {
        "node": {
          "onlineStoreUrl": null,
          "title": "Babbl - World Around Me- set of 10 flash cards (6m+)",
          "id": "gid://shopify/Product/4391401554001",
          "tags": []
        }
      },
      {
        "node": {
          "onlineStoreUrl": null,
          "title": "Babbl- What I see and feel (set of 20 Gorgeous Image cards) 6m+",
          "id": "gid://shopify/Product/4391404011601",
          "tags": [
            "early learning",
            "flashcards",
            "gift for babies",
            "infant gifts",
            "learning cards"
          ]
        }
      },
      {
        "node": {
          "onlineStoreUrl": "https://kinderpass-wobbl-more.myshopify.com/products/babbl-art-cards-for-baby-vision-vocabulary-builder",
          "title": "Babbl- Ultimate Collection of Art cards for baby (set of 30) 0+",
          "id": "gid://shopify/Product/4391406141521",
          "tags": [
            "0-12",
            "12-24",
            "cognitive",
            "early learning",
            "flashcards",
            "gift for babies",
            "gift ideas",
            "infant gifts",
            "linguistic"
          ]
        }
      },
      {
        "node": {
          "onlineStoreUrl": "https://kinderpass-wobbl-more.myshopify.com/products/wobbl-vip-edition",
          "title": "Wobbl- 3in1 developmental toy",
          "id": "gid://shopify/Product/4391525613649",
          "tags": [
            "0-12",
            "physical",
            "rattle",
            "teether",
            "toys",
            "tummy time toy"
          ]
        }
      },
      {
        "node": {
          "onlineStoreUrl": "https://kinderpass-wobbl-more.myshopify.com/products/wobbl-rattl",
          "title": "Wobbl + Rattl - perfect gift set for newborn & baby",
          "id": "gid://shopify/Product/4391906967633",
          "tags": [
            "gift for babies",
            "gift ideas",
            "gifts for babies",
            "infant gifts",
            "physical",
            "rattle",
            "teether",
            "toys",
            "tummy time toy"
          ]
        }
      },
      {
        "node": {
          "onlineStoreUrl": "https://kinderpass-wobbl-more.myshopify.com/products/wobbl-babbl-bundle-of-fun",
          "title": "Wobbl + Babbl: Bundle of fun | gift for 0-1 yr baby",
          "id": "gid://shopify/Product/4403541966929",
          "tags": [
            "flashcards",
            "gift for babies",
            "infant gifts",
            "physical",
            "rattle",
            "teether",
            "toys",
            "tummy time toy"
          ]
        }
      },
      {
        "node": {
          "onlineStoreUrl": "https://kinderpass-wobbl-more.myshopify.com/products/kinder-camp-5-morning-sessions",
          "title": "KINDER CAMP- 5  Sessions",
          "id": "gid://shopify/Product/4593921982545",
          "tags": [
            "3+",
            "early learning",
            "linguistic",
            "personal-social"
          ]
        }
      },
      {
        "node": {
          "onlineStoreUrl": "https://kinderpass-wobbl-more.myshopify.com/products/kinder-camp-10-sessions",
          "title": "KINDER CAMP- 10  Sessions",
          "id": "gid://shopify/Product/4593927290961",
          "tags": [
            "3+",
            "cognitive",
            "linguistic",
            "personal-social"
          ]
        }
      },
      {
        "node": {
          "onlineStoreUrl": null,
          "title": "Play Safe Bundle - KinderPass Lifetime subscription + 2 masks",
          "id": "gid://shopify/Product/4625446338641",
          "tags": []
        }
      },
      {
        "node": {
          "onlineStoreUrl": "https://kinderpass-wobbl-more.myshopify.com/products/play-safe-set-of-2-kids-masks",
          "title": "Playsafe- set of 2 reusable child masks",
          "id": "gid://shopify/Product/4625656184913",
          "tags": [
            "3+",
            "cotton",
            "personal-social"
          ]
        }
      },
      {
        "node": {
          "onlineStoreUrl": "https://kinderpass-wobbl-more.myshopify.com/products/ready-to-read-phonics-level-1",
          "title": "Ready to Read- Phonics Level 1",
          "id": "gid://shopify/Product/4628373438545",
          "tags": [
            "3+",
            "cognitive",
            "early learning",
            "linguistic",
            "personal-social"
          ]
        }
      }
    ]
  }
}

I am unable to figure out my mistake in the query it self

0 Likes