-
-
Notifications
You must be signed in to change notification settings - Fork 674
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(recipes): add AWS lambda integration snippet
- Loading branch information
1 parent
798b26a
commit 615680e
Showing
4 changed files
with
133 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters