GraphQL Query for all archived products not returning all items.

BeamJokerFavor
Excursionist
47 2 3

*edit, this works, just needed to restart my Python session. This is a good Python template in case you need it. 

I am having problem with my function that tries to list all archived products (snowflakes).

I am looking at an archived product, and I have its gid. But it is not returning in this query. I am getting a list of 803 items, but some are not included.  I am using the 2021-07 API.

 

 

 

def list_all_archived_snowflakes(shopify):
  url = form_graphql_url()
  header = {
        'Content-Type': 'application/json',
        'X-Shopify-Access_Token' :  shopify.StorefrontAccessToken.headers['X-Shopify-Access-Token']
  }
  query = '''
  query  FindArchivedSnowflakes($query: String!, $cursor: String) {
      products(first: 250, after: $cursor, query: $query){
        edges {
          cursor
          node {
            id
          }
        }
      }
    }
  '''

  variables = {
    "query": "status:Archived",
  }
  r = graphql_post(url, query, variables, header)
  my_json = json.loads(r.text)
  #print('json: {}'.format(my_json))
  d = my_json['data']
  d = d['products']
  edges = d['edges']

  #find cursor
  d = edges[-1]
  cursor = d['cursor']
  #print(cursor)

  #find ids
  these_ids = [edge['node']['id'] for edge in edges]
  #print(these_ids)

  def find_recursive_archived(cursor, list, url, query, variables, header):
    variables_2 = {
        "query": 'status:Archived',
        "cursor": '{}'.format(cursor)
    } 
    r = graphql_post(url, query, variables_2, header)
    my_json = json.loads(r.text)
    #print('\n \n')
    #print(my_json)
    d = my_json['data']
    d = d['products']
    edges = d['edges']

    #find cursor
    

    #find ids
    these_ids_r = [edge['node']['id'] for edge in edges]
    
    #add these ids to the list
    [list.append(id) for id in these_ids_r]
    #if cursor is empty, you are done
    try:
      e = edges[-1]
      cursor = e['cursor']
      #print(cursor)
    except IndexError:
      return list
    sleep_timer = 4.5
    print("sleeping {} seconds".format(sleep_timer))
    sleep(sleep_timer)
    return find_recursive_archived(cursor, list, url, query, variables_2, header)
  return find_recursive_archived(cursor, these_ids, url, query, variables, header)

 

 

 

  

Replies 0 (0)