rest - 如何在解析器graphql中获取祖父母数据?
问题描述
我是 GraphQL 的新手。我正在用 GraphQL 包装一个 REST API,我的问题是我没有得到餐厅菜肴的成分(祖父母),只是得到菜肴的成分(父母)。这是我的模式
type Restaurant{
id_restaurant: String
name: String
dishes:[Dish]
}
type Dish{
id_dish: String
name: String
ingredients:[Ingredient]
}
type Ingredient{
id_ingredient: String
name: String
}
type Query{
restaurants: [Restaurant]
dishes: [Dish]
ingredients: [Ingredient]
}
这些是我的解析器
const mainURL = `https://my_apy_example`
const resolvers = {
Query: {
restaurants: () => { return fetch(`${mainURL}/restaurants`).then(res => res.json()) },
dishes: () => { return fetch(`${mainURL}/dishes`).then(res => res.json()) },
ingredients: () => { return fetch(`${mainURL}/ingredients`).then(res => res.json()) },
},
Restaurant: {
dishes: parent => {
const { id_restaurant } = parent
return fetch(`${mainURL}/dishes?id_restaurant=${id_restaurant}`).then(res => res.json())
},
},
Dish: {
ingredients: parent => {
const { id_dish } = parent
return fetch(`${mainURL}/ingredients?id_dish=${id_dish}`).then(res => res.json())
},
},
}
我认为 Dish resolver 也解决了 Restaurant 的菜肴,但我认为我需要其他的 resolver 来解决这个问题,我错了。
此查询不显示成分
query{
restaurants{
name
dishes{
name
ingredients{
name
}
}
}
}
此查询显示成分
query{
dishes{
name
ingredients{
name
}
}
}
}
有什么解决办法吗?
解决方案
推荐阅读
- cmake - CMake进行编译定义另一个的别名
- reactjs - PrivateRoute 呈现空白页面...在反应 js 中没有显示错误
- java - 如何打印存储为对象变量的任何类型的数组?
- ethereum - 使用 Node 运行 Compound 存储库
- php - 如何在 Woocommerce 中修改小部件的 innerHTML
- raspberry-pi - 视频过滤器调整不适用于 rasperry pi
- javascript - 文本未显示在世博会应用程序上。我认为道具没有被正确传递。我应该如何纠正?
- tensorflow - 加载模型后暗网停止训练
- r - 过滤一个数据帧以使其具有与 R 中的另一个数据帧匹配的字段的行?
- cmake - CMake - 在目录的多个版本之间切换