Skip to content

Commit

Permalink
fix(core): Resolve User.roles field in GraphQL APIs (#3011)
Browse files Browse the repository at this point in the history
  • Loading branch information
arrrrny authored Aug 19, 2024
1 parent 694845f commit 8f99b2d
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/core/src/api/resolvers/entity/user-entity.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<Role[]> {
if (user.roles) {
return user.roles;
}
let roles: Role[] = [];
roles = await this.userService.getUserById(ctx, user.id).then(u => u?.roles ?? []);
return roles;
}
}

0 comments on commit 8f99b2d

Please sign in to comment.