Support for Graphql
JoseVteRS opened this issue · comments
Hi!
Actually nestjs-access-control is supporting Graphql?
I have an error when I do a mutation:
"Type Error: Cannot read property "user" of undefined
at ACGuard.getUser
at ACGuard.gerRoles
at ACGuard.canActivate
I changed the code from library directly
the code into ACGuard class in file access-control.guard.js
async getUser(context) {
// const request = context.switchToHttp().getRequest();
// return request.user;
const request = GqlExecutionContext.create(context).getContext().req;
return request.user;
}
To the best of my knowledge the context.switchToHttp().getRequest()
is only for REST. In Graphql should be used
GqlExecutionContext.create(context).getContext().req;
After changing the file, the error did not appear again and my code run as I expected.
Please tell if this is correct or if it exists other way to do this.
Thanks!
I think you can send a PR for this. you're correct in how you're implementing the GQL
context.
I believe having an Enum to select between (REST and GRAPHQL) would do the trick @bashleigh any thoughts?
I'm looking for this too. Any update regarding the graphql support?
Btw, this issue can be fixed like this
async getUser(context) {
let request;
if (GqlArgumentsHost.create(executionContext).getType<GqlContextType>() === 'graphql') {
request = GqlExecutionContext.create(context).getContext().req;
} else {
request = context.switchToHttp().getRequest();
}
return request.user;
}