CodePark/ questions / GraphQL & WebSockets
START PRACTICINGPRACTICE
Backend question · NestJS › GraphQL & WebSockets

An HTTP auth guard that calls context.switchToHttp().getRequest() doesn't work on GraphQL resolvers. What is the fix?

Use GqlExecutionContext.create(context).getContext().req to get the request

Use GqlExecutionContext. For GraphQL, the ExecutionContext holds the resolver arguments, not the HTTP request and response. GqlExecutionContext.create(context) converts it. The request is then on the GraphQL context, usually as req with the Apollo driver.

"Guards can't be used with GraphQL; use middleware instead" is wrong: guards fully work with resolvers. "Use
context.switchToRpc().getData()" is wrong because switchToRpc() is for microservices.