Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apollo GraphQL Lambda Handler Cannot read property 'method' of undefined

I am trying to run Apollo GraphQL server inside my AWS lambda. I'm using the library from here. I'm also using CDK to deploy my lambda and the REST API Gateway.

My infrastructure is as follows:

const helloFunction = new NodejsFunction(this, 'lambda', {
    entry: path.join(__dirname, "lambda.ts"),
    handler: "handler"
});

new LambdaRestApi(this, 'apigw', {
    handler: helloFunction,
});

The lambda implementation is as follows:

const typeDefs = `#graphql
  type Query {
  hello: String
}`;

const resolvers = {
    Query: {
        hello: () => 'world',
    },
};


const server = new ApolloServer({
    typeDefs,
    resolvers,
    introspection: true,
})

console.log('###? running lambda')

export const handler = startServerAndCreateLambdaHandler(
    server,
    handlers.createAPIGatewayProxyEventV2RequestHandler(), {
        middleware: [
            async (event) => {
                console.log('###? received event=' + JSON.stringify(event, null, 2))
                return async (result) => {
                    console.log(("###? result=" + JSON.stringify(result, null, 2)))
                    result
                }
            }
        ]
    });

When I POST to my endpoint with the appropriate query I get this error:

{
  "statusCode": 400,
  "body": "Cannot read property 'method' of undefined"
}

I'm seeing my logging inside the lambda as expected and I can confirm the error is being returned in the 'result' from within startServerAndCreateLambdaHandler(). This code is based on the example for the @as-integrations/aws-lambda library. I don't understand why this is failing.

like image 863
Przemek Lach Avatar asked Sep 02 '26 17:09

Przemek Lach


1 Answers

Need to use:

handlers.createAPIGatewayProxyEventRequestHandler()

Instead of:

 handlers.createAPIGatewayProxyEventV2RequestHandler()

So final code is:

export const handler = startServerAndCreateLambdaHandler(
    server,
    handlers.createAPIGatewayProxyEventRequestHandler(),
    {
      middleware: [
        async (event) => {
          console.log('###? received event=' + JSON.stringify(event))
        }
      ]
  }
);
like image 152
Przemek Lach Avatar answered Sep 04 '26 20:09

Przemek Lach



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!