【问题标题】:How to explicitly set schema field type to [SanityImage]如何将架构字段类型显式设置为 [SanityImage]
【发布时间】:2019-09-25 14:48:56
【问题描述】:

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

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

在我的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],为什么我会得到那种奇怪的类型?

【问题讨论】:

  • 这个你搞定了吗?
  • 很遗憾没有。我也在他们的理智的松弛频道中问过,但没有运气。我切换到棱镜,一切看起来都更简单了

标签: graphql sanity


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多