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

Creating handler with createAPIGatewayProxyEventV2RequestHandler does not work #87

Closed Sebas1245 closed 1 year ago

Sebas1245 commented 1 year ago

Hi there. I tried following the example given on the official Apollo documentation to find out that using handlers.createAPIGatewayProxyEventV2RequestHandler() and trying to open the Apollo Server playground results in a Cannot read properties of undefined (reading 'method') response from the browser. Downgrading to the original version has it working just fine, so I'm guessing this is a bug with V2.

I'm using Node.JS LTS/Hydrogen with Typescript with the following dependencies

  "dependencies": {
    "@apollo/server": "^4.5.0",
    "@as-integrations/aws-lambda": "^2.0.1",
    "graphql": "^16.6.0"
  },
  "devDependencies": {
    "@graphql-codegen/cli": "3.2.2",
    "@graphql-codegen/typescript": "3.0.2",
    "@graphql-codegen/typescript-document-nodes": "3.0.2",
    "@graphql-codegen/typescript-resolvers": "3.1.1",
    "@types/aws-lambda": "^8.10.111",
    "serverless-offline": "^12.0.4",
    "serverless-webpack": "^5.11.0",
    "ts-loader": "^9.4.2",
    "typescript": "^4.9.5",
    "webpack": "^5.76.0",
    "webpack-node-externals": "^3.0.0"
  }

This is the code I'm using to create the Lambda handler

import { ApolloServer } from "@apollo/server";
import {
  startServerAndCreateLambdaHandler,
  handlers,
} from "@as-integrations/aws-lambda";
import { resolvers } from "./resolvers";
import { typeDefs } from "./types/type-defs";

type MyContext = {};

const apolloServer = new ApolloServer<MyContext>({ typeDefs, resolvers });

export const graphqlServer = startServerAndCreateLambdaHandler(
  apolloServer,
  handlers.createAPIGatewayProxyEventRequestHandler()
);
daniel-keller commented 1 year ago

I'm getting the same error. Works with createAPIGatewayProxyEventRequestHandler but not createAPIGatewayProxyEventV2RequestHandler.

BlenderDude commented 1 year ago

Can you provide your serverless.yml file? I suspect that you might be using a lambda event source that only supports the V1 event. As fellow Daniel, @daniel-keller mentioned, if that is the case you will need to utilize the createAPIGatewayProxyEventRequestHandler() function. Event sources that utilize V1 and V2 events are not cross compatible.

If you are following the documentation here, this should set you up to use the V2 event type. If this is incorrect please let me know and I will investigate further.

Sebas1245 commented 1 year ago

Yeah I think I followed the documentation you specified, except I'm using Node v18. The lambda event source on serverless.yml file is as follows:

functions:
  graphql:
    handler: src/graphql-api/apollo-server.graphqlServer
    events:
      - http:
          path: graphql
          method: post
          cors: true
      - http:
          path: graphql
          method: get
          cors: true
daniel-keller commented 1 year ago

Can you provide your serverless.yml file? I suspect that you might be using a lambda event source that only supports the V1 event. As fellow Daniel, @daniel-keller mentioned, if that is the case you will need to utilize the createAPIGatewayProxyEventRequestHandler() function. Event sources that utilize V1 and V2 events are not cross compatible.

If you are following the documentation here, this should set you up to use the V2 event type. If this is incorrect please let me know and I will investigate further.

This solved the problem for me on my local while running tests. In my tests I was using a mocked event APIGatewayProxyEvent and pairing it with createAPIGatewayProxyEventV2RequestHandler which resulted in the error. Mocking a APIGatewayProxyEventV2 event solved the problem.

However, I misunderstood what V1 vs V2 api gateway events meant. V1 refers to API Gateway's REST APIs while V2 refers to the HTTP and WebSocket APIs. They are not interchangeabl. I actually have to use V1 events b/c I need features only provided by V1

daniel-keller commented 1 year ago

@Sebas1245 In the serverless yml events http are for V1 Proxy events and httpApi is for V2

This config requires using createAPIGatewayProxyEventRequestHandler

functions:
  graphql:
    handler: src/graphql-api/apollo-server.graphqlServer
    events:
      - http:    <=== these should be httpApi for createAPIGatewayProxyEventV2RequestHandler to work
          path: graphql
          method: post
          cors: true
      - http:    <====
          path: graphql
          method: get
          cors: true