Gatsby搭配gatsby-source-strapi出现字段类型冲突数据为空问题求助
解决方案
核心问题原因
你当前类型定义中错误将非独立节点的嵌套字段声明为了Gatsby Node类型:只有Strapi的独立内容类型(集合类型/单例类型)会被gatsby-source-strapi处理为Gatsby Node,若certification是嵌套组件、重复字段等非独立内容结构,声明implements Node后Gatsby会尝试从全局Node集合中匹配对应id的节点,匹配不到就会返回全量null。
对应修改方案
根据你的实际数据结构选择对应方案即可:
方案一:certification为非独立嵌套结构
直接移除implements Node和@dontInfer指令,声明为普通对象类型:
exports.createSchemaCustomization = ({ actions }) => { const { createTypes } = actions const typeDefs = ` type StrapiLandingPages implements Node { programmes: [StrapiLandingPagesProgrammes] } type StrapiLandingPagesProgrammes { certification: StrapiLandingPagesProgrammesCertification } type StrapiLandingPagesProgrammesCertification { id: Int! Title: String slugUid: String sectionTitle: String content: String published_at: Date @dateformat created_at: Date @dateformat updated_at: Date @dateformat } ` createTypes(typeDefs) }
方案二:certification为Strapi独立内容类型(关联关系)
需添加@link指令指定关联字段,同时类型名要和Strapi生成的独立节点类型名保持一致:
exports.createSchemaCustomization = ({ actions }) => { const { createTypes } = actions const typeDefs = ` type StrapiLandingPages implements Node { programmes: [StrapiLandingPagesProgrammes] } type StrapiLandingPagesProgrammes { certification: StrapiCertification @link(by: "id") } type StrapiCertification implements Node @dontInfer { id: Int! Title: String slugUid: String sectionTitle: String content: String published_at: Date @dateformat created_at: Date @dateformat updated_at: Date @dateformat } ` createTypes(typeDefs) }
生效操作
修改完成后执行gatsby clean清除本地缓存,再重启开发服务即可看到完整数据。
内容的提问来源于stack exchange,提问作者Marko
相关产品推荐
相关产品推荐

