我有一个用 Strapi 制作的模型,其中包含可用于添加社交媒体链接的特定组件。每个链接都包含一个文本字段和一个链接字段。一切都按预期进行,除非我将其留空。如果有 0 个链接,我会收到如下所示的错误。
Gatsby GraphQL 尝试访问链接:
strapiWebsiteSetting {
footerSocialLinks {
text
link
}
footerOtherLinks {
text
link
}
}
有没有办法让 GraphQL 即使添加了 0 个链接也能工作。我试过了
将以下代码添加到 gatsby-node.js
但不起作用:
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions;
const typeDefs = `
type STRAPI__COMPONENT_LINK_FOOTER_OTHER_LINK implements Node {
id: ID!
parent: Node
children: [Node!]!
internal: Internal!
text: String
link: String
strapi_id: Int
}
type STRAPI__COMPONENT_LINK_FOOTER_SOCIAL_MEDIA_LINK implements Node {
id: ID!
parent: Node
children: [Node!]!
internal: Internal!
text: String
link: String
strapi_id: Int
}
`;
createTypes(typeDefs);
};
最佳答案
让 gatsby 和 Strapi 使用可选属性甚至可选记录的一种不太痛苦的方法如下:
- 安装
gatsby-plugin-schema-snapshot
插件。 - 在 gatsby-config.js 中将“update”选项设置为“true”(也可以通过环境变量处理):
{
resolve: `gatsby-plugin-schema-snapshot`,
options: {
update: true,
},
}
- 使用具有至少一条记录(例如虚拟记录)和所有属性的所有内容类型构建您的应用。这会在应用文件夹的根目录中创建一个 schema.gql 文件。
- 将插件的“更新”选项设置为“false”并删除您的虚拟记录。
- 再次构建。不再提示:)
关于graphql - Gatsby 和 Strapi 可选数据的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72141333/