#GqlExecutionContext not working properly in block

1 messages · Page 1 of 1 (latest)

fair shuttle
#

I had created a CurrentUser() decorator for my grapqhl mutation:

import {createParamDecorator, ExecutionContext} from "@nestjs/common"
import {getReqFromAutoContext} from "../../util/auto-context-request"

export const CurrentUser = createParamDecorator(
    (_data: unknown, context: ExecutionContext) => {
        return getReqFromAutoContext(context).user
    },
)

Where getReqFromAutoContext simply switches on the contextType:

import {GqlContextType, GqlExecutionContext} from "@nestjs/graphql"
import {ExecutionContext} from "@nestjs/common"


export const getReqFromAutoContext = (context: ExecutionContext) => {
    const contextType = context.getType<GqlContextType>()
    if (contextType === "ws") {
        return context.switchToWs().getClient().request
    } else if (contextType === "http") {
        return context.switchToHttp().getRequest()
    } else if (contextType === "graphql") {
        return GqlExecutionContext.create(context).getContext().req
    } else if (contextType === "rpc") {
        return context.switchToRpc().getContext().req
    } else {
        throw new Error(`Unknown context type: ${contextType}`)
    }
}

For some reason, req was always undefined, which seems to be related to the block-scope create, getContext and .req are called or accessed in. I do not assume this is expected behaviour, but has anyone had something similar happen before?

#

Hey where did my screenshots go

#

The last debug output is especially interesting, as the debugger shows gqlContext.req being defined, yet when assigned to the req variable it is undefined

#

Some more interesting behaviour is when I try to call console.log():

import {GqlContextType, GqlExecutionContext} from "@nestjs/graphql"
import {ExecutionContext} from "@nestjs/common"


export const getReqFromAutoContext = (context: ExecutionContext) => {
    console.log(GqlExecutionContext.create(context).getContext())
    return null
}

The debugger does not want to continue after that line, nor is anything printed or does the mutation resolve. I also can't step-into when the breakpoint is on the line with the log.

fair shuttle