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
@@ -60,30 +60,6 @@ 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
65
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:
@@ -92,32 +68,12 @@ Now we can import the `startServerAndCreateLambdaHandler` function from [`@as-in
@@ -284,42 +252,177 @@ 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 impelment 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
+
```ts
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
+
import { middleware, startServerAndCreateLambdaHandler, handlers } from "@as-integrations/aws-lambda";
// event is intended to be mutable and can be updated here
268
+
269
+
// optional result mutation, otherwise function returns `Promise<void>`
270
+
return async (result) => {
271
+
// result is intended to be mutable and can be updated here
272
+
273
+
// `Promise<void>` return
274
+
}
275
+
}
276
+
277
+
startServerAndCreateLambdaHandler(..., ..., {
278
+
middleware: [middlewareFn],
279
+
})
280
+
```
281
+
282
+
An example use case would be for cookie modificaiton. The `APIGatewayProxyStructuredResultV2` type contains a property `cookies` that then mutate to multiple `Set-Cookie` headers.
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 infront of them that contains custom state that will be used for authorization during GraphQL resolution. All of the handlers that are packged 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).
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).
The setup enables you to customize your HTTP behavior as needed.
321
423
322
-
## Using request information
424
+
425
+
## Using event information
323
426
324
427
You can use the [`context` function](../data/context/#the-context-function) to get information about the current operation from the original Lambda data structures.
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).
505
+
506
+
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.
507
+
508
+
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