You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the interest of supporting more event types and allowing user-extensibility, the event parsing has been rearchitected. The goal with v2.0 is to allow customizability at each step in the event pipeline, leading to a higher level of Lambda event coverage (including 100% custom event requests).
8
+
9
+
## What changed?
10
+
11
+
The second parameter introduces a handler that controls parsing and output generation based on the event type you are consuming. We support 3 event types out-of-the-box: APIGatewayProxyV1/V2 and ALB. Additionally, there is a function for creating your own event parsers in case the pre-defined ones are not sufficient.
12
+
13
+
This update also introduces middleware, a great way to modify the request on the way in or update the result on the way out.
Each of these have an optional type parameter which you can use to extend the base event. This is useful if you are using Lambda functions with custom authorizers and need additional context in your events.
55
+
56
+
Creating your own event parsers is now possible with `handlers.createRequestHandler()`. Creation of custom handlers is documented in the README.
Copy file name to clipboardExpand all lines: README.md
+260-7Lines changed: 260 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,8 @@
1
1
# `@as-integrations/aws-lambda`
2
2
3
-
## Getting started: Lambda middleware
3
+
## Getting started
4
4
5
-
Apollo Server runs as a part of your Lambda handler, processing GraphQL requests. This package allows you to easily integrate Apollo Server with AWS Lambda. This integration is compatible with Lambda's API Gateway V1 (REST) and V2 (HTTP). It doesn't currently claim support for any other flavors of Lambda, though PRs are welcome!
5
+
Apollo Server runs as a part of your Lambda handler, processing GraphQL requests. This package allows you to easily integrate Apollo Server with AWS Lambda. This integration comes with built-in request handling functionality for ProxyV1, ProxyV2, and ALB events [with extensible typing](#event-extensions). You can also create your own integrations via a [Custom Handler](#custom-request-handlers) and submitted as a PR if others might find them valuable.
6
6
7
7
First, install Apollo Server, graphql-js, and the Lambda handler package:
For mutating the event before passing off to `@apollo/server` or mutating the result right before returning, middleware can be utilized.
51
+
52
+
> Note, this middleware is strictly for event and result mutations and should not be used for any GraphQL modification. For that, [plugins](https://www.apollographql.com/docs/apollo-server/builtin-plugins) from `@apollo/server` would be much better suited.
53
+
54
+
For example, if you need to set cookie headers with a V2 Proxy Result, see the following code example:
// Both event and result are intended to be mutable
75
+
async (event) => {
76
+
const cookie =awaitregenerateCookie(event);
77
+
return (result) => {
78
+
result.cookies.push(cookie);
79
+
};
80
+
},
81
+
],
82
+
},
83
+
);
84
+
```
85
+
86
+
If you want to define strictly typed middleware outside of the middleware array, the easiest way would be to extract your request handler into a variable and utilize the `typeof` keyword from Typescript. You could also manually use the `RequestHandler` type and fill in the event and result values yourself.
// cookieMiddleware will always work here as its signature is
125
+
// tied to the `requestHandler` above
126
+
cookieMiddleware,
127
+
128
+
// otherMiddleware will error if the event and result types do
129
+
// not sufficiently overlap, meaning it is your responsibility
130
+
// to keep the event types in sync, but the compiler may help
131
+
otherMiddleware,
132
+
],
133
+
});
134
+
```
135
+
136
+
## Event Extensions
137
+
138
+
Each of the provided request handler factories has a generic for you to pass a manually extended event type if you have custom authorizers, or if the event type you need has a generic you must pass yourself. For example, here is a request that allows access to the lambda authorizer:
>(), // This event will also be applied to the MiddlewareFn type
156
+
);
157
+
```
158
+
159
+
## Custom Request Handlers
160
+
161
+
When invoking a lambda manually, or when using an event source we don't currently support (feel free to create a PR), a custom request handler might be necessary. A request handler is created using the `handlers.createHandler` function which takes two function arguments `eventParser` and `resultGenerator`, and two type arguments `EventType` and `ResultType`.
162
+
163
+
### `eventParser` Argument
164
+
165
+
There are two type signatures available for parsing events:
166
+
167
+
#### Method A: Helper Object
168
+
169
+
This helper object has 4 properties that will complete a full parsing chain, and abstracts some of the work required to coerce the incoming event into a `HTTPGraphQLRequest`. This is the recommended way of parsing events.
Returns the raw query param string from the request. If the request comes in as a pre-mapped type, you may need to use `URLSearchParams` to re-stringify it.
180
+
181
+
Example return value: `foo=1&bar=2`
182
+
183
+
##### `parseHeaders(event: EventType): HeaderMap`
184
+
185
+
Import from here: `import {HeaderMap} from "@apollo/server"`;
186
+
187
+
Return an Apollo Server header map from the event. `HeaderMap` automatically normalizes casing for you.
Return a plaintext body. Be sure to parse out any base64 or charset encoding. Headers are provided here for convenience as some body parsing might be dependent on `content-type`
192
+
193
+
#### Method B: Parser Function
194
+
195
+
If the helper object is too restrictive for your use-case, the other option is to create a function with `(event: EventType): HTTPGraphQLRequest` as the signature. Here you can do any parsing and it is your responsibility to create a valid `HTTPGraphQLRequest`.
196
+
197
+
### `resultGenerator` Argument
198
+
199
+
There are two possible result types, `success` and `error`, and they are to be defined as function properties on an object. Middleware will _always_ run, regardless if the generated result was from a success or error. The properties have the following signatures:
Given a complete response, generate the desired result type.
204
+
205
+
##### `error(e: unknown): ResultType`
206
+
207
+
Given an unknown type error, generate a result. If you want to create a basic parser that captures everything, utilize the instanceof type guard from Typescript.
208
+
209
+
```typescript
210
+
error(e) {
211
+
if(einstanceofError) {
212
+
return {
213
+
...
214
+
}
215
+
}
216
+
// If error cannot be determined, panic and use lambda's default error handler
217
+
// Might be advantageous to add extra logging here so unexpected errors can be properly handled later
0 commit comments