Gatsby createTypes 不使用 File 创建 localFile

Posted

技术标签:

【中文标题】Gatsby createTypes 不使用 File 创建 localFile【英文标题】:Gatsby createTypes don't create localFile using File 【发布时间】:2022-01-09 00:29:06 【问题描述】:

当 graphql 中有一个为空的字段时,我在运行 Gatsby 时遇到了问题(在 cms 中,有时该字段为空,有时不是)。我设法通过 createTypes 添加 gatsby-node.js 来解决这个问题。不幸的是,就像使用字符串类型的文本工作正常一样,所以对于使用文件类型子字段 localFile 的照片没有创建。即使没有照片,我也需要 localFile 出现,与添加照片的方式相同。有谁知道如何解决这个问题。非常感谢您的帮助。

gatsby-node.js

exports.sourceNodes = ( actions ) => 
 const  createTypes  = actions
 const typeDefs = ` type StrapiPageEstimateRealizations implements Node  
     Title: String
     Description: String
     Img: File
 

` 
createTypes(typeDefs) 

graphql when the photo field is empty

graphql when a picture is added in cms

【问题讨论】:

【参考方案1】:

File 不是本机类型,因此根据上下文,您需要添加自定义类型。

尝试定义您的自定义类型,例如:

type LocalFile 
  localFile: File @link(from: "localFile___NODE")

然后:

 const typeDefs = ` type StrapiPageEstimateRealizations implements Node  
     Title: String
     Description: String
     Img: LocalFile 
 

有用的资源:

https://github.com/gatsbyjs/gatsby/issues/29100

【讨论】:

谢谢,这对我很有用!

以上是关于Gatsby createTypes 不使用 File 创建 localFile的主要内容,如果未能解决你的问题,请参考以下文章