Skip to content

Commit d38774d

Browse files
BlenderDudetrevor-scheergithub-actions[bot]
authored
Version 3 (#115)
* Drop support for Node v14, start testing against Node v20 (#110) * Drop support for Node v14, start testing against Node v20 * Build release PRs on version-* branches * latest node@16 types * Updated middleware execution to provide new functionality (#91) * Updated middleware execution to provide new functionality * Add missing headers to the changeset * Remove context modification from result middleware * Update from review * Added new README sections and changelog message * Spelling * Prettier * Version Packages (#114) Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> --------- Co-authored-by: Trevor Scheer <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 493f2dc commit d38774d

File tree

12 files changed

+256
-39
lines changed

12 files changed

+256
-39
lines changed

.circleci/config.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,16 @@ jobs:
6767
- run: npm run spell-check
6868

6969
workflows:
70-
version: 2
7170
Build:
7271
jobs:
7372
- NodeJS:
7473
name: NodeJS << matrix.node-version >>
7574
matrix:
7675
parameters:
7776
node-version:
78-
- "14"
7977
- "16"
8078
- "18"
79+
- "20"
8180
- Prettier
8281
- Lint
8382
- Spell Check

.github/workflows/release-pr.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ on:
44
push:
55
branches:
66
- main
7+
- version-*
78

89
jobs:
910
release:

CHANGELOG.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,46 @@
11
# @as-integrations/aws-lambda
22

3+
## 3.0.0
4+
5+
### Major Changes
6+
7+
- [#110](https://github.com/apollo-server-integrations/apollo-server-integration-aws-lambda/pull/110) [`e76fc08`](https://github.com/apollo-server-integrations/apollo-server-integration-aws-lambda/commit/e76fc08da5152f1bfb9bae3e29033d714474fd33) Thanks [@trevor-scheer](https://github.com/trevor-scheer)! - Drop support for Node v14
8+
9+
### Minor Changes
10+
11+
- [#91](https://github.com/apollo-server-integrations/apollo-server-integration-aws-lambda/pull/91) [`71c94df`](https://github.com/apollo-server-integrations/apollo-server-integration-aws-lambda/commit/71c94dfc96aa6f3a3d08928162f7480be375a31e) Thanks [@BlenderDude](https://github.com/BlenderDude)! - ## Short circuit middleware execution
12+
13+
You can now opt to return a Lambda result object directly from the middleware. This will cancel the middleware chain, bypass GraphQL request processing, and immediately return the Lambda result.
14+
15+
Example
16+
17+
```ts
18+
export const handler = startServerAndCreateLambdaHandler(
19+
server,
20+
handlers.createAPIGatewayProxyEventV2RequestHandler(),
21+
{
22+
context: async () => {
23+
return {};
24+
},
25+
middleware: [
26+
async (event) => {
27+
const psk = Buffer.from('SuperSecretPSK');
28+
const token = Buffer.from(event.headers['X-Auth-Token']);
29+
if (
30+
psk.byteLength !== token.byteLength ||
31+
crypto.timingSafeEqual(psk, token)
32+
) {
33+
return {
34+
statusCode: '403',
35+
body: 'Forbidden',
36+
};
37+
}
38+
},
39+
],
40+
},
41+
);
42+
```
43+
344
## 2.0.1
445

546
### Patch Changes

README.md

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
// The GraphQL schema
2323
const typeDefs = `#graphql
2424
type Query {
25-
hello: String
25+
hello: String!
2626
}
2727
`;
2828

@@ -45,6 +45,59 @@ export default startServerAndCreateLambdaHandler(
4545
);
4646
```
4747

48+
## Context
49+
50+
As with all Apollo Server 4 integrations, the context resolution is done in the integration. For the Lambda integration, it will look like the following:
51+
52+
```ts
53+
import { ApolloServer } from '@apollo/server';
54+
import {
55+
startServerAndCreateLambdaHandler,
56+
handlers,
57+
} from '@as-integrations/aws-lambda';
58+
59+
type ContextValue = {
60+
isAuthenticated: boolean;
61+
};
62+
63+
// The GraphQL schema
64+
const typeDefs = `#graphql
65+
type Query {
66+
hello: String!
67+
isAuthenticated: Boolean!
68+
}
69+
`;
70+
71+
// Set up Apollo Server
72+
const server = new ApolloServer<ContextValue>({
73+
typeDefs,
74+
resolvers: {
75+
Query: {
76+
hello: () => 'world',
77+
isAuthenticated: (root, args, context) => {
78+
// For context typing to be valid one of the following must be implemented
79+
// 1. `resolvers` defined inline in the server config (not particularly scalable, but works)
80+
// 2. Add the type in the resolver function. ex. `(root, args, context: ContextValue)`
81+
// 3. Propagate the type from an outside definition like GraphQL Codegen
82+
return context.isAuthenticated;
83+
},
84+
},
85+
},
86+
});
87+
88+
export default startServerAndCreateLambdaHandler(
89+
server,
90+
handlers.createAPIGatewayProxyEventV2RequestHandler({
91+
context: async ({ event }) => {
92+
// Do some parsing on the event (parse JWT, cookie, auth header, etc.)
93+
return {
94+
isAuthenticated: true,
95+
};
96+
},
97+
}),
98+
);
99+
```
100+
48101
## Middleware
49102

50103
For mutating the event before passing off to `@apollo/server` or mutating the result right before returning, middleware can be utilized.
@@ -83,6 +136,8 @@ export default startServerAndCreateLambdaHandler(
83136
);
84137
```
85138

139+
### Middleware Typing
140+
86141
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.
87142

88143
```typescript
@@ -133,6 +188,47 @@ export default startServerAndCreateLambdaHandler(server, requestHandler, {
133188
});
134189
```
135190

191+
### Middleware Short Circuit
192+
193+
In some situations, a middleware function might require the execution end before reaching Apollo Server. This might be a global auth guard or session token lookup.
194+
195+
To achieve this, the request middleware function accepts `ResultType` or `Promise<ResultType>` as a return type. Should middleware resolve to such a value, that result is returned and no further execution occurs.
196+
197+
```typescript
198+
import {
199+
startServerAndCreateLambdaHandler,
200+
middleware,
201+
handlers,
202+
} from '@as-integrations/aws-lambda';
203+
import type {
204+
APIGatewayProxyEventV2,
205+
APIGatewayProxyStructuredResultV2,
206+
} from 'aws-lambda';
207+
import { server } from './server';
208+
209+
const requestHandler = handlers.createAPIGatewayProxyEventV2RequestHandler();
210+
211+
// Utilizing typeof
212+
const sessionMiddleware: middleware.MiddlewareFn<typeof requestHandler> = (
213+
event,
214+
) => {
215+
// ... check session
216+
if (!event.headers['X-Session-Key']) {
217+
// If header doesn't exist, return early
218+
return {
219+
statusCode: 401
220+
body: 'Unauthorized'
221+
}
222+
}
223+
};
224+
225+
export default startServerAndCreateLambdaHandler(server, requestHandler, {
226+
middleware: [
227+
sessionMiddleware,
228+
],
229+
});
230+
```
231+
136232
## Event Extensions
137233

138234
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:

cspell-dict.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ unawaited
1212
vendia
1313
withrequired
1414
typeof
15-
instanceof
15+
instanceof
16+
codegen

package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@as-integrations/aws-lambda",
33
"description": "An Apollo Server integration for hosting on AWS Lambda",
4-
"version": "2.0.1",
4+
"version": "3.0.0",
55
"author": "Michael Watson & Daniel Abdelsamed",
66
"license": "MIT",
77
"repository": {
@@ -15,7 +15,7 @@
1515
"main": "dist/index.js",
1616
"types": "dist/index.d.ts",
1717
"engines": {
18-
"node": ">=14.0"
18+
"node": ">=16.0"
1919
},
2020
"scripts": {
2121
"build": "tsc --build tsconfig.build.json",
@@ -40,7 +40,7 @@
4040
"@changesets/changelog-github": "0.4.8",
4141
"@changesets/cli": "2.26.2",
4242
"@types/jest": "29.5.3",
43-
"@types/node": "14.18.54",
43+
"@types/node": "16.18.39",
4444
"@typescript-eslint/eslint-plugin": "5.62.0",
4545
"@typescript-eslint/parser": "5.62.0",
4646
"cspell": "6.31.2",

renovate.json5

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
// versions we support.
2222
{
2323
"matchPackageNames": ["@types/node"],
24-
"allowedVersions": "14.x"
24+
"allowedVersions": "16.x"
2525
},
2626
],
2727
}

0 commit comments

Comments
 (0)