@@ -46,7 +46,7 @@ Resources:
46
46
type Mutation {
47
47
saveNote(NoteId: ID!, title: String!, content: String!): Note!
48
48
}
49
- type Schema {
49
+ schema {
50
50
query: Query
51
51
mutation: Mutation
52
52
}
@@ -62,6 +62,46 @@ Resources:
62
62
TableName : !Ref NotesTable
63
63
AwsRegion : !Sub ${AWS::Region}
64
64
65
+ GetNoteResolver :
66
+ Type : AWS::AppSync::Resolver
67
+ DependsOn : ApiSchema
68
+ Properties :
69
+ ApiId : !GetAtt AppSyncApi.ApiId
70
+ DataSourceName : !GetAtt NotesTableDataSource.Name
71
+ TypeName : Query
72
+ FieldName : getNote
73
+ RequestMappingTemplate : |
74
+ {
75
+ "version": "2017-02-28",
76
+ "operation": "GetItem",
77
+ "key": {
78
+ "NoteId": $util.dynamodb.toDynamoDBJson($context.arguments.NoteId)
79
+ }
80
+ }
81
+ ResponseMappingTemplate : $util.toJson($context.result)
82
+
83
+ SaveNoteResolver :
84
+ Type : AWS::AppSync::Resolver
85
+ DependsOn : ApiSchema
86
+ Properties :
87
+ ApiId : !GetAtt AppSyncApi.ApiId
88
+ TypeName : Mutation
89
+ FieldName : saveNote
90
+ DataSourceName : !GetAtt NotesTableDataSource.Name
91
+ RequestMappingTemplate : |
92
+ {
93
+ "version": "2017-02-28",
94
+ "operation": "PutItem",
95
+ "key": {
96
+ "NoteId": $util.dynamodb.toDynamoDBJson($context.arguments.NoteId)
97
+ },
98
+ "attributeValues": {
99
+ "title": $util.dynamodb.toDynamoDBJson($context.arguments.title),
100
+ "content": $util.dynamodb.toDynamoDBJson($context.arguments.content)
101
+ }
102
+ }
103
+ ResponseMappingTemplate : $util.toJson($context.result)
104
+
65
105
DataSourceToTableConnector :
66
106
Type : AWS::Serverless::Connector
67
107
Properties :
@@ -129,6 +169,7 @@ Resources:
129
169
req.end();
130
170
});
131
171
172
+
132
173
const makeRequest = async (queryName) => {
133
174
const options = {
134
175
method: "POST",
@@ -139,65 +180,27 @@ Resources:
139
180
timeout: 10000, // ms
140
181
};
141
182
142
- let statusCode;
143
- let body;
144
- let response;
145
-
146
- try {
147
- response = await fetch(process.env.GRAPHQL_URL, options);
148
- body = JSON.parse(response);
149
- const data = body.data?.[queryName];
150
- const hasNoErrors = body.errors === undefined;
151
- const allFieldsAreSet =
152
- data?.title === "1st note" && data?.content === "some note";
153
- statusCode = hasNoErrors && allFieldsAreSet ? 200 : 400;
154
- if (hasNoErrors) {
155
- body = body.data;
156
- } else {
157
- body = {
158
- [queryName]: {
159
- errors: body.errors,
160
- },
161
- };
162
- }
163
- } catch (error) {
164
- statusCode = 400;
165
- body = {
166
- [queryName]: {
167
- errors: [
168
- {
169
- status: response.status,
170
- message: error.message,
171
- stack: error.stack,
172
- },
173
- ],
174
- },
175
- };
183
+ const response = await fetch(process.env.GRAPHQL_URL, options);
184
+ let body = JSON.parse(response);
185
+ const data = body.data?.[queryName];
186
+
187
+ if (body.errors !== undefined) {
188
+ throw JSON.stringify(body.errors);
176
189
}
177
- return {
178
- statusCode,
179
- body,
180
- };
190
+
191
+ if (data?.title !== "1st note" || data?.content !== "some note") {
192
+ throw new Error(
193
+ `${queryName} error: '${data?.title}' must be '1st note', '${data?.content}' must be 'some note'`);
194
+ }
195
+
196
+ return body.data;
181
197
};
182
198
183
- let response = await makeRequest("saveNote");
184
- if (response.statusCode !== 200) {
185
- return {
186
- StatusCode: response.statusCode,
187
- Body: response.body,
188
- };
189
- }
190
- let body = response.body;
191
-
192
- response = await makeRequest("getNote");
193
- body = { ...body, ...response.body };
194
199
195
- return {
196
- StatusCode: response.statusCode,
197
- Body: body,
198
- };
200
+ const saveResponse = await makeRequest("saveNote");
201
+ const getResponse = await makeRequest("getNote");
202
+ return { ...saveResponse, ...getResponse };
199
203
};
200
204
201
-
202
205
Metadata :
203
206
SamTransformTest : true
0 commit comments