如何在Apollo graphql中解析深度嵌套模式

问题描述

我在解决graphql中的嵌套查询时遇到问题。我正在使用apollo-server-express。 这是架构。

 scalar DateTime
  scalar GeoJSON

  directive @isAuthenticated on FIELD_DEFINITION
  directive @isAdmin on FIELD_DEFINITION
  enum BookingStatus {
.............
 
  }
  type Space {
    id: ID!
    name: String!
    aboutLocation: String
    aboutSpace: String
    thingsToBeAwareOf: String
    rooms: [Room]
    amenitiesIncluded: [Amenity]
    amenitiesExcluded: [Amenity]
    images: [String]
    physicalAddress: String
    geoLocation: GeoJSON
    createdBy: User!
    created: DateTime!
    status: String
  }
  type Amenity {
    id: ID!
    name: String
    description: String
  }

  type Room {
    description: String!
    count: Int!
    price: Float
  }


  type Image {
    url: String
  }
  type User {
    id: ID!
    phoneNumber: String
    email: String
    password: String
    fullName: String
    cnic: String
    gender: String
    profilePhoto: Image
    dob: String
    maritalStatus: String
    about: String
    role: String
    otp: Int
    otpTimestamp: String
    mySpaces: [Space]
    myBookings: [Booking]
  }
  type Booking {
    id: ID!
    space: Space
    guest: User
    rooms: [Room]!
    checkIn: String!
    checkout: String!
    adults: Int
    kids: Int
    createdAt: String
    updatedAt: String
    status: BookingStatus!
  }

  type Query {
    space(id: ID!): Space @isAuthenticated
    spaces: [Space] @isAuthenticated
    amenities: [Amenity] @isAuthenticated
    me: User @isAuthenticated
    users: [User] @isAdmin
  }

这是我的解决者。

const resolvers = {
  DateTime: GraphQLJSON,GeoJSON: GraphQLJSON,Query: {
    spaces: (_,__,context) => {
      

      return Space.find()
        .populate('amenities.included')
        .populate('amenities.excluded')
        .populate('createdBy')

        .exec();
    },space: async (_,args,{ user }) => {
      
      const { id } = args;
      const space = await Space.findById(id)
        .populate('createdBy')
        .populate('amenities.included')
        .populate('amenities.excluded')
        .exec();

      if (!space) {
        throw new ApolloError('Not found');
      }

      return space;
    },amenities: () => {
      return Amenity.find();
    },me: async (parent,context) => {
      //console.log(context.user);
      return await User.findById(context.user._id);
    },users: async (parent,context) => {
      return User.find();
    },},Space: {
    amenitiesIncluded: async (parent) => {
      return parent.amenities.included.map((amenity) => {
        const a = {
          id: amenity._id,name: amenity.name,description: amenity.description,};

        return a;
      });
    },amenitiesExcluded: async (parent) => {
      return parent.amenities.excluded.map((amenity) => {
        const a = {
          id: amenity._id,rooms: (parent) => {
      return parent.rooms;
    },images: (parent) => {
      return parent.images;
    },User: {
    profilePhoto: (parent) => {
      return parent.profilePhoto;
    },mySpaces: (parent) => {
      const { _id } = parent;
      return Space.find({ createdBy: _id });
    },myBookings: (parent) => {
      const { _id } = parent;
      const result = Booking.find({ guest: _id })
        .populate('guest')
        .populate('space')
        .exec();

      return result;
    },}

现在在太空中(id),我收到了便利设施包括的便利设施 out put of the space(id) query

但是我收到空值

   me{
    fullName
    mySpaces{
    amenitiesIncluded{
    name}
}

当我想访问设施时查询包括在内,显示为空。 这是图像 me query

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)