Skip to content

Commit 80befe9

Browse files
authored
docs(appsync): add mutation example and infrastructure fix (#1964)
Co-authored by: Tobias Bruckert <tb102122@@users.noreply.github.com>
1 parent dfd4391 commit 80befe9

File tree

7 files changed

+119
-28
lines changed

7 files changed

+119
-28
lines changed

docs/core/event_handler/appsync.md

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ This is the sample infrastructure we are using for the initial examples with a A
4646

4747
You can define your functions to match GraphQL types and fields with the `app.resolver()` decorator.
4848

49-
Here's an example where we have two separate functions to resolve `getTodo` and `listTodos` fields within the `Query` type. For completion, we use Scalar type utilities to generate the right output based on our schema definition.
49+
???+ question "What is a type and field?"
50+
A type would be a top-level **GraphQL Type** like `Query`, `Mutation`, `Todo`. A **GraphQL Field** would be `listTodos` under `Query`, `createTodo` under `Mutation`, etc.
51+
52+
Here's an example with two separate functions to resolve `getTodo` and `listTodos` fields within the `Query` type. For completion, we use [Scalar type utilities](#scalar-functions) to generate the right output based on our schema definition.
5053

5154
???+ important
5255
GraphQL arguments are passed as function keyword arguments.
@@ -58,27 +61,35 @@ Here's an example where we have two separate functions to resolve `getTodo` and
5861

5962
=== "getting_started_graphql_api_resolver.py"
6063

61-
```python hl_lines="14 20 30 32-33 42 44 55"
64+
```python hl_lines="14 21 31 33-34 43 45 53 55 66"
6265
--8<-- "examples/event_handler_graphql/src/getting_started_graphql_api_resolver.py"
6366
```
6467

6568
=== "getting_started_schema.graphql"
6669

67-
```typescript hl_lines="6-7"
70+
```typescript hl_lines="7-9 13"
6871
--8<-- "examples/event_handler_graphql/src/getting_started_schema.graphql"
6972
```
7073

71-
=== "getting_started_get_todo.json"
74+
=== "sample events"
7275

73-
```json hl_lines="2-3"
74-
--8<-- "examples/event_handler_graphql/src/getting_started_get_todo.json"
75-
```
76+
=== "getting_started_get_todo.json"
7677

77-
=== "getting_started_list_todos.json"
78+
```json hl_lines="2-3 42"
79+
--8<-- "examples/event_handler_graphql/src/getting_started_get_todo.json"
80+
```
7881

79-
```json hl_lines="2 40 42"
80-
--8<-- "examples/event_handler_graphql/src/getting_started_list_todos.json"
81-
```
82+
=== "getting_started_list_todos.json"
83+
84+
```json hl_lines="2 40"
85+
--8<-- "examples/event_handler_graphql/src/getting_started_list_todos.json"
86+
```
87+
88+
=== "getting_started_create_todo.json"
89+
90+
```json hl_lines="2 48 49"
91+
--8<-- "examples/event_handler_graphql/src/getting_started_create_todo.json"
92+
```
8293

8394
### Scalar functions
8495

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version = 0.1
2+
[default]
3+
[default.deploy]
4+
[default.deploy.parameters]
5+
stack_name = "pt-graphql-getting-started"
6+
s3_bucket = "aws-sam-cli-managed-default-samclisourcebucket-1pssy5gdxqcao"
7+
s3_prefix = "pt-graphql-getting-started"
8+
region = "eu-west-1"
9+
capabilities = "CAPABILITY_IAM"
10+
image_repositories = []

examples/event_handler_graphql/sam/template.yaml

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,12 @@ Resources:
7070
Type: "AWS::AppSync::GraphQLSchema"
7171
Properties:
7272
ApiId: !GetAtt TodosApi.ApiId
73-
Definition: |
74-
schema {
75-
query:Query
76-
}
77-
78-
type Query {
79-
getTodo(id: ID!): Todo
80-
listTodos: [Todo]
81-
}
82-
83-
type Todo {
84-
id: ID!
85-
userId: String
86-
title: String
87-
completed: Boolean
88-
}
73+
DefinitionS3Location: ../src/getting_started_schema.graphql
74+
Metadata:
75+
cfn-lint:
76+
config:
77+
ignore_checks:
78+
- W3002 # allow relative path in DefinitionS3Location
8979

9080
# Lambda Direct Data Source and Resolver
9181

@@ -115,10 +105,18 @@ Resources:
115105
FieldName: "getTodo"
116106
DataSourceName: !GetAtt TodosFunctionDataSource.Name
117107

108+
CreateTodoResolver:
109+
Type: "AWS::AppSync::Resolver"
110+
Properties:
111+
ApiId: !GetAtt TodosApi.ApiId
112+
TypeName: "Mutation"
113+
FieldName: "createTodo"
114+
DataSourceName: !GetAtt TodosFunctionDataSource.Name
115+
118116
Outputs:
119117
TodosFunction:
120118
Description: "Hello World Lambda Function ARN"
121119
Value: !GetAtt TodosFunction.Arn
122120

123121
TodosApi:
124-
Value: !GetAtt TodosApi.Arn
122+
Value: !GetAtt TodosApi.GraphQLUrl
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"arguments": {
3+
"title": "Sample todo mutation"
4+
},
5+
"identity": null,
6+
"source": null,
7+
"request": {
8+
"headers": {
9+
"x-forwarded-for": "203.0.113.1, 203.0.113.18",
10+
"cloudfront-viewer-country": "NL",
11+
"cloudfront-is-tablet-viewer": "false",
12+
"x-amzn-requestid": "fdc4f30b-44c2-475d-b2f9-9da0778d5275",
13+
"via": "2.0 f655cacd0d6f7c5dc935ea687af6f3c0.cloudfront.net (CloudFront)",
14+
"cloudfront-forwarded-proto": "https",
15+
"origin": "https://eu-west-1.console.aws.amazon.com",
16+
"content-length": "166",
17+
"x-forwarded-proto": "https",
18+
"accept-language": "en-US,en;q=0.5",
19+
"host": "kiuqayvn4jhhzio6whpnk7xj3a.appsync-api.eu-west-1.amazonaws.com",
20+
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Firefox/102.0",
21+
"cloudfront-is-mobile-viewer": "false",
22+
"accept": "application/json, text/plain, */*",
23+
"cloudfront-viewer-asn": "1136",
24+
"cloudfront-is-smarttv-viewer": "false",
25+
"accept-encoding": "gzip, deflate, br",
26+
"referer": "https://eu-west-1.console.aws.amazon.com/",
27+
"content-type": "application/json",
28+
"x-api-key": "da2-vsqnxwyzgzf4nh6kvoaidtvs7y",
29+
"sec-fetch-mode": "cors",
30+
"x-amz-cf-id": "0kxqijFPsbGSWJ1u3Z_sUS4Wu2hRoG_2T77aJPuoh_Q4bXAB3x0a3g==",
31+
"x-amzn-trace-id": "Root=1-63fef2cf-6d566e9f4a35b99e6212388e",
32+
"sec-fetch-dest": "empty",
33+
"x-amz-user-agent": "AWS-Console-AppSync/",
34+
"cloudfront-is-desktop-viewer": "true",
35+
"sec-fetch-site": "cross-site",
36+
"x-forwarded-port": "443"
37+
},
38+
"domainName": null
39+
},
40+
"prev": null,
41+
"info": {
42+
"selectionSetList": [
43+
"id",
44+
"title",
45+
"completed"
46+
],
47+
"selectionSetGraphQL": "{\n id\n title\n completed\n}",
48+
"fieldName": "createTodo",
49+
"parentTypeName": "Mutation",
50+
"variables": {}
51+
},
52+
"stash": {}
53+
}

