首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Hotchocolate :依赖关系无法按预期工作

Hotchocolate :依赖关系无法按预期工作
EN

Stack Overflow用户
提问于 2021-03-26 08:17:23
回答 1查看 761关注 0票数 0

我有个小问题。在Hotchocolate 10中,我有以下查询:

代码语言:javascript
复制
query getUser($id: String) {
  user(id: $id) {
    id
    email
        userClaims {
      type
      value
    }
  }
}

它基本上会搜索用户的声明和电子邮件。为此,我在我的ApplicationUser类中有一个额外的方法:

代码语言:javascript
复制
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;
    }
  }
}

这很好,但是自从昨天我将HotChocolate更新到11.0.9版本之后,当我把“userClaims”暗示我的查询时,它就不起作用了。我得到了以下错误:

代码语言:javascript
复制
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)

我不太清楚问题是什么,但我认为我的服务“服务UserManager userManager”不再被注入。以前有人遇到过这样的错误吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-28 16:28:19

您是否可以尝试添加以下内容:

代码语言:javascript
复制
    [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();
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66813227

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档