CodeGenieApp / CodeGenieApp/serverless-express
NestJS & Unable to determine event source based on event
- Dominant language
- JavaScript
- Stars
- 5.3k
- Forks
- 676
- PR merge metrics
- No merged PRs in 30d
Description
Hi guys,
First of al thanks for the great plugin - helps a lot!
Unfortunately I have a small problem when I'm trying to use it together with NestJS and GraphQL.
When I'm executing `sls invoke local -f api` I'm getting:
```
{
"errorMessage": "Unable to determine event source based on event.",
"errorType": "Error",
"stackTrace": [
"Error: Unable to determine event source based on event.",
" at getEventSourceNameBasedOnEvent (/Users/lolo/Sites/app/node_modules/@vendia/serverless-express/src/event-sources/utils.js:127:9)",
" at proxy (/Users/lolo/Sites/app/node_modules/@vendia/serverless-express/src/configure.js:38:51)",
" at handler (/Users/lolo/Sites/app/node_modules/@vendia/serverless-express/src/configure.js:99:12)",
" at handler (/Users/lolo/Sites/app/src/events/api.ts:35:10)"
]
}
```
My setup is similar to the [example](https://github.com/vendia/serverless-express/tree/mainline/examples/basic-starter-nestjs) but of course much more advanced (I use additional GraphQL).
Below my setup:
--------
**serverless.yml**
```yaml
service: tmp-app
frameworkVersion: '3'
useDotenv: true
plugins:
- serverless-offline
package:
excludeDevDependencies: true
individually: true
provider:
name: aws
runtime: nodejs18.x
architecture: arm64
region: ${opt:region, 'eu-central-1'}
stage: ${opt:stage, 'dev'}
memorySize: 2048
versionFunctions: false
logRetentionInDays: 1
functions:
api:
handler: ./src/events/api.handler
events:
- httpApi:
path: /{proxy+}
method: ANY
```
**./src/events/api.ts**
```ts
import { ExpressAdapter } from '@nestjs/platform-express';
import serverlessExpress from '@vendia/serverless-express';
import { NestFactory } from '@nestjs/core';
import { APIGatewayProxyEventV2, Callback, Context, Handler } from 'aws-lambda';
import express from 'express';
import { AppModule } from './app.module';
let server: Handler;
export const handler: Handler = async (
event: APIGatewayProxyEventV2,
context: Context,
callback: Callback,
): Promise => {
if (!server) {
const expressApp = express();
const app = await NestFactory.create(AppModule, new ExpressAdapter(expressApp))
await app.init();
server = serverlessExpress({ app: expressApp });
}
return server(event, context, callback);
};
```
**./app.module.ts**
```ts
import { ApolloDriverConfig } from '@nestjs/apollo';
import { Module, NestModule } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
const IS_PROD = process.env.NODE_ENV === 'production';
@Module({
imports: [
GraphQLModule.forRoot({
driver: ApolloDriver,
autoSchemaFile: IS_PROD ? undefined : `${process.cwd()}/src/schema.graphql`,
typePaths: IS_PROD ? ['./**/*.graphql'] : undefined,
autoTransformHttpErrors: true,
buildSchemaOptions: {
// Refs. https://docs.nestjs.com/graphql/scalars#code-first
dateScalarMode: 'timestamp',
},
introspection: true,
installSubscriptionHandlers: true,
sortSchema: true,
}),
],
})
export class AppModule implements NestModule {}
```
**./nest-cli.json**
```json
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"deleteOutDir": true,
"assets": [
{ "include": "**/*.graphql", "watchAssets": true }
],
"plugins": [
{
"name": "@nestjs/graphql",
"options": {
"introspectComments": true
}
}
]
}
}
```
Any ideas what is wrong?
Contributor guide
Assessment
This issue has not been assessed yet.