aws / aws/aws-appsync-community
[feature request]: evaluate code and mocking utils
- Dominant language
- HTML
- Stars
- 507
- Forks
- 37
- PR merge metrics
- No merged PRs in 30d
Description
Hello
I'm currently working on a greenfield project and finally got the chance to use AppSync. I have been playing around with the evaluate code from the SDK after reading the docs.
I have been toying with the following example from https://blog.graphbolt.dev/improving-developer-experience-with-typescript-how-to-write-strongly-typed-appsync-resolvers
I have a resolver that looks like this:
```
export function request(
ctx: Context,
): DynamoDBPutItemRequest {
// add timestamps
const item = createItem(ctx.args.post);
return {
operation: 'PutItem',
key: {
pk: util.dynamodb.toDynamoDB(util.autoKsuid()),
sk: util.dynamodb.toDynamoDB(util.autoKsuid()),
},
attributeValues: util.dynamodb.toMapValues({
'__typename': 'Post',
...item,
}),
};
}
```
with the following test:
```
import { AppSyncClient, EvaluateCodeCommand, EvaluateCodeRequest } from '@aws-sdk/client-appsync'
import { describe, expect, test } from 'vitest'
import { bundleAppSyncResolver } from './bundler';
import * as path from 'path';
describe('createPost resolver', () => {
test('returns correct appsync request', async () => {
const client = new AppSyncClient({})
const ctx = {
arguments: {
post: {
title: "Hello World",
content: "This is my first post",
authorName: "John Doe",
}
},
}
const input: EvaluateCodeRequest = { // EvaluateCodeRequest
runtime: {
name: "APPSYNC_JS",
runtimeVersion: "1.0.0",
},
code: bundleAppSyncResolver(path.join(__dirname, 'createPost.ts')),
context: JSON.stringify(ctx),
function: "request",
};
const command = new EvaluateCodeCommand(input);
const response = await client.send(command);
const result = JSON.parse(response.evaluationResult!);
expect(result).toMatchSnapshot();
})
})
```
and I tried to write a evaluate test that uses snapshots. The thing is that `util.autoKsuid()` will autogenerate a new ID on every invocation. It would be a great addition to be able to mock functions like `util.autoKsuid()` or `util.time` to enable snapshot testing of requests
Snapshot:
```
exports[`createPost resolver > returns correct appsync request 1`] = `
{
"attributeValues": {
"__typename": {
"S": "Post",
},
"authorName": {
"S": "John Doe",
},
"content": {
"S": "This is my first post",
},
"createdAt": {
"S": "2023-05-10T12:20:38.107Z",
},
"title": {
"S": "Hello World",
},
"updatedAt": {
"S": "2023-05-10T12:20:38.108Z",
},
},
"key": {
"pk": {
"S": "2PbMHLA1k9oSoF01iP9BCEWJ4Tk",
},
"sk": {
"S": "2PbMHHyXWOUvsuuzpr8sNxtF7e2",
},
},
"operation": "PutItem",
}
`;
```
Contributor guide
Research direction
Start with the evaluate-code flow used by bundleAppSyncResolver and the createPost.ts example, then review the AppSync EvaluateCodeRequest test shown in the issue. Determine how util.autoKsuid() and util.time are evaluated, and define a way to make those calls deterministic for snapshot tests. Done means the resolver test can produce repeatable snapshots without changing the resolver itself.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- api, testing
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100