【问题标题】:Prisma graphql computed fields on relationsPrisma graphql 关系计算域
【发布时间】:2019-08-05 20:16:06
【问题描述】:

我有以下数据模型:

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 的自定义电视节目解析器:

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 中,父级没有任何订阅者对象

【问题讨论】:

    标签: graphql apollo-client prisma prisma-graphql


    【解决方案1】:

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

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

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

    【讨论】: