Skip to content

Commit b97ab20

Browse files
committed
doc: includeRouter & appendContext method doc
1 parent 9f4bbb1 commit b97ab20

File tree

7 files changed

+125
-0
lines changed

7 files changed

+125
-0
lines changed

docs/features/event-handler/appsync-graphql.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,64 @@ Here's a table with their related scalar as a quick reference:
114114

115115
## Advanced
116116

117+
### Split operations with Router
118+
119+
As you grow the number of related GraphQL operations a given Lambda function should handle, it is natural to split them into separate files to ease maintenance - That's when the `Router` feature comes handy.
120+
121+
Let's assume you have `app.ts` as your Lambda function entrypoint and routes in `postRouter.ts` and `userRouter.ts`. This is how you'd use the `Router` feature.
122+
123+
=== "postRouter.ts"
124+
125+
We import **Router** instead of **AppSyncGraphQLResolver**; syntax wise is exactly the same.
126+
127+
```typescript hl_lines="1 3"
128+
--8<-- "examples/snippets/event-handler/appsync-graphql/postRouter.ts"
129+
```
130+
131+
=== "userRouter.ts"
132+
133+
We import **Router** instead of **AppSyncGraphQLResolver**; syntax wise is exactly the same.
134+
135+
```typescript hl_lines="1 3"
136+
--8<-- "examples/snippets/event-handler/appsync-graphql/userRouter.ts"
137+
```
138+
139+
=== "app.ts"
140+
141+
We use `includeRouter` method and include all operations registered in the router instances.
142+
143+
```typescript hl_lines="2-3 7"
144+
--8<-- "examples/snippets/event-handler/appsync-graphql/splitRouter.ts"
145+
```
146+
147+
#### Sharing contextual data
148+
149+
You can use `appendContext` when you want to share data between your App and Router instances. Any data you share will be available via the `sharedContext` parameter in your resolver handlers.
150+
151+
???+ warning
152+
For safety, we clear the context after each invocation.
153+
154+
???+ tip
155+
This can also be useful for injecting contextual information before a request is processed.
156+
157+
=== "app.ts"
158+
159+
```typescript hl_lines="9"
160+
--8<-- "examples/snippets/event-handler/appsync-graphql/appendContext.ts"
161+
```
162+
163+
=== "postRouter.ts"
164+
165+
```typescript hl_lines="5-8"
166+
--8<-- "examples/snippets/event-handler/appsync-graphql/postRouterWithContext.ts"
167+
```
168+
169+
=== "userRouter.ts"
170+
171+
```typescript hl_lines="5-8"
172+
--8<-- "examples/snippets/event-handler/appsync-graphql/userRouterWithContext.ts"
173+
```
174+
117175
### Nested mappings
118176

119177
!!! note
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';
2+
import { postRouter } from './postRouter';
3+
import { usersRouter } from './userRouter';
4+
5+
const app = new AppSyncGraphQLResolver();
6+
7+
app.includeRouter([postRouter, usersRouter]);
8+
9+
app.appendContext({ requestId: crypto.randomUUID() });
10+
11+
export const handler = async (event, context) => app.resolve(event, context);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Router } from '@aws-lambda-powertools/event-handler/appsync-graphql';
2+
3+
const postRouter = new Router();
4+
5+
postRouter.onQuery('getPosts', async () => {
6+
return [{ id: 1, title: 'First post', content: 'Hello world!' }];
7+
});
8+
9+
postRouter.onMutation('createPost', async ({ title, content }) => {
10+
return {
11+
id: Date.now(),
12+
title,
13+
content,
14+
createdAt: new Date().toISOString(),
15+
};
16+
});
17+
18+
export { postRouter };
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Router } from '@aws-lambda-powertools/event-handler/appsync-graphql';
2+
3+
const postRouter = new Router();
4+
5+
postRouter.onQuery('getPosts', async (args, { sharedContext }) => {
6+
const requestId = sharedContext?.get('requestId');
7+
return [{ id: 1, title: 'First post', content: 'Hello world!', requestId }];
8+
});
9+
10+
export { postRouter };
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';
2+
import { postRouter } from './postRouter';
3+
import { userRouter } from './userRouter';
4+
5+
const app = new AppSyncGraphQLResolver();
6+
7+
app.includeRouter([postRouter, userRouter]);
8+
9+
export const handler = async (event, context) => app.resolve(event, context);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Router } from '@aws-lambda-powertools/event-handler/appsync-graphql';
2+
3+
const userRouter = new Router();
4+
5+
userRouter.onQuery('getUsers', async () => {
6+
return [{ id: 1, name: 'John Doe', email: '[email protected]' }];
7+
});
8+
9+
export { userRouter };
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Router } from '@aws-lambda-powertools/event-handler/appsync-graphql';
2+
3+
const usersRouter = new Router();
4+
5+
usersRouter.onQuery('getUsers', async (args, { sharedContext }) => {
6+
const requestId = sharedContext?.get('requestId');
7+
return [{ id: 1, name: 'John Doe', email: '[email protected]', requestId }];
8+
});
9+
10+
export { usersRouter };

0 commit comments

Comments
 (0)