GraphQL查询访问子目录下文件夹中的所有已转换json文件 [英] GraphQL Query access all transformed json files within a folder located in subdirectories
问题描述
我的项目结构如下:
-$PROJECT
--src
---data
----projects
-----project1
------project.json
------images
-------project1-preview.png
-----project2
------project.json
-------images
...
依此类推,对于许多项目而言.我可以在graphQl中使用allProjectsJson
将这些project.json文件命名为项目标题时,并在projects
文件夹中,但是现在它们位于projects
的子文件夹中.我只能作为allProject1Json单独查询它们,依此类推.有没有一种查询allProjectsJson的方法,所以我可以获得所有的project.json文件?
And so on, for however many projects. I could query these project.json files when they were named the title of the project and within a projects
folder using allProjectsJson
in graphQl, however now they are within subfolders within projects
. I can only query them individually as allProject1Json and so on. Is there a way to query allProjectsJson so I get all the project.json files?
我可以通过使用json文件过滤器查询allFile
来找到我的项目文件,但是,这些文件不是json转换的,所以我无法访问元素.
I can find my projects files by querying allFile
with a filter for json files, however, these files are not transformed json so I can't access the elements.
在我的gatsby-config文件中,我正在导入src/data作为文件源.
In my gatsby-config file I am importing src/data as a source for files.
推荐答案
从我在
啊,我明白你的意思了:)
幸运的是,gatsby-transformer-json
具有一个插件选项,可以帮助您轻松解决问题!
Ahh, I see what you mean :)
luckily gatsby-transformer-json
has a plugin option that will help get you unstuck!
这是 typeName 选项.您应该能够检查每个JSON节点上的字段,并将其用作类型名称.像这样:
It's the typeName option. You should be able to check for a field on each JSON node, and use that as the type name. Something like this:
{
resolve: `gatsby-transformer-json`,
options: {
typeName: ({ node, object, isArray }) =>
object.project ? `Project` : `Json`,
},
},
这样,定义了字段project
的所有内容都将显示为allProject { ... }
,其他所有json文件将显示为allJson { ... }
That way anything with the field project
defined will show up as allProject { ... }
, where any other json files will show up as allJson { ... }
{
allProject {
nodes {
project
}
}
}
{
"data": {
"allProject": {
"nodes": [
{
"project": "project1"
},
{
"project": "project2"
}
]
}
}
}
这篇关于GraphQL查询访问子目录下文件夹中的所有已转换json文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!