【问题标题】:GraphQL, Shopify Get All products or totalcount of productsGraphQL,Shopify 获取所有产品或产品总数
【发布时间】:2021-02-13 13:28:25
【问题描述】:

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

 {
  products {
    id
    title
    price
    
  }
}

// or get total count of products

 {
  products {
   totalCount
  }
}

我基本上是在尝试做类似的事情。我知道我可能不会得到任何帮助,因为没有人可以访问我的 shopify admin 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 shopify


    【解决方案1】:

    您无法使用 StoreFront GraphQL 或 Admin GraphQL 通过单个 GraphQL 请求获取所有产品。

    如果您的产品少于 250 个,您可以使用 first: 250 发出请求,但如果您的产品超过 250 个,则需要使用 GraphQL 的光标分页进行递归请求。因此,如果您有 1000 个产品,您将需要发出 4 个请求(取决于您使用的 API、店面或管理 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

    并使用视图参数向它发出获取请求:

    fetch('/collections/all?view=ajax').then((response) => handle the response)
    

    请记住,您拥有的产品越多,对该页面的请求就越长。如果您有 1000 个产品,则请求最多可能需要 10 秒。因此,对于大量产品而言,这也不是一个很好的解决方案。


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

    【讨论】:

      【解决方案2】:

      您可以使用BULK API 结合 GRAPHQL 获取所有产品

      【讨论】:

        【解决方案3】:

        GraphQL 的新手,但我的理解是,边缘呈现与节点相关的信息。

        与您在关系 ManyToMany 关系中所做的非常相似,能够添加额外的列,其中包含有关两条记录之间关系的信息,但不存储在任何这些记录中。

        例如:一个用户喜欢一个帖子,是属于关系的信息。

        取自文档:

        如果存在特定于边缘而不是对象之一的信息,则边缘的概念也被证明是有用的。例如,如果我们想在 API 中公开“友谊时间”,将其放在边缘是很自然的放置位置。 https://graphql.org/learn/pagination/

        【讨论】:

          猜你喜欢
          • 2020-09-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-03-18
          • 1970-01-01
          • 2020-10-31
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多