Any pattern for this? Let's say I have a field on ...
# orm-help
k
Any pattern for this? Let's say I have a field on a Type which doesn't exist in the database, it only has a resolver. If I query the db with
prisma-binding
, the returned elements (obviously) doesn't have the field. But what if I want to use the resolved value/field e.g. for sorting or filtering the Response? In other words, is there a place where I can modify the response of a resolver after every sub-resolvers finished?
n
can you elaborate this question with a specific example?
k
datamodel.graphql
Copy code
type Item {
  id: ID! @unique
  foo: String!
}
schema.graphql
Copy code
type Query {
  search(something: String!): [Item!]!
}
type Item {
  id: ID!
  foo: String!
  bar: String #  <-- only in schema
}
Item.js
Copy code
export const Item = {
  bar: async () => {
    return await someThingAsync()
  }
}
Query.js
Copy code
export const Query = {
  search: async (parent, args, ctx, info) => {
    const items = await ctx.db.query.items({}, info)
    // this won't work, bar resolver will run in the future
    return items.filter(item => item.bar === args.something)
  }
}
n
That's an interesting use case I haven't come across yet. I think you would need to solve this outside of the resolver execution chain, which would lead to duplication of code for the extra field. Maybe I'm not seeing something right now to make this easier, so I recommend to post your question in the Forum to get more eyes on it: https://www.prisma.io/forum/c/questions/.
k
I'm not sure if it's possible, but one solution would be to create 2 resolvers: 1 as the current one (without the
filter
) and another which would query the first, so I'd be able to do something like this in the second:
Copy code
// info is the same so no extra fields are requested from the db
const resolvedItemsWithBar = await Query.firstSearch(info: info)
return resolvedItemsWithBar.filter(item => item.bar === args.something)
Can a resolver query another resolver with the same
info
?