0

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?

Cameron Cheung
  • 197
  • 1
  • 2
  • 12

1 Answers1

0

Your case is pretty common and hotchocolate's docs have a few words about it (link). So according to the article here's how you can get current poll ID in the resolver - context.Parent<Poll>().Id

Eugene
  • 169
  • 1
  • 6