-
Notifications
You must be signed in to change notification settings - Fork 421
/
Copy pathbase.py
163 lines (133 loc) · 5.29 KB
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from __future__ import annotations
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from collections.abc import Callable
class BaseRouter(ABC):
"""Abstract base class for Router (resolvers)"""
@abstractmethod
def resolver(self, type_name: str = "*", field_name: str | None = None) -> Callable:
"""
Retrieve a resolver function for a specific type and field.
Parameters
-----------
type_name: str
The name of the type.
field_name: str, optional
The name of the field (default is None).
Examples
--------
```python
from typing import Optional
from aws_lambda_powertools.event_handler import AppSyncResolver
from aws_lambda_powertools.utilities.data_classes import AppSyncResolverEvent
from aws_lambda_powertools.utilities.typing import LambdaContext
app = AppSyncResolver()
@app.resolver(type_name="Query", field_name="getPost")
def related_posts(event: AppSyncResolverEvent) -> Optional[list]:
return {"success": "ok"}
def lambda_handler(event, context: LambdaContext) -> dict:
return app.resolve(event, context)
```
Returns
-------
Callable
The resolver function.
"""
raise NotImplementedError
@abstractmethod
def batch_resolver(
self,
type_name: str = "*",
field_name: str | None = None,
raise_on_error: bool = False,
aggregate: bool = True,
) -> Callable:
"""
Retrieve a batch resolver function for a specific type and field.
Parameters
-----------
type_name: str
The name of the type.
field_name: str, optional
The name of the field (default is None).
raise_on_error: bool
A flag indicating whether to raise an error when processing batches
with failed items. Defaults to False, which means errors are handled without raising exceptions.
aggregate: bool
A flag indicating whether the batch items should be processed at once or individually.
If True (default), the batch resolver will process all items in the batch as a single event.
If False, the batch resolver will process each item in the batch individually.
Examples
--------
```python
from typing import Optional
from aws_lambda_powertools.event_handler import AppSyncResolver
from aws_lambda_powertools.utilities.data_classes import AppSyncResolverEvent
from aws_lambda_powertools.utilities.typing import LambdaContext
app = AppSyncResolver()
@app.batch_resolver(type_name="Query", field_name="getPost")
def related_posts(event: AppSyncResolverEvent, id) -> Optional[list]:
return {"post_id": id}
def lambda_handler(event, context: LambdaContext) -> dict:
return app.resolve(event, context)
```
Returns
-------
Callable
The batch resolver function.
"""
raise NotImplementedError
@abstractmethod
def async_batch_resolver(
self,
type_name: str = "*",
field_name: str | None = None,
raise_on_error: bool = False,
aggregate: bool = True,
) -> Callable:
"""
Retrieve a batch resolver function for a specific type and field and runs async.
Parameters
-----------
type_name: str
The name of the type.
field_name: str, optional
The name of the field (default is None).
raise_on_error: bool
A flag indicating whether to raise an error when processing batches
with failed items. Defaults to False, which means errors are handled without raising exceptions.
aggregate: bool
A flag indicating whether the batch items should be processed at once or individually.
If True (default), the batch resolver will process all items in the batch as a single event.
If False, the batch resolver will process each item in the batch individually.
Examples
--------
```python
from typing import Optional
from aws_lambda_powertools.event_handler import AppSyncResolver
from aws_lambda_powertools.utilities.data_classes import AppSyncResolverEvent
from aws_lambda_powertools.utilities.typing import LambdaContext
app = AppSyncResolver()
@app.async_batch_resolver(type_name="Query", field_name="getPost")
async def related_posts(event: AppSyncResolverEvent, id) -> Optional[list]:
return {"post_id": id}
def lambda_handler(event, context: LambdaContext) -> dict:
return app.resolve(event, context)
```
Returns
-------
Callable
The batch resolver function.
"""
raise NotImplementedError
@abstractmethod
def append_context(self, **additional_context) -> None:
"""
Appends context information available under any route.
Parameters
-----------
**additional_context: dict
Additional context key-value pairs to append.
"""
raise NotImplementedError