简体   繁体   中英

Hotchocolate GraphQL: Dependency not working as intended

I have a small question. In version 10 of Hotchocolate I had the following query:

query getUser($id: String) {
  user(id: $id) {
    id
    email
        userClaims {
      type
      value
    }
  }
}

Which basically would search for the claims and email of an user. To to that I had in my ApplicationUser Class an extra method:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using HotChocolate;
using LLIS.Data.Data_Transfer_Objects;
using LLIS.Data.Entities;
using Microsoft.AspNetCore.Identity;
namespace LLIS.Data.Models
{
  public class ApplicationUser : IdentityUser
  {
    public DateTime? LastLogin { get; set; } = null;
    [GraphQLIgnore] public bool CanBeDeleted { get; set; } = true;
    [GraphQLIgnore] public override string NormalizedUserName { get; set; }
    [GraphQLIgnore] public override string NormalizedEmail { get; set; }
    [GraphQLIgnore] public override string PasswordHash { get; set; }
    [GraphQLIgnore] public override string SecurityStamp { get; set; }
    [GraphQLIgnore] public override string ConcurrencyStamp { get; set; }
    [GraphQLIgnore] public override string PhoneNumber { get; set; }
    [GraphQLIgnore] public override bool PhoneNumberConfirmed { get; set; }
    [GraphQLIgnore] public override bool TwoFactorEnabled { get; set; }
    [GraphQLIgnore] public override DateTimeOffset? LockoutEnd { get; set; }
    [GraphQLIgnore] public override bool LockoutEnabled { get; set; }
    [GraphQLIgnore] public override int AccessFailedCount { get; set; }
    public async Task<IEnumerable<LlisUserClaimDto>> GetUserClaims([Service] UserManager<ApplicationUser> userManager)
    {
      return (await userManager.GetClaimsAsync(this)).ToList()
        .Select(claim => new LlisUserClaimDto {Type = claim.Type, Value = claim.Value}).ToList();
    }
    /*
     * Control if the current user is allowed to update this activity
     * if he has edit/delete access or he his the supervisor of the activity it will return true
     */
    public static bool IsCurrentUserAllowedToUpdateActivity(ClaimsPrincipal user, Activity activity)
    {
      return user.HasClaim(c => c.Type == "Activity" && c.Value == "delete") ||
             user.HasClaim(c => c.Type == "Activity" && c.Value == "edit") ||
             user.Claims.FirstOrDefault(c => c.Type == "uid")?.Value
             == activity.Supervisor.UserName;
    }
  }
}

This was working very well but since yesterday when I updated HotChocolate to the v11.0.9 it isn't working when I put the “userClaims” insinde my query. I got the following error:

Incorrect number of arguments supplied for call to method 'System.Threading.Tasks.Task`1[System.Collections.Generic.IEnumerable`1[LLIS.Data.Data_Transfer_Objects.LlisUserClaimDto]] GetUserClaims(Microsoft.AspNetCore.Identity.UserManager`1[LLIS.Data.Models.ApplicationUser])' (Parameter 'method')",
        "stackTrace": "   at System.Dynamic.Utils.ExpressionUtils.ValidateArgumentCount(MethodBase method, ExpressionType nodeKind, Int32 count, ParameterInfo[] pis)\n   at System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method)\n   at HotChocolate.Data.Projections.Expressions.Handlers.ExpressionExtensions.Append(Expression expression, MemberInfo memberInfo)\n   at HotChocolate.Data.Projections.Expressions.Handlers.QueryableProjectionListHandler.OnBeforeEnter(QueryableProjectionContext context, ISelection selection)\n   at HotChocolate.Data.Projections.ProjectionVisitor`1.OnBeforeEnter(ISelection selection, TContext context)\n   at HotChocolate.Data.Projections.SelectionVisitor`1.Visit(ISelection selection, TContext context)\n   at HotChocolate.Data.Projections.ProjectionVisitor`1.Visit(ISelection selection, TContext context)\n   at HotChocolate.Data.Projections.SelectionVisitor`1.VisitChildren(IOutputField field, TContext context)\n   at HotChocolate.Data.Projections.SelectionVisitor`1.Visit(IOutputField field, TContext context)\n   at HotChocolate.Data.Projections.ProjectionVisitor`1.Visit(IOutputField field, TContext context)\n   at HotChocolate.Data.Projections.ProjectionVisitor`1.Visit(TContext context)\n   at HotChocolate.Data.Projections.Expressions.QueryableProjectionProvider.<CreateExecutor>g__ExecuteAsync|2_1[TEntityType](FieldDelegate next, IMiddlewareContext context)\n   at HotChocolate.Data.Projections.FirstOrDefaultMiddleware`1.InvokeAsync(IMiddlewareContext context)\n   at HotChocolate.Utilities.MiddlewareCompiler`1.ExpressionHelper.AwaitTaskHelper(Task task)\n   at HotChocolate.Execution.Processing.ResolverTask.ExecuteResolverPipelineAsync(CancellationToken cancellationToken)\n   at HotChocolate.Execution.Processing.ResolverTask.TryExecuteAsync(CancellationToken cancellationToken)

I'm not quite sure what the problem is but I think my service “[Service] UserManager userManager” is not being injected anymore. Did anyone encounter this kind of error before?

Can you try to add this:

    [IsProjected(false)]
    public async Task<IEnumerable<LlisUserClaimDto>> GetUserClaims([Service] UserManager<ApplicationUser> userManager)
    {
      return (await userManager.GetClaimsAsync(this)).ToList()
        .Select(claim => new LlisUserClaimDto {Type = claim.Type, Value = claim.Value}).ToList();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM