我正在尝试通过基于标签的过滤从shopify获取产品。标签将是动态的,不止一个,并且会发生变化。
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
# }
# }
# }
# }"""
我得到的输出基本上是一个JSON,产品为空
{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": []}}
我无法在查询中将变量作为变量传递。我目前正在使用GraphQl,因为我找不到基于会变化的多个标签的REST API获取产品。
编辑:删除了Python标记,因为这不是python问题,我添加了答案以及关于如何执行此操作的两个方法
我终于找到了我自己的问题的答案->
(
和逗号的行连续属性在其中添加变量,
。graphQLquery1 = """ {
products(first:100, query:"tag:[%s, %s]") {
edges {
node {
id
tags
title
onlineStoreUrl
}
}
}
}"""%('3+', 'personal-social')
data = None
resp = callShopifyGraphQL(graphQLquery1, data)
但是,这不是在GraphQl查询中使用变量的方式。
graphQLquery2 = """ query($tags: String){
products(first:100, query:$tags) {
edges {
node {
id
tags
title
onlineStoreUrl
}
}
}
}"""
str1 = '0-12'
str2 = 'physical'
tags = dict({
"tags":[str1,str2]
})
resp = callShopifyGraphQL(graphQLquery2, tags)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句