如何将架构字段类型显式设置为[SanityImage]

时间:2019-05-07 22:10:32

标签: graphql sanity

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

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

在我的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
            }
          }
        }
      }
    }
  }
}

当我单击GraphiQL Web IDE内的images字段时: GraphiQL web IDE screenshot

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

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

我的问题是如何将架构中的字段类型设置为显式[SanityImage]而不是[SanityCollectionItemsImages],为什么我要获得这种奇怪的类型?

1 个答案:

答案 0 :(得分:0)

Sanity模式看起来完全是理智的,而且API可以提供您所说的适当的数据结构,因此问题似乎与Gatsby和GraphQL层有关。

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