0

我有以下数据模型:

type Tvshow {
  id: ID! @unique
  title: String!
  pricing: [Pricing]
  startDate: DateTime!
  endDate: DateTime!
  subscribers: [Tvshowsubscription!]
 .....
}

type FavoriteTvshow {
  id: ID! @unique
  tvshow: Tvshow!
  user: User!
}

type User {
  id: ID! @unique
  name: String
  email: String! @unique
  password: String
  googleID: String @unique
  resetToken: String
  resetTokenExpiry: String
  permissions: [Permission]
  address: Address
  phone: String
  favorites: [FavoriteTvshow!]
  tvshowSubscriptions: [Tvshowsubscription!]
}

我有使用 addFragmentToInfo 的自定义 Tvshow 解析器:

解析器-queries.js

const Query = {
 ...
  favoriteTvshows: forwardTo('db'),
  tvshow: (parent, args, ctx, info) => {
    const fragment = `fragment EnsureComputedFields on Tvshow { pricing { minQuantity maxQuantity unitPrice} subscribers { id }}`
    return ctx.db.query.tvshow({}, addFragmentToInfo(info, fragment))
  },
 ....
};

tvshow-resolver.js

const Tvshow = {
  countSubscribers: (parent) => {
    return parent.subscribers.length;
  },
}

这是一个例子,我有更多的 Tvshow 计算字段

我可以使用 countSubscribers 查询 Tvshows,这样可以正常工作:

query SINGLE_TVSHOW_QUERY($id: ID!) {
  tvshow(where: { id: $id }) {
    id
    title
    pricing {
      minQuantity
      maxQuantity
      unitPrice
    }
    startDate
    endDate
    countSubscribers
  }
}

但我想做的是从返回 countSubscribers 的用户那里获取所有最喜欢的电视节目,对此的查询可能是这样的:

query FAVORITES_FROM_USER($userId: ID!) {
    favoriteTvshows(where: { user: {id: $userId} }) {
      tvshow {
        id
        title
        startDate
        endDate
        countSubscribers
      }
    }
  }

问题是当我查询这个时,在我之前提到的 tvshow-resolver.js 中,父级没有任何订阅者对象

4

1 回答 1

0

错误非常愚蠢,但无论如何我都会发布它。我在查询中需要订阅者

query FAVORITES_FROM_USER($userId: ID!) {
    favoriteTvshows(where: { user: {id: $userId} }) {
      tvshow {
        id
        title
        startDate
        endDate
        subscribers { <--- 
          id
          quantity
        }
        countSubscribers
      }
    }
  }

这样tvshow-resolver.js中的父级将拥有订阅者对象

于 2019-03-15T02:49:55.037 回答