From 8f99b2dac250ca110b6a5ff4e0fa8aebff424888 Mon Sep 17 00:00:00 2001 From: Ahmet TOK <48218623+arrrrny@users.noreply.github.com> Date: Mon, 19 Aug 2024 13:18:52 +0300 Subject: [PATCH] fix(core): Resolve User.roles field in GraphQL APIs (#3011) --- .../src/api/resolvers/entity/user-entity.resolver.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/core/src/api/resolvers/entity/user-entity.resolver.ts b/packages/core/src/api/resolvers/entity/user-entity.resolver.ts index b2e19e47f0..d73f0831e8 100644 --- a/packages/core/src/api/resolvers/entity/user-entity.resolver.ts +++ b/packages/core/src/api/resolvers/entity/user-entity.resolver.ts @@ -4,6 +4,7 @@ import { AuthenticationMethod as AuthenticationMethodType } from '@vendure/commo import { NATIVE_AUTH_STRATEGY_NAME } from '../../../config/auth/native-authentication-strategy'; import { AuthenticationMethod } from '../../../entity/authentication-method/authentication-method.entity'; import { ExternalAuthenticationMethod } from '../../../entity/authentication-method/external-authentication-method.entity'; +import { Role } from '../../../entity/role/role.entity'; import { User } from '../../../entity/user/user.entity'; import { UserService } from '../../../service/services/user.service'; import { RequestContext } from '../../common/request-context'; @@ -32,4 +33,14 @@ export class UserEntityResolver { strategy: m instanceof ExternalAuthenticationMethod ? m.strategy : NATIVE_AUTH_STRATEGY_NAME, })); } + + @ResolveField() + async roles(@Ctx() ctx: RequestContext, @Parent() user: User): Promise { + if (user.roles) { + return user.roles; + } + let roles: Role[] = []; + roles = await this.userService.getUserById(ctx, user.id).then(u => u?.roles ?? []); + return roles; + } }