Skip to content

Commit

Permalink
docs(recipes): add AWS lambda integration snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalLytek committed Oct 4, 2023
1 parent 798b26a commit 615680e
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 2 deletions.
35 changes: 35 additions & 0 deletions docs/aws-lambda.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
title: AWS Lambda integration
---

## Using TypeGraphQL in AWS Lambda environment

AWS Lambda environment is a bit different than a standard Node.js server deployment.

However, the only tricky part with the setup is that we need to "cache" the built schema, to save some computing time by avoiding rebuilding the schema on every request to our lambda.

So all we need to do is to assign the built schema to the local variable using the `??=` conditional assignment operator.
We can do the same thing for `ApolloServer`.

Below you you can find the full snippet for the AWS Lambda integration:

```ts
import { APIGatewayProxyHandlerV2 } from "aws-lambda";
import { ApolloServer } from "apollo-server-lambda";

let cachedSchema: GraphQLSchema | null = null;
let cachedServer: ApolloServer | null = null;

export const handler: APIGatewayProxyHandlerV2 = async (event, context, callback) => {
// build TypeGraphQL executable schema only once, then read it from local "cached" variable
cachedSchema ??= await buildSchema({
resolvers: [RecipeResolver],
});

// create the GraphQL server only once
cachedServer ??= new ApolloServer({ schema: cachedSchema });

// make a handler for `aws-lambda`
return cachedServer.createHandler({})(event, context, callback);
};
```
96 changes: 96 additions & 0 deletions website/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
"authorization": {
"title": "Authorization"
},
"aws-lambda": {
"title": "AWS Lambda integration"
},
"bootstrap": {
"title": "Bootstrapping"
},
Expand Down Expand Up @@ -551,6 +554,98 @@
"version-1.2.0-rc.1/version-1.2.0-rc.1-validation": {
"title": "Argument and Input validation",
"sidebar_label": "Validation"
},
"version-2.0.0-beta.3/version-2.0.0-beta.3-authorization": {
"title": "Authorization"
},
"version-2.0.0-beta.3/version-2.0.0-beta.3-bootstrap": {
"title": "Bootstrapping"
},
"version-2.0.0-beta.3/version-2.0.0-beta.3-browser-usage": {
"title": "Browser usage"
},
"version-2.0.0-beta.3/version-2.0.0-beta.3-complexity": {
"title": "Query complexity"
},
"version-2.0.0-beta.3/version-2.0.0-beta.3-custom-decorators": {
"title": "Custom decorators"
},
"version-2.0.0-beta.3/version-2.0.0-beta.3-dependency-injection": {
"title": "Dependency injection"
},
"version-2.0.0-beta.3/version-2.0.0-beta.3-directives": {
"title": "Directives"
},
"version-2.0.0-beta.3/version-2.0.0-beta.3-emit-schema": {
"title": "Emitting the schema SDL"
},
"version-2.0.0-beta.3/version-2.0.0-beta.3-enums": {
"title": "Enums"
},
"version-2.0.0-beta.3/version-2.0.0-beta.3-esm": {
"title": "ECMAScript Modules"
},
"version-2.0.0-beta.3/version-2.0.0-beta.3-examples": {
"title": "Examples",
"sidebar_label": "List of examples"
},
"version-2.0.0-beta.3/version-2.0.0-beta.3-extensions": {
"title": "Extensions"
},
"version-2.0.0-beta.3/version-2.0.0-beta.3-faq": {
"title": "Frequently Asked Questions"
},
"version-2.0.0-beta.3/version-2.0.0-beta.3-generic-types": {
"title": "Generic Types"
},
"version-2.0.0-beta.3/version-2.0.0-beta.3-getting-started": {
"title": "Getting started"
},
"version-2.0.0-beta.3/version-2.0.0-beta.3-inheritance": {
"title": "Inheritance"
},
"version-2.0.0-beta.3/version-2.0.0-beta.3-installation": {
"title": "Installation"
},
"version-2.0.0-beta.3/version-2.0.0-beta.3-interfaces": {
"title": "Interfaces"
},
"version-2.0.0-beta.3/version-2.0.0-beta.3-introduction": {
"title": "Introduction",
"sidebar_label": "What & Why"
},
"version-2.0.0-beta.3/version-2.0.0-beta.3-middlewares": {
"title": "Middleware and guards"
},
"version-2.0.0-beta.3/version-2.0.0-beta.3-nestjs": {
"title": "NestJS Integration",
"sidebar_label": "NestJS"
},
"version-2.0.0-beta.3/version-2.0.0-beta.3-performance": {
"title": "Performance"
},
"version-2.0.0-beta.3/version-2.0.0-beta.3-prisma": {
"title": "Prisma Integration",
"sidebar_label": "Prisma"
},
"version-2.0.0-beta.3/version-2.0.0-beta.3-resolvers": {
"title": "Resolvers"
},
"version-2.0.0-beta.3/version-2.0.0-beta.3-scalars": {
"title": "Scalars"
},
"version-2.0.0-beta.3/version-2.0.0-beta.3-subscriptions": {
"title": "Subscriptions"
},
"version-2.0.0-beta.3/version-2.0.0-beta.3-types-and-fields": {
"title": "Types and Fields"
},
"version-2.0.0-beta.3/version-2.0.0-beta.3-unions": {
"title": "Unions"
},
"version-2.0.0-beta.3/version-2.0.0-beta.3-validation": {
"title": "Argument and Input validation",
"sidebar_label": "Validation"
}
},
"links": {
Expand All @@ -568,6 +663,7 @@
"Features": "Features",
"Integrations": "Integrations",
"Others": "Others",
"Recipes": "Recipes",
"Examples": "Examples"
}
},
Expand Down
1 change: 0 additions & 1 deletion website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"postnew-release": "git restore ../docs",
"publish-gh-pages": "npx docusaurus-publish",
"rename-version": "npx docusaurus-rename-version",
"prestart": "npm run --prefix ../ gen:docs",
"start": "npx docusaurus-start"
},
"devDependencies": {
Expand Down
3 changes: 2 additions & 1 deletion website/sidebars.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"complexity"
],
"Integrations": ["prisma", "nestjs"],
"Others": ["emit-schema", "performance", "browser-usage"]
"Others": ["emit-schema", "performance"],
"Recipes": ["browser-usage", "aws-lambda"]
},
"examples": {
"Examples": ["examples"]
Expand Down

0 comments on commit 615680e

Please sign in to comment.