aws / aws-appsync-community

The AWS AppSync community
https://aws.amazon.com/appsync
Apache License 2.0
506 stars 32 forks source link

[feature request]: evaluate code and mocking utils #290

Open eikster-dk opened 1 year ago

eikster-dk commented 1 year ago

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<MutationCreatePostArgs>,
): 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",
}
`;