简体   繁体   中英

Getting users from UserManager using HotChocolate

How to get list of users from UserManager from Microsoft.AspNetCore.Identity using [ScopedService]

Below is what I already tried:

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

    }
}

Input:

query {
  users {
    emailConfirmed
  }
}

Output:

{
  "errors": [
    {
      "message": "Unexpected Execution Error",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "users"
      ]
    }
  ],
  "data": {
    "users": null
  }
}

You do not need to use [ScopedService] but rather [Service] You really only even need to use [ScopedService] in case of a DBContext in combination with UseDbContext . We will fix this confusion in the next version

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