8

Say you wanted to call the createdAt resolver from the updatedAt resolver. For example this doesn't work:

{
  Review: {
    createdAt: review => review._id.getTimestamp(),
    updatedAt: review => review.updatedAt || this.createdAt(review)
  },
}

I realize I could make a reviewCreatedAt() function that is called from both, but I'm looking for a way to call the createdAt resolver.

Loren
  • 13,903
  • 8
  • 48
  • 79

1 Answers1

2

There is no standard way to call another resolver. Using this won't work even if you don't use an arrow function because the context is lost when the resolver is called by the underlying code. You can do something like this:

const resolvers = {
  Review: {
    createdAt: review => review._id.getTimestamp(),
    updatedAt: review => review.updatedAt || resolvers.Review.createdAt(review)
  },
}

If you're using a data model, though, you may find it easier to just lift this logic into the data model (using a calculated or virtual field).

Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183