【问题标题】:Hotchocolate GraphQL: Dependency not working as intendedHotchocolate GraphQL:依赖项未按预期工作
【发布时间】:2021-03-26 08:17:23
【问题描述】:

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

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

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

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 更新到 v11.0.9 时,当我将“userClaims”放入我的查询时它就不起作用了。我收到以下错误:

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)

我不太确定问题出在哪里,但我认为我的服务“[Service] UserManager userManager”不再被注入。有没有人遇到过这种错误?

【问题讨论】:

  • 你能提出问题并提供更多代码吗?
  • 当然,我也在 Slack 上问过同样的问题,但似乎没有人知道问题所在。

标签: c# asp.net .net-core graphql hotchocolate


【解决方案1】:

你可以尝试添加这个吗:

    [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();
    }

【讨论】:

  • 这可行,但我也找到了另一个解决方案,如果你从查询中删除“[UseProjection]”,它也可以工作!
猜你喜欢
  • 2015-05-22
  • 1970-01-01
  • 1970-01-01
  • 2011-04-22
  • 2019-12-01
  • 2019-01-22
  • 1970-01-01
  • 2018-06-27
  • 2017-10-14
相关资源
最近更新 更多