繁体   English   中英

在 GraphQL 解析器中链接集合

[英]Linking collections in GraphQL resolver

我正在尝试在 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 正确传递到查找过滤器的方法。 任何提示?

更新:当我实现解析器生命时:

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;
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM