Javascript Graphql—对象中数组中每个标记的返回类型
我试图弄清楚如何使graphql“query”成为基于名称的“type”标记,将来它将是ID,但我想稍后的区别只是名称与十六进制ID。我希望graphql获取标记中的每个元素并返回它们存储的数据库对应项,因此我也获取ID。这样我就可以存储id而不是名称。但到目前为止,我还并没有在网上找到任何东西,也并没有理解杰克的文档 这是对象盘,标记:Javascript Graphql—对象中数组中每个标记的返回类型,javascript,graphql,Javascript,Graphql,我试图弄清楚如何使graphql“query”成为基于名称的“type”标记,将来它将是ID,但我想稍后的区别只是名称与十六进制ID。我希望graphql获取标记中的每个元素并返回它们存储的数据库对应项,因此我也获取ID。这样我就可以存储id而不是名称。但到目前为止,我还并没有在网上找到任何东西,也并没有理解杰克的文档 这是对象盘,标记: { "name": "bolleMjaus", "country&q
{
"name": "bolleMjaus",
"country": "Norge",
"tasty": "false",
"chefsId": "5f84c63e18fc2543f00d0ce4",
"tags": ["vegan", "gluten", "nuts", "vegetarian", "lactose-free"]
}
###########
{
"_id": {
"$oid": "5f850d5128c05b27a8b64cd6"
},
"name": "vegetarian"
}
这是我的根查询:
dishes: {
type: new GraphQLList(DishType),
resolve(parent, args) {
return Dish.find({});
}
},
我使用这个查询ishquery{disks{name id chefs{name}tags{name id}}}
当前类型:
const DishType = new GraphQLObjectType({
name: 'Dish',
fields: () => ({
id: {
type: GraphQLID
},
name: {
type: GraphQLString
},
tasty: {
type: GraphQLBoolean
},
country: {
type: GraphQLString
},
tags: {
type: new GraphQLList(TagType),
resolve(parent, args) {
return parent.tags.map((x) => {return Tag.find({ name: x.toLowerCase() }) })
}
},
chefs: {
type: ChefType,
resolve(parent, args) {
return Chef.findById(parent.chefsId)
}
}
})
});
这使得:
{
"name": "bolleMjaus",
"id": "5f84ea7828c05b27a8b64cd5",
"chefs": {
"name": "Andreas"
},
"tags": [
{
"name": null,
"id": null
},
{
"name": null,
"id": null
},
{
"name": null,
"id": null
},
{
"name": null,
"id": null
},
{
"name": null,
"id": null
}
]
}
我尝试了一些不同的方法,但实际显示结果的唯一方法是在使用查询时
query{displays{name id chefs{name}tags}}
上述类型的标签部分如下所示:
tags:{
type: GraphQLString
}
给出:
{
"data": {
"dishes": [
{
"name": "bolle",
"id": "5f84e62128c05b27a8b64cd4",
"chefs": {
"name": "Andreas"
},
"tags": [
"Egg",
"Gluten",
"Vegetarian",
"Vegan",
"Wheat"
]
},
{
"name": "bolleMjaus",
"id": "5f84ea7828c05b27a8b64cd5",
"chefs": {
"name": "Andreas"
},
"tags": [
"Vegan",
"Bodily",
"Fluid",
"Vegetarian",
"Egg"
]
}
]
}
}
如何将每个正确的标记从mongoDB返回到grapHQL?我希望“tags”变量的每个元素返回带有ID和NAME的相应标记。我不明白在这样一个发达的技术中,这么基本的东西怎么会这么难。很明显,我遗漏了一些明显的东西,但令人沮丧的是,在你知道之前,在新技术中,基本的东西是多么的难以想象。。。。因为
Tag.find
返回一个数组,而不是单个实体?