I have a domain entity Poll:
public class Poll
{
public int Id { get; set; }
public ICollection<Question> Questions { get; set; }
}
I want to add a new resolver to the Poll called "userHasAnswered" that is whether the logged in user has already taken a given poll. I've added a new field but I'm not sure how to access the current poll's Id in the resolver:
public class PollType : ObjectType<Poll>
{
protected override void Configure(IObjectTypeDescriptor<Poll> descriptor)
{
descriptor
.Field("userHasAnswered")
.Resolve(context =>
{
var dataContext = context.Service<DataContext>();
var userId = context.Service<IHttpContextAccessor>().HttpContext.User.GetUserId();
return dataContext.Answers.Any(x => x.UserId == userId && x.Question.PollId == /*How do if get the current pollId here?*/1);
});
}
}
Is this the ideal way of adding a new field that is dependant on the given poll's data or is there a better way?