Skip to content

Commit

Permalink
Implemented createPost resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
willa75 committed May 18, 2022
1 parent 449148e commit 40bd85a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
21 changes: 20 additions & 1 deletion lib/cdk-typescript-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@ import * as appsync from '@aws-cdk/aws-appsync-alpha';

import { Stack, StackProps, CfnOutput } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { aws_cognito as cognito } from 'aws-cdk-lib';
import {
aws_cognito as cognito,
aws_dynamodb as dynamodb,
aws_lambda as lambda
} from 'aws-cdk-lib';


export class CdkTypescriptStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);

const postsTable = new dynamodb.Table(this, 'PostsTable', {
partitionKey: {name: 'id', type: dynamodb.AttributeType.STRING},
replicationRegions: ['us-east-1']
});

const userPool = new cognito.UserPool(this, 'CdkAuth', {
userPoolName: 'CdkAuth',
selfSignUpEnabled: true,
Expand Down Expand Up @@ -44,6 +53,16 @@ export class CdkTypescriptStack extends Stack {
xrayEnabled: true,
});

const postsDS = api.addDynamoDbDataSource('postsTable', postsTable);

// create post
postsDS.createResolver({
typeName: 'Mutation',
fieldName: 'createPost',
requestMappingTemplate: appsync.MappingTemplate.fromFile('mapping-template/Mutation.createPost.request.vtl'),
responseMappingTemplate: appsync.MappingTemplate.dynamoDbResultItem()
});

new CfnOutput(this, 'GraphQL_URL', { value: api.graphqlUrl });

}
Expand Down
15 changes: 15 additions & 0 deletions mapping-template/Mutation.createPost.request.vtl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version" : "2018-05-29",
"operation" : "PutItem",
"key": {
"id": $util.dynamodb.toDynamoDBJson($util.autoUlid())
},
"attributeValues" : {
"title": $util.dynamodb.toDynamoDBJson($context.arguments.title),
"text": $util.dynamodb.toDynamoDBJson($context.arguments.text),
"createdAt": $util.dynamodb.toDynamoDBJson($util.time.nowISO8601()),
},
"condition" : {
"expression" : "attribute_not_exists(id)"
},
}

0 comments on commit 40bd85a

Please sign in to comment.