GraphQL、Shopify获取所有产品或产品总数

GraphQL、Shopify获取所有产品或产品总数,graphql,shopify,Graphql,Shopify,我是graphQL的新手,我必须检索所有的产品。我环顾四周,观看教程和阅读,人们似乎返回了他们想要的所有特定类型的数据,但我不能。例如,它会抛出一个错误,说您必须提供第一个或最后一个,但我需要它们全部,或者它会说totalCount或count不存在 { products { id title price } } // or get total count of products { products { totalCount }

我是graphQL的新手,我必须检索所有的产品。我环顾四周,观看教程和阅读,人们似乎返回了他们想要的所有特定类型的数据,但我不能。例如,它会抛出一个错误,说您必须提供第一个或最后一个,但我需要它们全部,或者它会说totalCount或count不存在

 {
  products {
    id
    title
    price
    
  }
}

// or get total count of products

 {
  products {
   totalCount
  }
}
我基本上是想做这样的事情。我知道我可能得不到任何帮助,因为没有人可以访问我的shopify管理api,甚至可能是一个示例或我能看到的任何东西。这是我的产品选项的graphQL查询根目录。 产品清单

ProductConnection!
first: Int
Returns up to the first n elements from the list.

after: String
Returns the elements that come after the specified cursor.

last: Int
Returns up to the last n elements from the list.

before: String
Returns the elements that come before the specified cursor.

reverse: Boolean = false
Reverse the order of the underlying list.

sortKey: ProductSortKeys = ID
Sort the underlying list by the given key.

query: String
Supported filter parameters:

barcode
created_at
delivery_profile_id
error_feedback
gift_card
inventory_total
is_price_reduced
out_of_stock_somewhere
price
product_type
publishable_status
published_status
sku
status
tag
title
updated_at
vendor
See the detailed search syntax for more information about using filters.

savedSearchId: ID
ID of an existing saved search. The search’s query string is used as the query argument.

您无法使用店面GraphQL或管理GraphQL通过单个GraphQL请求获取所有产品

如果您的产品低于250,则可以使用
first:250
发出请求,但如果您的产品超过250,则需要使用GraphQL的光标分页进行递归请求。因此,如果您有1000个产品,您将需要发出4个请求(取决于您使用的API,storefront或admin graphql API,因为它们是不同的)

目前,无法通过Shopify提供的任何API使用单个请求获取所有产品

实现此目的的唯一方法是使用以下代码创建自定义模板:

[
{% paginate collection.products by 9999 %}
  {% for product in collection.products %}
    {{product | json}}{% unless forloop.last %},{% endunless %}
  {% endfor %}
{% endpagination %}
]
将其称为类似于
collection.ajax.liquid

并使用view参数向其发出获取请求:

fetch('/collections/all?view=ajax').then((response) => handle the response)
请记住,您拥有的产品越多,请求该页面的时间就越长。如果您有1000种产品,则请求最多需要10秒钟。因此,对于大量产品来说,这也不是一个很好的解决方案

ProductConnection!
first: Int
Returns up to the first n elements from the list.

after: String
Returns the elements that come after the specified cursor.

last: Int
Returns up to the last n elements from the list.

before: String
Returns the elements that come before the specified cursor.

reverse: Boolean = false
Reverse the order of the underlying list.

sortKey: ProductSortKeys = ID
Sort the underlying list by the given key.

query: String
Supported filter parameters:

barcode
created_at
delivery_profile_id
error_feedback
gift_card
inventory_total
is_price_reduced
out_of_stock_somewhere
price
product_type
publishable_status
published_status
sku
status
tag
title
updated_at
vendor
See the detailed search syntax for more information about using filters.

savedSearchId: ID
ID of an existing saved search. The search’s query string is used as the query argument.

至于总计数,对于该
{{collection.all_products_count}}
有一个液体对象,或者如果您正在做管理工作,请使用rest api,因为有一个方法可以获取产品计数,但graphql中没有