Warm tip: This article is reproduced from serverfault.com, please click

其他-在给定mongoDB objectID的情况下,如何显示嵌套的graphql对象数据类型?

(其他 - How to display nested graphql object datatypes given mongoDB objectID?)

发布于 2020-11-30 02:09:02

我正在用GraphQL和 Mongoose 编写一个应用程序。我创建了一个函数来获取MongoDB内部的所有预订。预订对象包含对另一个称为Service的对象的引用。当我存储预订时,它将在MongoDB中将关联的服务对象存储为ObjectID。当我在graphql中进行查询以获取所有预订时,由于graphql仅收到一个objectID,因此graphql不提供服务及其字段类型。如何修复graphql?

为graphql返回的错误消息

所有预订都在数据库内部,但用户表示为一个objectID

GraphQL数据类型定义

有问题的解析器进行查询

graphql函数声明称为createAppointmentBookings

Questioner
c0caDJ
Viewed
11
Indragith 2020-11-30 18:09:13

你需要在AppointmentBooking中为serviceType编写一个解析程序。

Query: {
  async getAppointmentBookings() {
    ...
  }
},
Mutation: {
  ...
},
AppointmentBooking: {
  serviceType: async(parent, args, ctx, info) => {
    // Here you will get the objectId from the parent that need to query services
    // This will call for every object inside the bookings
    // Assuming you are storing the objectID for the services in the key servicetype
    const serviceId = parent.serviceType;
    try {
      const serviceDetails = await Sevice.findByID(serviceID) ;
      return serviceDetails;
    } catch (err) {
      throw new Error(err);
    }
  }
}