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

Example on how to work with SAM? #41

Closed tunesmith closed 2 years ago

tunesmith commented 2 years ago

Since the recommended approach is to export a call to a function, this doesn't play nicely with the idiomatic SAM approach. Where SAM template.yaml might have:

 HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: hello-world/
      Handler: server.graphqlHandler

This in server.ts:

export default startServerAndCreateLambdaHandler(server);

yields a

'errorType': 'Runtime.HandlerNotFound', 'errorMessage': 'server.graphqlHandler is undefined or not exported'

This approach appears to work:

export const graphHandler = startServerAndCreateLambdaHandler(server);

While this one does not:

const graphqlHandler = startServerAndCreateLambdaHandler(server);
export default graphqlHandler;

(same error as above)

export const graphqlHandler = async () => {
    return startServerAndCreateLambdaHandler(server);
}

(The above yields a

'errorType': 'TypeError', 'errorMessage': 'Wrong arguments', 'trace': ['TypeError: Wrong arguments', '    at RAPIDClient.postInvocationResponse

)

Meanwhile, the following does work:

export const graphqlHandler = async (e:APIGatewayProxyEvent, context: Context) => {
    const fn = await startServerAndCreateLambdaHandler(server);
    return fn(e, context, () => {});
}

Although I'm not sure what to do with the callback parameter.

Is there a simpler way to enable this? For SAM purposes, ideally we should be able to export something named we can refer to in the template.yaml (that isn't the output of a function).

tunesmith commented 2 years ago

I just discovered the "easy way":

template.yaml:

 HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: hello-world/
      Handler: server.default # default export

server.ts:

// as described in README
export default startServerAndCreateLambdaHandler(server);