-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphql_cost_aware_loop.py
More file actions
59 lines (47 loc) · 1.42 KB
/
Copy pathgraphql_cost_aware_loop.py
File metadata and controls
59 lines (47 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""Run many GraphQL queries in a row without tripping the rate limit.
The client sleeps on its own before a query if the last response showed the
bucket running low, so a tight loop like this doesn't need any manual
throttling code. Useful as a starting point for a bulk relabeling or
tagging script.
Usage:
export SHOPIFY_STORE_DOMAIN=my-store.myshopify.com
export SHOPIFY_ADMIN_ACCESS_TOKEN=shpat_xxx
python examples/graphql_cost_aware_loop.py
"""
import os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
from shopify_client import ShopifyGraphQLClient # noqa: E402
PRODUCT_TITLES_QUERY = """
query ProductTitles($cursor: String) {
products(first: 50, after: $cursor) {
edges {
cursor
node {
id
title
}
}
pageInfo {
hasNextPage
}
}
}
"""
def main() -> None:
shop = os.environ["SHOPIFY_STORE_DOMAIN"]
token = os.environ["SHOPIFY_ADMIN_ACCESS_TOKEN"]
client = ShopifyGraphQLClient(shop, token)
cursor = None
total = 0
while True:
payload = client.query(PRODUCT_TITLES_QUERY, {"cursor": cursor})
connection = payload["data"]["products"]
for edge in connection["edges"]:
total += 1
cursor = edge["cursor"]
if not connection["pageInfo"]["hasNextPage"]:
break
print(f"{total} product(s) scanned", file=sys.stderr)
if __name__ == "__main__":
main()