对类型的健全性引用是'reference'



在Sanity中,我有这个字段:

{
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"
},

问题是,在我的前端组件中,当我进行引用时_类型通常是"post"、"questionPost"、"linkCategory"或"information",但在另一个页面引用中_类型作为"reference"出现。你知道为什么有时引用吗_类型是"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中,它看起来像:bioPortable文本中的ctaWidget((当我点击ctaWidget时,链接引用看起来像:链接引用(获取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是"reference",node.reeference上没有slug属性…

您面临的问题是_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-字段。

相关内容

最新更新