Skip to content

Bug: AppSyncEventsResolver decorators break scope #3973

Open
@dreamorosi

Description

@dreamorosi

Expected Behavior

When using the @onPublish and @onSubscribed decorator of the AppSyncEventsResolver the decorated methods should preserve the this scope of the class they belong to.

This way, these methods can continue accessing class properties and methods.

Current Behavior

When applying the decorators the decorated method is no longer bound and this goes to either the instance of AppSyncEventsResolver or undefined, thus preventing from using other class methods/properties.

Code snippet

const app = new AppSyncEventsResolver({ logger: console });

class Lambda {
  public scope = 'scoped';

  @app.onPublish('/foo')
  public async handleFoo(payload: string) {
    return `${this.scope} ${payload}`;
  }

  public async handler(event: unknown, context: Context) {
    return this.stuff(event, context);
  }

  async stuff(event: unknown, context: Context) {
    return app.resolve(event, context);
  }
}
const lambda = new Lambda();
const handler = lambda.handler.bind(lambda);

Steps to Reproduce

N/A

Possible Solution

No response

Powertools for AWS Lambda (TypeScript) version

latest

AWS Lambda function runtime

22.x

Packaging format used

npm

Execution logs

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingconfirmedThe scope is clear, ready for implementationevent-handlerThis item relates to the Event Handler Utility

Type

No type

Projects

Status

Pending review

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions