MichalLytek / type-graphql

Create GraphQL schema and resolvers with TypeScript, using classes and decorators!

Home Page:https://typegraphql.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Argument set through Custom Parameter Decorator is empty when using inheritance with a FieldResolver

maurocolella opened this issue · comments

Describe the Bug
I have a custom parameter decorator on a field resolver applied through inheritance.

Unfortunately, with this structure, the parameter returned is empty (although as far as I can tell the decorator is executed and returns the correct value).

This is using "type-graphql": "^1.2.0-rc.1"

To Reproduce
Abstract base class (sample):

export function createBaseResolver<T extends ClassType>(_suffix: string, objectTypeCls: T) : any {
  @Resolver(of => objectTypeCls, { isAbstract: true })
  abstract class BaseResolver {
    @Authorized()
    @HasBranch()
    @FieldResolver(returns => Boolean)
    async onboarded(
      @CurrentSession() user: AuthContext,
    ): Promise<boolean> {
      console.log({ user }) // # empty

     // # logic here

      return false
    }
  }

  return BaseResolver
}

Custom parameter decorator:

export const CurrentSession = (): ParameterDecorator => {
  return createParamDecorator<any>(
    ({ context }) : AuthContext => context?.auth?.user,
  )
}

Usage:

const EntityBaseResolver = createBaseResolver('entity', Entity)

@Resolver(Entity)
export class EntityResolver extends EntityBaseResolver {
  constructor(
    private entityService: EntityService,
  ) {
    super()
  }

Expected Behavior
I would expect the decorator to correctly inject the user as an argument into the field resolver.

Logs
No error. But user is defined in the decorator, undefined in the field resolver.

Environment (please complete the following information):

  • OS: Ubuntu Linux 22.0.4
  • Node v14.8.2
  • Package version ^1.2.0-rc.1 (please check if the bug still exist in newest release)
  • TypeScript version ^4.5.2

Additional Context
This is transpiled and executed in dev using ts-node, eq.

"ts-node": "^10.2.0"
"ts-node-dev": "^1.1.8"
ts-node-dev --inspect --exit-child -r tsconfig-paths/register --respawn src/main/index.ts