如何解决使用 HotChocolate 从 UserManager 获取用户
如何使用 UserManager
Microsoft.AspNetCore.Identity
获取来自 [ScopedService]
的用户列表
以下是我已经尝试过的:
using System.Linq;
using hostapp.Data;
using hostapp.Models;
using HotChocolate;
using HotChocolate.Data;
using Microsoft.AspNetCore.Identity;
namespace hostapp.GraphQL
{
public class Query
{
// 1.
[UseDbContext(typeof(DataContext))]
public IQueryable<AppUser> Users([ScopedService] UserManager<AppUser> userManager)
{
return (IQueryable<AppUser>)userManager.Users;
}
// 2.
[UseDbContext(typeof(DataContext))]
public async Task<List<AppUser>> Users([ScopedService] UserManager<AppUser> userManager)
{
return await userManager.Users.ToListAsync();
}
}
}
输入:
query {
users {
emailConfirmed
}
}
输出:
{
"errors": [
{
"message": "Unexpected Execution Error","locations": [
{
"line": 2,"column": 3
}
],"path": [
"users"
]
}
],"data": {
"users": null
}
}
解决方法
您不需要使用 [ScopedService]
而是使用 [Service]
您实际上甚至只需要在 [ScopedService]
与 DBContext
结合使用的情况下使用 UseDbContext
。
我们将在下一个版本中解决这个混淆
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。