Let's say I have a `posts` query which is retrieve...
# orm-help
k
Let's say I have a
posts
query which is retrieved by an API call. The
comments
field for every
post
is retrieved by another API call, the requests are batched with dataloader. Is it possible to filter the returned
posts
based on the
comments
field (e.g. only return
posts
with
comments
)? My problem is that the
comments
field is only available after I returned the promise from the
posts
query. Is it possible to filter/transform the returned data after every related promises resolved?
d
I think that the section of docs you are looking for is this:- https://www.prisma.io/docs/reference/prisma-api/queries-ahwee4zaey#querying-data-across-relations I think you should be able to query by nested relations with the generated API. Did you already give it a go in playground?
k
It works in Prisma, but in my example both
posts
and
comments
are retrieved via an external API call (e.g. `fetch('someapi/posts'). So initially the
posts
array can't be filtered by the
comments
because they're retrieved separately. E.g.:
Copy code
// Posts.js

export const Posts = {
  comments: (parent, args, ctx, info) => {
    return ctx.externalApi.getCommentsForPost(parent.id)
  }
}

// Query.js

async posts(parent, args, ctx, info) {
  const posts = ctx.externalApi.getPosts()
  // ideally I'd be able to filter the posts
  // based on the comments property
  // but the prop is not there, it'll be
  // populated by Posts() above
  return posts
}
d
So you want to filter it in posts resolver? Or pass it the external API for filtering?
k
I want to filter the returned
posts
based on their
comments
- the problem is that the
comments
are not available when I receive the
posts
. I have to return the promise (which will resolve to the posts array) in order for the
comments
resolver function to run. So somehow I want to modify the results after
return
...
example `schema.graphql`:
Copy code
type Query {
  posts(onlyCommented: Boolean): [Post]
}

type Post {
  id: Int!
  comments: [Comment]
}

type Comment {
  id: Int!
  message: String!
}
it looks like I need this: https://github.com/prismagraphql/graphql-transform-schema but it looks abandoned. is it replaces by something else?
d
You don't have to return promise from resolver, you can use await to get the posts in a variable and fetch the respective comments
k
ah, you're right, I thought I can't use dataloader that way but it works. Thanks!
Hm, or not. I can't wait for the dataloader to load everything 😞
d
Can you please persist this in form of an issue? https://github.com/prismagraphql/graphql-yoga Sounds like a good use case, we can brainstorm on that later!