I am currently trying to get past inventory transfer history using the new “inventoryTransfers” scope from the unstable version of GraphQL on Shopify. The response is returned successfully, but the data is returned empty. I will attach the code below, so I would appreciate any advice on where the mistake is.
import requests, json
import pandas as pd
shop_creds = "shop_creds.json"
with open(shop_creds) as f:
shop_creds = json.load(f)
api_version = 'unstable'
domain = f'https://{shop_creds["shop_name"]}.myshopify.com'
endpoint = f'/admin/api/{api_version}/graphql.json'
url = domain + endpoint
headers = {
'Content-Type': 'application/json',
'X-Shopify-Access-Token': shop_creds['api_token']
}
query = """
query($after: String, $query: String) {
inventoryTransfers(first: 20, query: $query, reverse: false, after: $after) {
edges {
node {
dateCreated
destination{
address1
address2
name
}
origin{
address1
address2
name
}
lineItems(first: 50) {
edges {
node {
inventoryItem{
id
sku
unitCost{
amount
currencyCode
}
}
title
totalQuantity
}
}
}
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
start_date = "2024-11-01"
def fetch_transfers(after_cursor=None):
query_filter = f"created_at:>={start_date}"
variables = {"after": after_cursor, "query": query_filter}
payload = {
"query": query,
"variables": variables
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)
if response.status_code != 200:
raise Exception(f"Failed. Status Code: {response.status_code}, Response: {response.text}")
return response.json()
records = []
after_cursor = None
while True:
result = fetch_transfers(after_cursor)
transfers_data = result.get("data", {}).get("inventoryTransfers", {})
edges = transfers_data.get("edges", [])
for edge in edges:
node = edge.get("node", {})
created_at = node.get("dateCreated")
origin = node.get("origin", {})
destination = node.get("destination", {})
line_items = node.get("lineItems", {}).get("edges", [])
for item_edge in line_items:
item = item_edge.get("node", {})
inventory_item = item.get("inventoryItem", {})
unit_cost = inventory_item.get("unitCost", {})
records.append({
"created_at": created_at,
"origin_name": origin.get("name"),
"origin_address1": origin.get("address1"),
"origin_address2": origin.get("address2"),
"destination_name": destination.get("name"),
"destination_address1": destination.get("address1"),
"destination_address2": destination.get("address2"),
"inventory_item_id": inventory_item.get("id"),
"sku": inventory_item.get("sku"),
"unit_cost_amount": unit_cost.get("amount"),
"unit_cost_currency": unit_cost.get("currencyCode"),
"title": item.get("title"),
"quantity": item.get("totalQuantity"),
})
page_info = transfers_data.get("pageInfo", {})
if page_info.get("hasNextPage"):
after_cursor = page_info.get("endCursor")
else:
break
df = pd.DataFrame(records)
print(df.head())
"""
I’m using Jupiter notebook, Python version: 3.12.0
and, credentials is written in shop_creds.json file.