链接GraphQL解析器中的集合



我正在尝试连接GraphQL查询中的两个集合。

我的数据如下:邮轮:

[
{
"cruiseID" : "00001",
"title" : "title 1",
"description" : "desc 1",
"startDate" : 20150820,
"endDate" : 20150827,
"numDays" : 8,
"startPort" : "Juneau, Alaska",
"roomTypes" : [
{"roomID" : "IPD"},
{"roomID" : "SDS"}
]
}, {
"cruiseID" : "00002",
"title" : "title 1",
"description" : "desc 2",
"startDate" : 20150710,
"endDate" : 20150724,
"numDays" : 14,
"startPort" : "San Diego",
"roomTypes" : [
{"roomID" : "IPD"},
{"roomID" : "SJS"},
{"roomID" : "SDS"}
]
}
]

房间:

[
{
"roomID": "IPD",
"roomDetails": {
"roomType": "IPD aaaaa",
"title": "aaaa",
"description": "ddddd",
"maxOccupants": 2
},
"capacity": [
{"cruiseID": "00001", "total": 21, "available": 20},
{"cruiseID": "00002", "total": 31, "available": 30}
]
},
{
"roomID": "SJS",
"roomDetails": {
"roomType": "SJS aaaa",
"title": "aaaaa",
"description": "aaaaa",
"maxOccupants": 4
},
"capacity": [
{"cruiseID": "00001", "available": 27},
{"cruiseID": "00002", "available": 27}
]
},
{
"roomID": "SDS",
"roomDetails": {
"roomType": "SDS aaa",
"title": "sssss",
"description": "sssssss",
"maxOccupants": 4
},
"capacity": [
{"cruiseID": "00001", "available": 20},
{"cruiseID": "00002", "available": 20}
]
}
]

我的GraphQL模式:

type Query {
Cruises: [Cruise]
}
type RoomDetails {
roomType: String
title: String
description: String
maxOccupants: Int
}
type Capacity {
cruiseID: String
total: Int
available: Int
}
type Room {
roomID: String
roomDetails: RoomDetails
capacity: [Capacity]
}
type Cruise {
_id: ID
cruiseID: String
title: String
description: String
startDate: Int
endDate: Int
numDays: Int
startPort: String
roomTypes: [Room]
}

我的GraphQL解析器,我试图通过从房间集合中读取房间类型来打印它们的详细信息:

const Query = {
Cruises: async () => {
data = await db.collection('cruises').find().toArray().then(res => { return res });
return data
}
}
const Cruise = {
roomTypes: async (root) => {
data = await db.collection('rooms').find({'roomID': 'SDS'}).toArray().then(res => { return res });
// prints OK
logger.debug(`root.roomTypes.roomID': ${JSON.stringify(root.roomTypes[1].roomID)}`);
// undefined
logger.debug(`root.roomTypes.roomID': ${JSON.stringify(root.roomTypes.roomID)}`);
// prints OK
logger.debug(`root.startPort': ${root.startPort}`);
return data
}
}
module.exports = {
Query,
Cruise
}

我在Cruises和Rooms之间找不到合适的接头。硬编码{'roomID':'SDS'}工作正常,即显示正确的输出。然而,我找不到一种方法来正确地将roomID从Cruises传递到find过滤器。有什么提示吗?

更新:当我实现解析器的生活这个:

const Cruise = {
roomTypes: async (root) => {
data = await db.collection('rooms').find({'capacity.cruiseID': root.cruiseID}).toArray().then(res => { return res })
//data = await db.collection('rooms').find().toArray().then(res => { return res })
return data
}
}

我得到了数据(roomTypes完全解析(,但实际上它没有进行任何过滤/连接。以下是我得到的(用一些垃圾数据编辑,所以它更短(:

"data": {
"Cruises": [
{
"cruiseID": "00001",
"title": "and",
"startDate": 20150820,
"endDate": 20150827,
"numDays": 8,
"startPort": "Juneau, Alaska",
"roomTypes": [
{
"roomID": "IPD",
"roomDetails": {
"roomType": "asd",
"title": "and",
"description": "asdr",
"maxOccupants": 2
},
"capacity": [
{
"cruiseID": "00001",
"total": 21,
"available": 20
},
{
"cruiseID": "00002",
"total": 31,
"available": 30
}
]
},
{
"roomID": "OPD",
"roomDetails": {
"roomType": "ad",
"title": "and",
"description": "asdr",
"maxOccupants": 2
},
"capacity": [
{
"cruiseID": "00001",
"total": null,
"available": 30
},
{
"cruiseID": "00002",
"total": null,
"available": 30
}
]
},
{
"roomID": "SDD",
"roomDetails": {
"roomType": "asd",
"title": "and",
"description": "and",
"maxOccupants": 2
},
"capacity": [
{
"cruiseID": "00001",
"total": null,
"available": 25
},
{
"cruiseID": "00002",
"total": null,
"available": 25
}
]
},
{
"roomID": "SD2",
"roomDetails": {
"roomType": "asd",
"title": "and",
"description": "Fast",
"maxOccupants": 4
},
"capacity": [
{
"cruiseID": "00001",
"total": null,
"available": 22
},
{
"cruiseID": "00002",
"total": null,
"available": 22
}
]
},
{
"roomID": "SJS",
"roomDetails": {
"roomType": "asd",
"title": "and",
"description": "Tasdr",
"maxOccupants": 4
},
"capacity": [
{
"cruiseID": "00001",
"total": null,
"available": 27
},
{
"cruiseID": "00002",
"total": null,
"available": 27
}
]
},
{
"roomID": "SDS",
"roomDetails": {
"roomType": "asd",
"title": "and",
"description": "Tase",
"maxOccupants": 4
},
"capacity": [
{
"cruiseID": "00001",
"total": null,
"available": 20
},
{
"cruiseID": "00002",
"total": null,
"available": 20
}
]
}
]
},

而不是根据Cruises集合中的条目为cruiseID 00001仅列出2种房间类型(IPD、SDS(。

这就是我最终做到的。这不是最干净的代码/解决方案,但它完成了任务。

const Cruise = {
roomTypes: async (root) => {
let rooms = root.roomTypes;
allRooms = await db.collection('rooms').find().toArray().then(res => { return res });
rooms.forEach(room => {
let currentRoomID = room.roomID;
currentRoom = allRooms.filter(function(r) {
return r.roomID === currentRoomID;
});
currentRoomCapacity = currentRoom[0].capacity.filter(function(c) {
return c.cruiseID === root.cruiseID;
});
room.roomDetails = currentRoom[0].roomDetails
room.capacity = currentRoomCapacity
});
return rooms;
}
}

最新更新