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
[Docs] AS Integrations AWS Lambda 2.0.0 Update (#7329)
With the v2.0.0 release of `@as-integrations/aws-lambda`, the docs
needed updates to reflect the new syntax and requirements. This PR also
describes more of the added functionality in the 2.0 release.
There was also an issue with the local event type that slipped through
the cracks the first time around due to our runtime type-parsing. The
local event we were using for testing was a `ProxyV1` event type, while
the `httpApi` spun up by `serverless` was of the `ProxyV2` type. I
normalized the example event and integration example to both be the new
`V2` event type (as its now necessary to be explicit with v2.0.0).
Fixes#7327 as `requestContext.http.method` was missing from the event
as it was not required in `ProxyV1`, but is in `ProxyV2`
@@ -60,64 +60,20 @@ const server = new ApolloServer({
60
60
});
61
61
```
62
62
63
-
```js title="server.js"
64
-
import { ApolloServer } from'@apollo/server';
65
-
66
-
// The GraphQL schema
67
-
consttypeDefs=`#graphql
68
-
type Query {
69
-
hello: String
70
-
}
71
-
`;
72
-
73
-
// A map of functions which return data for the schema.
74
-
constresolvers= {
75
-
Query: {
76
-
hello: () =>'world',
77
-
},
78
-
};
79
-
80
-
// Set up Apollo Server
81
-
constserver=newApolloServer({
82
-
typeDefs,
83
-
resolvers,
84
-
});
85
-
```
86
-
87
63
</MultiCodeBlock>
88
64
89
-
Now we can import the `startServerAndCreateLambdaHandler` function from [`@as-integrations/aws-lambda`](https://www.npmjs.com/package/@as-integrations/aws-lambda), passing in our `ApolloServer` instance:
65
+
Now we can import the `startServerAndCreateLambdaHandler` function and `handlers` object from [`@as-integrations/aws-lambda`](https://www.npmjs.com/package/@as-integrations/aws-lambda), passing in our `ApolloServer` instance:
@@ -284,42 +252,184 @@ If you ever want to remove the S3 bucket or Lambda functions that Serverless cre
284
252
serverless remove
285
253
```
286
254
287
-
## Customizing HTTP behavior
255
+
## Middleware
288
256
289
-
The `@as-integrations/aws-lambda` package is compatible with the following event types `APIGatewayProxyEvent`, `APIGatewayProxyEventV2`, `ALBEvent`. This supports a wide range of services like API Gateway HTTP Proxy APIs, API Gateway REST Proxy APIs, Lambda Function URLs, and Application Load Balancers. However, it does not let you customize HTTP behavior directly or support other AWS products that invoke Lambda functions (e.g., S3 or DynamoDB).
257
+
In order to implement event and result mutations, type-safe middleware can be passed to the `startServerAndCreateLambdaHandler` call. The API is as follows:
290
258
291
-
If you want to customize your HTTP behavior, you can couple Apollo Server's Express integration (i.e., [`expressMiddleware`](../api/express-middleware)) with the [`@vendia/serverless-express`](https://github.com/vendia/serverless-express) package. The `@vendia/serverless-express` library translates between Lambda events and Express requests. Despite their similar names, the Serverless CLI and the `@vendia/serverless-express` package are unrelated.
259
+
<MultiCodeBlock>
292
260
293
-
You can update your Apollo Server setup to the following to have a fully functioning Lambda server that works in a variety of AWS features:
261
+
```ts
294
262
295
-
<MultiCodeBlock>
263
+
import { middleware, startServerAndCreateLambdaHandler, handlers } from "@as-integrations/aws-lambda";
One use case for middleware is cookie modification. The `APIGatewayProxyStructuredResultV2` type contains a property `cookies` which can be pushed to. This allows you to set multiple `set-cookie` headers in the response.
More use-cases and API information can be found in the [library's README](https://github.com/apollo-server-integrations/apollo-server-integration-aws-lambda#middleware).
In many cases, API Gateway events will have an authorizer in front of them that contains custom state that will be used for authorization during GraphQL resolution. All of the handlers that are packaged with the library contain a generic type which allows you to explicitly extend the base event type. By passing an event with authorization information, that event type will be used during the creation of `contextValue` and for `middleware`. Below is an example, and more information can be found in the [library's README](https://github.com/apollo-server-integrations/apollo-server-integration-aws-lambda/blob/main/README.md#event-extensions).
327
+
328
+
<MultiCodeBlock>
329
+
330
+
```ts
331
+
import {
332
+
startServerAndCreateLambdaHandler,
333
+
middleware,
334
+
handlers,
335
+
} from '@as-integrations/aws-lambda';
336
+
import type { APIGatewayProxyEventV2WithLambdaAuthorizer } from 'aws-lambda';
The setup enables you to customize your HTTP behavior as needed.
351
+
## Custom request handling
352
+
353
+
In order to support all event types from AWS Lambda (including custom ones), a request handler creation utility is exposed as `handlers.createHandler(eventParser, resultGenerator)`. This function returns a fully typed request handler that can be passed as the second argument to the `startServerAndCreateLambdaHandler` call. Below is an example and the exact API is documented in the [library's README](https://github.com/apollo-server-integrations/apollo-server-integration-aws-lambda/blob/main/README.md#custom-request-handlers).
354
+
355
+
<MultiCodeBlock>
356
+
357
+
```ts
358
+
import {
359
+
startServerAndCreateLambdaHandler,
360
+
handlers,
361
+
} from '@as-integrations/aws-lambda';
362
+
import type { APIGatewayProxyEventV2 } from 'aws-lambda';
You can use the [`context` function](../data/context/#the-context-function) to get information about the current operation from the original Lambda data structures.
If you want to customize your HTTP routing behavior, you can couple Apollo Server's Express integration (i.e., [`expressMiddleware`](../api/express-middleware)) with the [`@vendia/serverless-express`](https://github.com/vendia/serverless-express) package. The `@vendia/serverless-express` library translates between Lambda events and Express requests. Despite their similar names, the Serverless CLI and the `@vendia/serverless-express` package are unrelated.
512
+
513
+
You can update your Apollo Server setup to the following to have a fully functioning Lambda server that works in a variety of AWS features:
0 commit comments