Skip to content

Commit 517e692

Browse files
authored
fix: integ test for AppSync DataSource -> DDB connector (#3049)
1 parent eb5fc39 commit 517e692

File tree

2 files changed

+66
-55
lines changed

2 files changed

+66
-55
lines changed

integration/resources/expected/combination/connector_appsync_to_table.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,13 @@
3434
{
3535
"LogicalResourceId": "DataSourceToTableConnectorPolicy",
3636
"ResourceType": "AWS::IAM::ManagedPolicy"
37+
},
38+
{
39+
"LogicalResourceId": "SaveNoteResolver",
40+
"ResourceType": "AWS::AppSync::Resolver"
41+
},
42+
{
43+
"LogicalResourceId": "GetNoteResolver",
44+
"ResourceType": "AWS::AppSync::Resolver"
3745
}
3846
]

integration/resources/templates/combination/connector_appsync_to_table.yaml

Lines changed: 58 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Resources:
4646
type Mutation {
4747
saveNote(NoteId: ID!, title: String!, content: String!): Note!
4848
}
49-
type Schema {
49+
schema {
5050
query: Query
5151
mutation: Mutation
5252
}
@@ -62,6 +62,46 @@ Resources:
6262
TableName: !Ref NotesTable
6363
AwsRegion: !Sub ${AWS::Region}
6464

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+
65105
DataSourceToTableConnector:
66106
Type: AWS::Serverless::Connector
67107
Properties:
@@ -129,6 +169,7 @@ Resources:
129169
req.end();
130170
});
131171
172+
132173
const makeRequest = async (queryName) => {
133174
const options = {
134175
method: "POST",
@@ -139,65 +180,27 @@ Resources:
139180
timeout: 10000, // ms
140181
};
141182
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);
176189
}
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;
181197
};
182198
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 };
194199
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 };
199203
};
200204
201-
202205
Metadata:
203206
SamTransformTest: true

0 commit comments

Comments
 (0)