Is it possible to nullify parent inside a GraphQL Resolver?

218 views Asked by At

While optimizing my GraphQL resolvers, I have realized that resolving all the fields using their resolvers(not using the parent resolvers with some pre-filled data) is the way to utilize DataLoader in the most powerful way. Like this:

export const User = objectType({
  name: 'User',
  definition(t) {
    t.nonNull.id('id')
    t.string('email', {
      resolve(parent, _, ctx) {
        // `getUser()` is a function that fetches the DB and then returns Promise<User>
        // It's internally using Prisma, which has an internal DataLoader
        return getUser(ctx).then(user => user?.email)
      }
    })
    // more fields with similar resolvers...
  }
})

However, by doing this, it became impossible(at least in my knowledge) to return null user when the user doesn't exist. So the question came up.

  1. Is it possible to nullify the entire parent object inside a GraphQL resolver?
  2. If so, How?
  3. If not, would adding something like exists field be the best solution for this?
0

There are 0 answers