apollo-server-integrations / apollo-server-integration-aws-lambda

An integration to use AWS Lambda as a hosting service with Apollo Server
MIT License
46 stars 9 forks source link

Support for authorizer-backed API Gateway events #68

Closed coyoteecd closed 1 year ago

coyoteecd commented 1 year ago

The current implementation of the context function uses an IncomingEvent that's defined as a union type: https://github.com/apollo-server-integrations/apollo-server-integration-aws-lambda/blob/fec618976880a6d499cf2dbfb02da2a71a4a96f3/src/index.ts#L20-L23

This makes it inconvenient to define a context function that actually uses data from the event. A good example is the fact that @types/aws-lambda package includes type definitions for lambdas protected by an authorizer, in fact APIGatewayProxyEvent is just assuming an "empty authorizer" as shown here. When the lambda is protected by an authorizer, one can define the handler using APIGatewayEventLambdaAuthorizerContext.

This worked fine with apollo-server-lambda, because ApolloServer's constructor did not type the context function parameters. I am trying to upgrade our project to v4 and find myself being forced to force-cast the event type just to get it to compile properly.

Proposal I would like to have a generic parameter added that specifies the type of the event, with default being IncomingEvent. If one uses a custom authorizer, they can specify an event typed with the custom authorizer result.

coyoteecd commented 1 year ago

After digging through the PRs, I think #67 covers this.

BlenderDude commented 1 year ago

Thanks for the note! My intent is to wrap up #67 this weekend so we can get this pushed out. I'll add a note here when the PR is ready if you want to take a look.

BlenderDude commented 1 year ago

@coyoteecd #67 is ready for review if you want to take a look. The updated README.md would be a great place to see the new handler changes in-use.

BlenderDude commented 1 year ago

This is now possible with V2.0.0! The following syntax should allow you to use custom authorizers.

export default startServerAndCreateLambdaHandler(
  server,
  handlers.createAPIGatewayProxyEventV2RequestHandler<
    APIGatewayProxyEventV2WithLambdaAuthorizer<{
      myAuthorizerContext: string;
    }>
  >(),
);

(The syntax will be the same for the V1 event, but I'm just not familiar with V1 typing)

As long as the provided optional type param extends the base proxy event, you'll be good to go!

coyoteecd commented 1 year ago

(Bit late, sorry) Today I had the time to upgrade our project; version 2 covers my needs very well, thanks!