examples/event_handler_graphql/src/getting_started_graphql_api_resolver.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from aws_lambda_powertools import Logger, Tracer
1414
from aws_lambda_powertools.event_handler import AppSyncResolver
1515
from aws_lambda_powertools.logging import correlation_paths
16+
from aws_lambda_powertools.utilities.data_classes.appsync import scalar_types_utils
1617
from aws_lambda_powertools.utilities.typing import LambdaContext
1718

1819
tracer = Tracer()
@@ -49,6 +50,16 @@ def list_todos() -> List[Todo]:
4950
return todos.json()[:10]
5051

5152

53+
@app.resolver(type_name="Mutation", field_name="createTodo")
54+
@tracer.capture_method
55+
def create_todo(title: str) -> Todo:
56+
payload = {"userId": scalar_types_utils.make_id(), "title": title, "completed": False} # dummy UUID str
57+
todo: Response = requests.post("https://jsonplaceholder.typicode.com/todos", json=payload)
58+
todo.raise_for_status()
59+
60+
return todo.json()
61+
62+
5263
@logger.inject_lambda_context(correlation_id_path=correlation_paths.APPSYNC_RESOLVER)
5364
@tracer.capture_lambda_handler
5465
def lambda_handler(event: dict, context: LambdaContext) -> dict:

examples/event_handler_graphql/src/getting_started_schema.graphql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
schema {
22
query: Query
3+
mutation: Mutation
34
}
45

56
type Query {
7+
# these are fields you can attach resolvers to (field: Query, field: getTodo)
68
getTodo(id: ID!): Todo
79
listTodos: [Todo]
810
}
911

12+
type Mutation {
13+
createTodo(title: String!): Todo
14+
}
15+
1016
type Todo {
1117
id: ID!
1218
userId: String
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
aws-lambda-powertools[tracer]
2+
requests

0 commit comments

Comments
 (0)