0

我想将images文档中的一个字段设置为 type 的数组[SanityImage]。我尝试将其设置为,type: 'image'但是当我查询该字段时,我得到类型是[SanityCollectionItemsImages]

images字段位于对象 ( productVariant) 中,然后包含在主文档 ( ) 中,我的字段collection中有一个数组。productVariantsitems

在我的collection文档的 fields 数组中:

...
{
  title: "Items",
  name: "items",
  type: "array",
  of: [
    {
      title: "Item",
      type: "productVariant",
    },
  ],
},
...

productVariant类型对象:

export default {
  title: "Product variant",
  name: "productVariant",
  type: "object",
  fields: [
    {
      name: "images",
      title: "Images",
      type: "array",
     of: [
       {
         type: "image",
         options: {
           hotspot: true,
         },
       },
     ],
   },
   ...

我使用 GraphQL 的查询:

allSanityCollection {
  edges {
    node {
      _id
      title
      description
      items {
        _key
        title
        images {
          asset {
            _ref
            _id
            fluid(maxHeight: 600) {
              base64
              aspectRatio
              src
              srcSet
              srcWebp
              srcSetWebp
              sizes
            }
          }
        }
      }
    }
  }
}

当我单击imagesGraphiQL Web IDE 中的字段时: GraphiQL Web IDE 屏幕截图

使用 Sanity.io HTTP API 获取整个文档:

"images": [
  {
    "_key": "5605f5383975",
    "_type": "image",
    "asset": {
      "_ref": "image-14f9b7688912499f187b7c20e57816b3cdf42c1e-4016x4688-jpg",
      "_type": "reference"
    }
  },
  ...

我的问题是如何将我的模式中的字段类型设置为显式[SanityImage]而不是,[SanityCollectionItemsImages]为什么我会得到那种奇怪的类型?

4

1 回答 1

1

Sanity 模式看起来完全正常,并且 API 提供了正确的数据结构,如您所说,因此问题似乎与 Gatsby 和 GraphQL 层有关。

我建议您确保使用最新版本的 CLI、Studio 和 Sanity Gatsby 插件,看看随着 Gatsby 源插件的开发进展迅速,这是否会使事情变得更好。

于 2019-05-14T11:39:41.640 回答