首页 > 解决方案 > prisma graphql pass argument to a nested query

问题描述

I have a graphql query that pulls a Business by id , and then retrive all the reviews related to that business.

query Business($id: ID!) {
    business(id: $id) {
      _id
      name
      desc
      reviews {
        _id
        text
        createdAt
        published
}
}

the challenge I had is I would like to pull only the published reviews. I was able to acheive this by using a field resolver. however I am not too sure if it is the best way because the query already did the heavy work of pulling all the reviews and the reolver only filter the published ones.

const Business = {
  reviews: (parent, args, ctx, info) => parent.reviews.filter(rev => rev.published)
}

below is my resolver with prisma query :

  async business(parent, args, { prisma, request }, info) {
    let opArgs = {}
    opArgs.where = { _id: args.id }


    let post = null

    try {
      post = await prisma.query.business(opArgs, info)
      console.log(post)
    } catch (err) {
      throw new Error(err.message)
    }

    return post
  },

my goal is to filter on the published review on the root. is there a better way to approach this.

any help please

标签: graphqlapollo-clientapollo-serverprisma

解决方案


你不能通过 id 和发布键查询评论吗?选择所有评论并过滤它们是没有意义的。


推荐阅读