aws-cdk-lib.aws_appsync.DynamoDbDataSource

class DynamoDbDataSource (construct)

LanguageType name
.NETAmazon.CDK.AWS.AppSync.DynamoDbDataSource
Gogithub.com/aws/aws-cdk-go/awscdk/v2/awsappsync#DynamoDbDataSource
Javasoftware.amazon.awscdk.services.appsync.DynamoDbDataSource
Pythonaws_cdk.aws_appsync.DynamoDbDataSource
TypeScript (source)aws-cdk-lib » aws_appsync » DynamoDbDataSource

Implements IConstruct, IDependable, IGrantable

An AppSync datasource backed by a DynamoDB table.

Example

const api = new appsync.GraphqlApi(this, 'Api', {
  name: 'demo',
  schema: appsync.SchemaFile.fromAsset(path.join(__dirname, 'schema.graphql')),
  authorizationConfig: {
    defaultAuthorization: {
      authorizationType: appsync.AuthorizationType.IAM,
    },
  },
  xrayEnabled: true,
});

const demoTable = new dynamodb.Table(this, 'DemoTable', {
  partitionKey: {
    name: 'id',
    type: dynamodb.AttributeType.STRING,
  },
});

const demoDS = api.addDynamoDbDataSource('demoDataSource', demoTable);

// Resolver for the Query "getDemos" that scans the DynamoDb table and returns the entire list.
// Resolver Mapping Template Reference:
// https://docs.aws.amazon.com/appsync/latest/devguide/resolver-mapping-template-reference-dynamodb.html
demoDS.createResolver('QueryGetDemosResolver', {
  typeName: 'Query',
  fieldName: 'getDemos',
  requestMappingTemplate: appsync.MappingTemplate.dynamoDbScanTable(),
  responseMappingTemplate: appsync.MappingTemplate.dynamoDbResultList(),
});

// Resolver for the Mutation "addDemo" that puts the item into the DynamoDb table.
demoDS.createResolver('MutationAddDemoResolver', {
  typeName: 'Mutation',
  fieldName: 'addDemo',
  requestMappingTemplate: appsync.MappingTemplate.dynamoDbPutItem(
    appsync.PrimaryKey.partition('id').auto(),
    appsync.Values.projecting('input'),
  ),
  responseMappingTemplate: appsync.MappingTemplate.dynamoDbResultItem(),
});

//To enable DynamoDB read consistency with the `MappingTemplate`:
demoDS.createResolver('QueryGetDemosConsistentResolver', {
  typeName: 'Query',
  fieldName: 'getDemosConsistent',
  requestMappingTemplate: appsync.MappingTemplate.dynamoDbScanTable(true),
  responseMappingTemplate: appsync.MappingTemplate.dynamoDbResultList(),
});

Initializer

new DynamoDbDataSource(scope: Construct, id: string, props: DynamoDbDataSourceProps)

Parameters

  • scope Construct
  • id string
  • props DynamoDbDataSourceProps

Construct Props

NameTypeDescription
apiIGraphqlApiThe API to attach this data source to.
tableITableThe DynamoDB table backing this data source.
description?stringthe description of the data source.
name?stringThe name of the data source.
readOnlyAccess?booleanSpecify whether this DS is read only or has read and write permissions to the DynamoDB table.
serviceRole?IRoleThe IAM service role to be assumed by AppSync to interact with the data source.
useCallerCredentials?booleanuse credentials of caller to access DynamoDB.

api

Type: IGraphqlApi

The API to attach this data source to.


table

Type: ITable

The DynamoDB table backing this data source.


description?

Type: string (optional, default: None)

the description of the data source.


name?

Type: string (optional, default: id of data source)

The name of the data source.


readOnlyAccess?

Type: boolean (optional, default: false)

Specify whether this DS is read only or has read and write permissions to the DynamoDB table.


serviceRole?

Type: IRole (optional, default: Create a new role)

The IAM service role to be assumed by AppSync to interact with the data source.


useCallerCredentials?

Type: boolean (optional, default: false)

use credentials of caller to access DynamoDB.

Properties

NameTypeDescription
dsCfnDataSourcethe underlying CFN data source resource.
grantPrincipalIPrincipalthe principal of the data source to be IGrantable.
namestringthe name of the data source.
nodeNodeThe tree node.

ds

Type: CfnDataSource

the underlying CFN data source resource.


grantPrincipal

Type: IPrincipal

the principal of the data source to be IGrantable.


name

Type: string

the name of the data source.


node

Type: Node

The tree node.

Methods

NameDescription
createFunction(id, props)creates a new appsync function for this datasource and API using the given properties.
createResolver(id, props)creates a new resolver for this datasource and API using the given properties.
toString()Returns a string representation of this construct.

createFunction(id, props)

public createFunction(id: string, props: BaseAppsyncFunctionProps): AppsyncFunction

Parameters

  • id string
  • props BaseAppsyncFunctionProps

Returns

  • AppsyncFunction

creates a new appsync function for this datasource and API using the given properties.


createResolver(id, props)

public createResolver(id: string, props: BaseResolverProps): Resolver

Parameters

  • id string
  • props BaseResolverProps

Returns

  • Resolver

creates a new resolver for this datasource and API using the given properties.


toString()

public toString(): string

Returns

  • string

Returns a string representation of this construct.