在GatsbyJS中从Shopify查询多个集合

时间:2020-08-21 19:57:53

标签: graphql gatsby

我想使用gatsby-source-shopify

根据与之相关的收藏集显示Shopify产品组

通过过滤可从一个集合中获取所有产品,就像运行此查询一样简单:

  const { allShopifyCollection } = useStaticQuery(
    graphql`
      query {
        allShopifyCollection(filter: {id: {in: "Shopify__Collection__Z2lkOi8vc2hvcGlmeS9Db2xsZWN0aW9uLzE3NzAxMjY3MDQ5OA=="}}) {
          edges {
            node {
              products {
                title
              }
            }
          }
        }
    `
  )

但是,据我所知,不可能对同一组件中的相同数据类型进行多次查询。

解决此问题的首选方法是什么?

  • 使用多个组件来获取每个集合的数据,并 传递给网格组件?

  • 获取所有集合并过滤出每个集合?

  • 另一种解决方案?

1 个答案:

答案 0 :(得分:2)

您可以使用query aliases吗?

const { allShopifyCollection } = useStaticQuery(
  graphql`
    query {
      collection1: allShopifyCollection(filter: {id: {in: "Shopify__Collection__Z2lkOi8vc2hvcGlmeS9Db2xsZWN0aW9uLzE3NzAxMjY3MDQ5OA=="}}) {
        edges {
          node {
            products {
              title
            }
          }
        }
      }

      collection2: allShopifyCollection(filter: {id: {in: "Shopify__Collection__someOtherCollection"}}) {
        edges {
          node {
            products {
              title
            }
          }
        }
      }

      collection3: allShopifyCollection(filter: {id: {in: "Shopify__Collection__yetAnotherCollection"}}) {
        edges {
          node {
            products {
              title
            }
          }
        }
      }
  `
)