【问题标题】:Sanity reference to type comes as 'reference'对类型的健全参考作为“参考”
【发布时间】:2021-09-03 02:18:48
【问题描述】:

在理智中,我有这个字段:

{
  name: 'reference',
  type: 'reference',
  title: 'Link Reference',
  to: [
    { type: 'post' }, { type: 'questionPost' }, { type: 'linkCategory' }, { type: 'information' }
    // other types you may want to link to
  ]
},

在 CMS 中它提示为链接选择器,我选择了一个链接。然后在graphql中是:

"reference": 
{
          "_ref": "1a558cde-16fb-4362-8082-634468a1cc20",
          "_type": "reference"
},

问题是,在我的前端组件中,当我参考时。_type 通常是“post”、“questionPost”、“linkCategory”或“信息”,但在另一个页面上,reference._type 是“参考”。 你知道为什么有时 reference._type 是 'reference' 而不是 'post'、'questionPost' ...?

编辑:

所以我有一个 ctaWidget:

export default {
  name: "ctaWidget",
  type: "object",
  title: "CTA Widget",
  fields: [
    {
      name: "LeftSideText",
      type: "portableText",
      title: "Left side text"
    },
    {
      name: "text",
      type: "string",
      title: "Link text",
    },
    {
      name: 'reference',
      type: 'reference',
      title: 'Link Reference',
      to: [
        { type: 'post' }, { type: 'questionPost' }, { type: 'linkCategory' }, { type: 'information' }
        // other types you may want to link to
      ]
    },
  ],
  preview: {
  },
};

ctaWidget 是 Sanity 可移植文本 bioPortableText 的一部分:

 export default { name: "bioPortableText",
  type: "array",
  title: "Excerpt",
  of: [
    {
      type: "block",
      title: "Block",
      styles: [{ title: "Normal", value: "normal" }],
      lists: [],
      marks: {
        decorators: [
          { title: "Strong", value: "strong" },
          { title: "Emphasis", value: "em" },
          { title: "Code", value: "code" },
        ],
      },
    },
    {
      type: "mainImage",
      options: { hotspot: true },
    },
    {
      type: "mainVideo",
    },
    {
      type: "ctaWidget",
    },
    {
      type: "prosConsWidget",
    },
  ],
};

(在 CMS 中看起来像: ctaWidget in bioPortable text) (当我点击 ctaWidget 时,链接参考如下:link reference) 获取 bioPortableText 的查询是:

export const query = graphql` query peopleTemplateQuery($id: String!) {
post: sanityPeople(id: { eq: $id }) {
  id
  publishedAt
  email
  slug {
    current
  }
  name
  jobTitle
  image {
    ...SanityImage
    alt
  }
  location {
    location
  }
  _rawBio
  feeStructure
  qualification {
    qualification
  }
  specialisations {
    specialisation
  }
}
dictionary: allSanityDictionary {
  nodes {
    key
    value
  }
}

} `;

_rawBio 我这样传递给组件:

 {_rawBio && <PortableText blocks={_rawBio} />}

然后在序列化器中:

ctaWidget: ({ node }) => {
  if (node.LeftSideText === undefined) {
    return null;
  }
  else {
    return (<CtaWidget leftSideText={node.LeftSideText} linkTitle={node.text} reference={node.reference} />)
  }
},

然后在 CtaWidget 组件中 node.reference._type 应该是 'post' 或 'questionPost' 或 'linkCategory' 或 'information' 这样我就可以使用 node.reference.slug.current 来获取 url。但相反,node.reference._type 是“参考”,并且 node.reference 上没有 slug 属性...。

【问题讨论】:

  • 您是说给定完全相同的查询,reference._type 选择会得到不同的结果吗?你能分享你的完整查询吗?您通常希望在参考字段的 GraphQL 查询中使用特定类型的内联片段。也许你只需要类型?
  • 嗨,Anders Stensaas,我更新了我的帖子,提供了更多信息。我使用的 graphql 查询的唯一部分是 _rawBio 传递给 组件...

标签: graphql sanity


【解决方案1】:

我想我现在明白这个问题了。

您面临的问题是_rawX 字段不会自动解析引用。也就是说,Sanity 为您提供了一种方法来判断您希望解析参考的深度。

export const query = graphql`
  query peopleTemplateQuery($id: String!) {
    post: sanityPeople(id: { eq: $id }) {
      id
      publishedAt
      email
      slug {
        current
      }
      name
      jobTitle
      image {
        ...SanityImage
        alt
      }
      location {
        location
      }
      _rawBio(resolveReferences: { maxDepth: 5 })
      feeStructure
      qualification {
        qualification
      }
      specialisations {
        specialisation
      }
    }
    dictionary: allSanityDictionary {
      nodes {
        key
        value
      }
    }

您需要指定适合您的数据结构的深度。

文档:https://www.sanity.io/docs/gatsby-source-plugin#raw-fields

【讨论】:

  • 向 _rawBio 字段添加 (resolveReferences: {maxDepth: 50}) 解决了我的问题!很好的答案!非常感谢 Anders Stensaas!
  • 我使用了 maxDepth: 50 它也适用于 maxDepth: 2 但不能低于。再次感谢!
猜你喜欢
  • 2012-10-21
  • 2019-06-14
  • 2014-03-27
  • 2023-01-09
  • 1970-01-01
  • 2010-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多