【发布时间】:2018-07-28 11:26:17
【问题描述】:
我有两个现有的 JSON 文件,一个包含位置信息,另一个包含帖子信息,其中包含位置和帖子 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": ["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": "..."
},
// ...
]
我正在使用gatsby-transformer-json,它允许我使用allLocationsJson 和allPostsJson 创建GraphQL 查询。但是,我不知道如何更改 GraphQL 架构以允许这样的查询:
{
allLocationsJson {
edges {
node {
locationId
name
posts {
postId
title
text
}
}
}
}
}
问题在于posts 字段,因为GraphQL 没有意识到我希望posts 成为帖子对象的列表,而不仅仅是帖子ID。我有什么办法可以用我现有的 JSON 结构完成这种查询吗?
【问题讨论】: