|
| 1 | +--- |
| 2 | +title: Event Handler |
| 3 | +description: Utility |
| 4 | +--- |
| 5 | + |
| 6 | +Event handler decorators for common Lambda events |
| 7 | + |
| 8 | + |
| 9 | +### AppSync Resolver Decorator |
| 10 | + |
| 11 | +> New in 1.14.0 |
| 12 | +
|
| 13 | + |
| 14 | +=== "app.py" |
| 15 | + |
| 16 | + ```python hl_lines="1-3 6 8-9 13-14 18-19 23 25" |
| 17 | + from aws_lambda_powertools.logging import Logger, correlation_paths |
| 18 | + from aws_lambda_powertools.utilities.data_classes import AppSyncResolverEvent |
| 19 | + from aws_lambda_powertools.utilities.event_handler import AppSyncResolver |
| 20 | + |
| 21 | + logger = Logger() |
| 22 | + app = AppSyncResolver() |
| 23 | + |
| 24 | + @app.resolver(type_name="Query", field_name="listLocations", include_event=True) |
| 25 | + def list_locations(event: AppSyncResolverEvent, page: int = 0, size: int = 10): |
| 26 | + # Your logic to fetch locations |
| 27 | + ... |
| 28 | + |
| 29 | + @app.resolver(type_name="Merchant", field_name="extraInfo", include_event=True) |
| 30 | + def get_extra_info(event: AppSyncResolverEvent): |
| 31 | + # Can use "event.source" to filter within the parent context |
| 32 | + ... |
| 33 | + |
| 34 | + @app.resolver(field_name="commonField") |
| 35 | + def common_field(): |
| 36 | + # Would match all fieldNames matching 'commonField' |
| 37 | + ... |
| 38 | + |
| 39 | + @logger.inject_lambda_context(correlation_id_path=correlation_paths.APPSYNC_RESOLVER) |
| 40 | + def handle(event, context): |
| 41 | + app.resolve(event, context) |
| 42 | + ``` |
| 43 | +=== "schema.graphql" |
| 44 | + |
| 45 | + ```graphql hl_lines="7-10 17-18 22-23" |
| 46 | + @model |
| 47 | + type Merchant |
| 48 | + { |
| 49 | + id: String! |
| 50 | + name: String! |
| 51 | + description: String |
| 52 | + # Resolves to `get_extra_info` |
| 53 | + extraInfo: ExtraInfo @function(name: "merchantInformation-${env}") |
| 54 | + # Resolves to `common_field` |
| 55 | + commonField: String @function(name: "merchantInformation-${env}") |
| 56 | + } |
| 57 | + |
| 58 | + type Location { |
| 59 | + id: ID! |
| 60 | + name: String! |
| 61 | + address: Address |
| 62 | + # Resolves to `common_field` |
| 63 | + commonField: String @function(name: "merchantInformation-${env}") |
| 64 | + } |
| 65 | + |
| 66 | + type Query { |
| 67 | + # List of locations resolves to `list_locations` |
| 68 | + listLocations(page: Int, size: Int): [Location] @function(name: "merchantInformation-${env}") |
| 69 | + } |
| 70 | + ``` |
| 71 | +=== "Example list_locations event" |
| 72 | + |
| 73 | + ```json hl_lines="2-7" |
| 74 | + { |
| 75 | + "typeName": "Query", |
| 76 | + "fieldName": "listLocations", |
| 77 | + "arguments": { |
| 78 | + "page": 2, |
| 79 | + "size": 1 |
| 80 | + }, |
| 81 | + "identity": { |
| 82 | + "claims": { |
| 83 | + "iat": 1615366261 |
| 84 | + ... |
| 85 | + }, |
| 86 | + "username": "mike", |
| 87 | + ... |
| 88 | + }, |
| 89 | + "request": { |
| 90 | + "headers": { |
| 91 | + "x-amzn-trace-id": "Root=1-60488877-0b0c4e6727ab2a1c545babd0", |
| 92 | + "x-forwarded-for": "127.0.0.1" |
| 93 | + ... |
| 94 | + } |
| 95 | + }, |
| 96 | + ... |
| 97 | + } |
| 98 | + ``` |
| 99 | + |
| 100 | +=== "Example get_extra_info event" |
| 101 | + |
| 102 | + ```json hl_lines="2 3" |
| 103 | + { |
| 104 | + "typeName": "Merchant", |
| 105 | + "fieldName": "extraInfo", |
| 106 | + "arguments": { |
| 107 | + }, |
| 108 | + "identity": { |
| 109 | + "claims": { |
| 110 | + "iat": 1615366261 |
| 111 | + ... |
| 112 | + }, |
| 113 | + "username": "mike", |
| 114 | + ... |
| 115 | + }, |
| 116 | + "request": { |
| 117 | + "headers": { |
| 118 | + "x-amzn-trace-id": "Root=1-60488877-0b0c4e6727ab2a1c545babd0", |
| 119 | + "x-forwarded-for": "127.0.0.1" |
| 120 | + ... |
| 121 | + } |
| 122 | + }, |
| 123 | + ... |
| 124 | + } |
| 125 | + ``` |
| 126 | + |
| 127 | +=== "Example common_field event" |
| 128 | + |
| 129 | + ```json hl_lines="2 3" |
| 130 | + { |
| 131 | + "typeName": "Merchant", |
| 132 | + "fieldName": "commonField", |
| 133 | + "arguments": { |
| 134 | + }, |
| 135 | + "identity": { |
| 136 | + "claims": { |
| 137 | + "iat": 1615366261 |
| 138 | + ... |
| 139 | + }, |
| 140 | + "username": "mike", |
| 141 | + ... |
| 142 | + }, |
| 143 | + "request": { |
| 144 | + "headers": { |
| 145 | + "x-amzn-trace-id": "Root=1-60488877-0b0c4e6727ab2a1c545babd0", |
| 146 | + "x-forwarded-for": "127.0.0.1" |
| 147 | + ... |
| 148 | + } |
| 149 | + }, |
| 150 | + ... |
| 151 | + } |
| 152 | + ``` |
0 commit comments