能够在 Shopify 中通过 GraphQL 检索多个元字段

问题描述 投票:0回答:1

我为博客文章创建了一些元字段并填充了它们。我正在构建一个 Hydrogen Shopify 店面并使用 GraphQL 来获取该博客/文章数据。但我似乎无法弄清楚如何获取所有元字段。似乎“元字段”和“元字段”仅返回基于键和命名空间的单个字段。有没有办法可以多一个?

query Blog($language: LanguageCode, $blogHandle: String!, $first: Int, $last: Int, $startCursor: String, $endCursor: String) @inContext(language: $language) {
  blog(handle: $blogHandle) {
    title
    seo {
      title
      description
    }
    articles(
      first: $first
      last: $last
      before: $startCursor
      after: $endCursor
      reverse: true
    ) {
      nodes {
        ...ArticleItem
        metafields(identifiers: {namespace: "custom", key: "misc_content_long_text_1"}) {
          value
          references {
            edges {
              node
            }
          }
        }
      }
      pageInfo {
        hasPreviousPage
        hasNextPage
        hasNextPage
        endCursor
        startCursor
      }
    }
  }
}

fragment ArticleItem on Article {
  author: authorV2 {
    name
  }
  contentHtml
  handle
  id
  image {
    id
    altText
    url
    width
    height
  }
  publishedAt
  title
  blog {
    handle
  }
}

我的元字段包括:

  • misc_content_long_text_1
  • misc_content_long_text_2
  • misc_content_long_text_3
  • misc_text_field_1
  • misc_text_field_2
  • misc_text_field_3
  • misc_text_field_4
  • misc_text_field_5

我希望我不必调用多个查询来获取这些。 TIA

graphql shopify shopify-hydrogen
1个回答
0
投票

您可以通过为元字段添加别名来实现这一点。这是官方文档https://www.shopify.com/in/partners/blog/how-to-use-graphql-aliases

query Blog($language: LanguageCode, $blogHandle: String!, $first: Int, $last: Int, $startCursor: String, $endCursor: String) @inContext(language: $language) {
  blog(handle: $blogHandle) {
    title
    seo {
      title
      description
    }
    articles(
      first: $first
      last: $last
      before: $startCursor
      after: $endCursor
      reverse: true
    ) {
      nodes {
        ...ArticleItem
        misc_content_long_text_1 : metafields(identifiers: {namespace: "custom", key: "misc_content_long_text_1"}) {
          value
          references {
            edges {
              node
            }
          }
        },
misc_text_field_2: metafields(identifiers: {namespace: "custom", key: "misc_text_field_2"}) {
          value
          references {
            edges {
              node
            }
          }
        }
      }
      pageInfo {
        hasPreviousPage
        hasNextPage
        hasNextPage
        endCursor
        startCursor
      }
    }
  }
}

fragment ArticleItem on Article {
  author: authorV2 {
    name
  }
  contentHtml
  handle
  id
  image {
    id
    altText
    url
    width
    height
  }
  publishedAt
  title
  blog {
    handle
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.