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.
Resolver is a collection of functions that generate response for a GraphQL query. In simple terms, a resolver acts as a GraphQL query handler. Every resolver function in a GraphQL schema accepts four positional arguments as given below − fieldName:(root, args, context, info) => { result }
Apollo Server needs to know how to populate data for every field in your schema so that it can respond to requests for that data. To accomplish this, it uses resolvers. A resolver is a function that's responsible for populating the data for a single field in your schema.
Each field on each type is backed by a function called the resolver which is provided by the GraphQL server developer. When a field is executed, the corresponding resolver is called to produce the next value. If a field produces a scalar value like a string or number, then the execution completes.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With