Prisma graphql计算关系上的字段

时间:2019-03-14 19:51:00

标签: graphql apollo-client prisma prisma-graphql

我有以下数据模型:

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解析器:

resolver-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查询电视节目,这样做很不错:

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中查询此内容时,父级没有任何订阅者对象

1 个答案:

答案 0 :(得分:0)

错误非常愚蠢,但是我还是会发布它。我需要查询中的订阅者

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

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