Last active
December 30, 2021 16:17
-
-
Save dabit3/e325dbfc55b4d59f05c949399b3c5d8c to your computer and use it in GitHub Desktop.
Todo app AppSync API with CDK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as cdk from '@aws-cdk/core'; | |
import { CfnApiKey, PrimaryKey, Values, GraphQLApi, MappingTemplate, FieldLogLevel, AttributeValues } from '@aws-cdk/aws-appsync' | |
import { AttributeType, BillingMode, Table } from '@aws-cdk/aws-dynamodb'; | |
import * as lambda from '@aws-cdk/aws-lambda' | |
import { join } from 'path'; | |
export class AppsyncCdkStack extends cdk.Stack { | |
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) { | |
super(scope, id, props); | |
const api = new GraphQLApi(this, 'Api', { | |
name: `cdkapi`, | |
logConfig: { | |
fieldLogLevel: FieldLogLevel.ALL, | |
}, | |
schemaDefinitionFile: join('__dirname', '/../', 'graphql/schema.graphql'), | |
}); | |
const apiKey = new CfnApiKey(this, 'public-api-key', { | |
apiId: api.apiId | |
}); | |
const todoTable = new Table(this, 'MainTodoTable', { | |
billingMode: BillingMode.PAY_PER_REQUEST, | |
partitionKey: { | |
name: 'id', | |
type: AttributeType.STRING, | |
}, | |
}); | |
const todoDS = api.addDynamoDbDataSource('Todo', 'The todo data source', todoTable); | |
// Query Resolver to get all Todos | |
todoDS.createResolver({ | |
typeName: 'Query', | |
fieldName: 'listTodos', | |
requestMappingTemplate: MappingTemplate.fromString(` | |
#set( $limit = $util.defaultIfNull($context.args.limit, 100) ) | |
#set( $ListRequest = { | |
"version": "2018-05-29", | |
"limit": $limit | |
} ) | |
#if( $context.args.nextToken ) | |
#set( $ListRequest.nextToken = $context.args.nextToken ) | |
#end | |
$util.qr($ListRequest.put("operation", "Scan")) | |
$util.toJson($ListRequest) | |
`), | |
responseMappingTemplate: MappingTemplate.fromString(` | |
#if( $ctx.error) | |
$util.error($ctx.error.message, $ctx.error.type) | |
#else | |
$util.toJson($ctx.result) | |
#end | |
`) | |
}); | |
// Query Resolver to get an individual Todo by id | |
todoDS.createResolver({ | |
typeName: 'Query', | |
fieldName: 'getTodo', | |
requestMappingTemplate: MappingTemplate.dynamoDbGetItem('id', 'id'), | |
responseMappingTemplate: MappingTemplate.dynamoDbResultItem(), | |
}); | |
// Mutation Resolver for creating a new Todo | |
todoDS.createResolver({ | |
typeName: 'Mutation', | |
fieldName: 'createTodo', | |
requestMappingTemplate: MappingTemplate.dynamoDbPutItem( | |
PrimaryKey.partition('id').auto(), | |
Values.projecting('todo')), | |
responseMappingTemplate: MappingTemplate.dynamoDbResultItem(), | |
}); | |
// Mutation Resolver for updating an exisiting Todo | |
todoDS.createResolver({ | |
typeName: 'Mutation', | |
fieldName: 'updateTodo', | |
requestMappingTemplate: MappingTemplate.dynamoDbPutItem( | |
PrimaryKey.partition('id').is('id'), | |
Values.projecting('todo') | |
), | |
responseMappingTemplate: MappingTemplate.dynamoDbResultItem(), | |
}); | |
// Mutation Resolver for deleting an exisiting Todo | |
todoDS.createResolver({ | |
typeName: 'Mutation', | |
fieldName: 'deleteTodo', | |
requestMappingTemplate: MappingTemplate.dynamoDbDeleteItem('id', 'id'), | |
responseMappingTemplate: MappingTemplate.dynamoDbResultItem(), | |
}); | |
// defines an AWS Lambda resource | |
const testLambda = new lambda.Function(this, 'CDKAppSyncLambdaTest', { | |
runtime: lambda.Runtime.NODEJS_12_X, // execution environment | |
code: lambda.Code.asset('lambda-fns'), // code loaded from the "lambda-fns" directory | |
handler: 'callrest.handler', // file is "callrest", function is "handler" | |
}); | |
/** | |
* Add Lambda as a Datasource for the Graphql API. | |
*/ | |
const lambdaDs = api.addLambdaDataSource('CDKLambdaTest', 'The test Lambda data source', testLambda); | |
// Query Resolver to get all Customers | |
lambdaDs.createResolver({ | |
typeName: 'Query', | |
fieldName: 'getLambdaInfo', | |
requestMappingTemplate: MappingTemplate.lambdaRequest(), | |
responseMappingTemplate: MappingTemplate.lambdaResult(), | |
}); | |
// GraphQL API Endpoint | |
new cdk.CfnOutput(this, 'Endpoint', { | |
value: api.graphQlUrl | |
}); | |
// API Key | |
new cdk.CfnOutput(this, 'API_Key', { | |
value: apiKey.attrApiKey | |
}); | |
} | |
} | |
/* | |
GraphQL Schema | |
type Todo { | |
id: ID! | |
name: String! | |
completed: Completed | |
} | |
type ModelTodoConnection { | |
items: [Todo] | |
nextToken: String | |
} | |
enum Completed { | |
yes | |
no | |
} | |
type Query { | |
listTodos: ModelTodoConnection | |
getTodo(id: ID): Todo | |
getLambdaInfo: LambdaResponse | |
} | |
type LambdaResponse { | |
greeting: String | |
} | |
type Mutation { | |
createTodo(todo: TodoInput!): Todo | |
updateTodo(todo: TodoInput!): Todo | |
deleteTodo(id: ID!): Todo | |
} | |
input TodoInput { | |
id: ID! | |
name: String | |
completed: Completed | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment