Apollo GraphQL Lambda Handler Cannot read property 'method' of undefined
Asked Answered
P

1

6

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.

Protrusive answered 29/1, 2023 at 22:37 Comment(3)
I have several Apollo GraphQL lambdas running and I don't recognize a single line of the above code except for the typeDefs! (this kind of blows my mind) Which version of Apollo server are you using? Are you using the serverless package to deploy?Gambado
I am using "@apollo/server": "^4.3.2" and "@as-integrations/aws-lambda": "^2.0.0". "aws-cdk": "2.61.1" for deployment. I'm following the instructions here: npmjs.com/package/@as-integrations/aws-lambdaProtrusive
Hey, I'm using the same code, and having issue with API Gateway, when I test from AWS console requests works, but testing from curl I'm getting Forbidden error. Could you share please your cloudformation template? Probably I'm missing something for permissions.Suricate
P
13

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))
        }
      ]
  }
);
Protrusive answered 3/2, 2023 at 19:8 Comment(1)
Thanks ! this worked. Can you explain ?Natch

© 2022 - 2024 — McMap. All rights reserved.