我想将文档中的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
字段时:
使用Sanity.io HTTP API获取整个文档:
"images": [
{
"_key": "5605f5383975",
"_type": "image",
"asset": {
"_ref": "image-14f9b7688912499f187b7c20e57816b3cdf42c1e-4016x4688-jpg",
"_type": "reference"
}
},
...
我的问题是如何将架构中的字段类型设置为显式[SanityImage]
而不是[SanityCollectionItemsImages]
,为什么我要获得这种奇怪的类型?
答案 0 :(得分:0)
Sanity模式看起来完全是理智的,而且API可以提供您所说的适当的数据结构,因此问题似乎与Gatsby和GraphQL层有关。
我建议您确保使用的是最新版本的CLI,Studio和Sanity Gatsby插件,看看随着Gatsby源插件开发的发展迅速,情况是否会变得更好。