Graphql 使用gatsby transformer json手动定义模式
我有两个现有的JSON文件,一个包含位置信息,另一个包含post信息,位置和post ID将它们连接在一起:Graphql 使用gatsby transformer json手动定义模式,graphql,gatsby,Graphql,Gatsby,我有两个现有的JSON文件,一个包含位置信息,另一个包含post信息,位置和post ID将它们连接在一起: locations.json: [ { "locationId": "location-id-1", "name": "name-2", "posts": ["post-id-1", "post-id-2"] }, { "locationId": "location-id-2", "name": "name-2", "posts
locations.json
:
[
{
"locationId": "location-id-1",
"name": "name-2",
"posts": ["post-id-1", "post-id-2"]
},
{
"locationId": "location-id-2",
"name": "name-2",
"posts": ["post-id-3", "post-id-4", "post-id-5"]
},
// ...
]
posts.js
:
[
{
"postId": "post-id-1",
"title": "some-title-1",
"locationId": "location-id-1",
"text": "..."
},
{
"postId": "post-id-2",
"title": "some-title-2",
"locationId": "location-id-1",
"text": "..."
},
// ...
]
我正在使用它,它允许我使用allLocationsJson
和allPostsJson
创建GraphQL查询。但是,我不知道如何更改GraphQL模式以允许这样的查询:
{
allLocationsJson {
edges {
node {
locationId
name
posts {
postId
title
text
}
}
}
}
}
问题出现在
posts
字段中,因为GraphQL没有意识到我希望posts
是post对象的列表,而不仅仅是post id。有什么方法可以用我现有的JSON结构来完成这种查询吗?Gatsby处理以\uuuu节点结尾的特殊字段。盖茨比为这些字段创建模式时,会将它们转换为指向其他类型的链接。因此,如果将locations.json上的posts
字段重命名为posts\uuuuuu节点
,则可以根据需要查询数据