package
2.166.0
Repository: https://github.com/aws/aws-cdk-go.git
Documentation: pkg.go.dev

# README

AWS AppSync Construct Library

The aws-cdk-lib/aws-appsync package contains constructs for building flexible APIs that use GraphQL.

import appsync "github.com/aws/aws-cdk-go/awscdk"

Example

DynamoDB

Example of a GraphQL API with AWS_IAM authorization resolving into a DynamoDb backend data source.

GraphQL schema file schema.graphql:

type demo {
  id: String!
  version: String!
}
type Query {
  getDemos: [ demo! ]
}
input DemoInput {
  version: String!
}
type Mutation {
  addDemo(input: DemoInput!): demo
}

CDK stack file app-stack.ts:

api := appsync.NewGraphqlApi(this, jsii.String("Api"), &GraphqlApiProps{
	Name: jsii.String("demo"),
	Definition: appsync.Definition_FromFile(path.join(__dirname, jsii.String("schema.graphql"))),
	AuthorizationConfig: &AuthorizationConfig{
		DefaultAuthorization: &AuthorizationMode{
			AuthorizationType: appsync.AuthorizationType_IAM,
		},
	},
	XrayEnabled: jsii.Boolean(true),
})

demoTable := dynamodb.NewTable(this, jsii.String("DemoTable"), &TableProps{
	PartitionKey: &Attribute{
		Name: jsii.String("id"),
		Type: dynamodb.AttributeType_STRING,
	},
})

demoDS := api.AddDynamoDbDataSource(jsii.String("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(jsii.String("QueryGetDemosResolver"), &BaseResolverProps{
	TypeName: jsii.String("Query"),
	FieldName: jsii.String("getDemos"),
	RequestMappingTemplate: appsync.MappingTemplate_DynamoDbScanTable(),
	ResponseMappingTemplate: appsync.MappingTemplate_DynamoDbResultList(),
})

// Resolver for the Mutation "addDemo" that puts the item into the DynamoDb table.
demoDS.CreateResolver(jsii.String("MutationAddDemoResolver"), &BaseResolverProps{
	TypeName: jsii.String("Mutation"),
	FieldName: jsii.String("addDemo"),
	RequestMappingTemplate: appsync.MappingTemplate_DynamoDbPutItem(appsync.PrimaryKey_Partition(jsii.String("id")).Auto(), appsync.Values_Projecting(jsii.String("input"))),
	ResponseMappingTemplate: appsync.MappingTemplate_DynamoDbResultItem(),
})

//To enable DynamoDB read consistency with the `MappingTemplate`:
demoDS.CreateResolver(jsii.String("QueryGetDemosConsistentResolver"), &BaseResolverProps{
	TypeName: jsii.String("Query"),
	FieldName: jsii.String("getDemosConsistent"),
	RequestMappingTemplate: appsync.MappingTemplate_*DynamoDbScanTable(jsii.Boolean(true)),
	ResponseMappingTemplate: appsync.MappingTemplate_*DynamoDbResultList(),
})

Aurora Serverless

AppSync provides a data source for executing SQL commands against Amazon Aurora Serverless clusters. You can use AppSync resolvers to execute SQL statements against the Data API with GraphQL queries, mutations, and subscriptions.

Aurora Serverless V1 Cluster

// Build a data source for AppSync to access the database.
var api graphqlApi
// Create username and password secret for DB Cluster
secret := rds.NewDatabaseSecret(this, jsii.String("AuroraSecret"), &DatabaseSecretProps{
	Username: jsii.String("clusteradmin"),
})

// The VPC to place the cluster in
vpc := ec2.NewVpc(this, jsii.String("AuroraVpc"))

// Create the serverless cluster, provide all values needed to customise the database.
cluster := rds.NewServerlessCluster(this, jsii.String("AuroraCluster"), &ServerlessClusterProps{
	Engine: rds.DatabaseClusterEngine_AURORA_MYSQL(),
	Vpc: Vpc,
	Credentials: map[string]*string{
		"username": jsii.String("clusteradmin"),
	},
	ClusterIdentifier: jsii.String("db-endpoint-test"),
	DefaultDatabaseName: jsii.String("demos"),
})
rdsDS := api.AddRdsDataSource(jsii.String("rds"), cluster, secret, jsii.String("demos"))

// Set up a resolver for an RDS query.
rdsDS.CreateResolver(jsii.String("QueryGetDemosRdsResolver"), &BaseResolverProps{
	TypeName: jsii.String("Query"),
	FieldName: jsii.String("getDemosRds"),
	RequestMappingTemplate: appsync.MappingTemplate_FromString(jsii.String(`
	  {
	    "version": "2018-05-29",
	    "statements": [
	      "SELECT * FROM demos"
	    ]
	  }
	  `)),
	ResponseMappingTemplate: appsync.MappingTemplate_*FromString(jsii.String(`
	    $utils.toJson($utils.rds.toJsonObject($ctx.result)[0])
	  `)),
})

// Set up a resolver for an RDS mutation.
rdsDS.CreateResolver(jsii.String("MutationAddDemoRdsResolver"), &BaseResolverProps{
	TypeName: jsii.String("Mutation"),
	FieldName: jsii.String("addDemoRds"),
	RequestMappingTemplate: appsync.MappingTemplate_*FromString(jsii.String(`
	  {
	    "version": "2018-05-29",
	    "statements": [
	      "INSERT INTO demos VALUES (:id, :version)",
	      "SELECT * WHERE id = :id"
	    ],
	    "variableMap": {
	      ":id": $util.toJson($util.autoId()),
	      ":version": $util.toJson($ctx.args.version)
	    }
	  }
	  `)),
	ResponseMappingTemplate: appsync.MappingTemplate_*FromString(jsii.String(`
	    $utils.toJson($utils.rds.toJsonObject($ctx.result)[1][0])
	  `)),
})

Aurora Serverless V2 Cluster

// Build a data source for AppSync to access the database.
var api graphqlApi
// Create username and password secret for DB Cluster
secret := rds.NewDatabaseSecret(this, jsii.String("AuroraSecret"), &DatabaseSecretProps{
	Username: jsii.String("clusteradmin"),
})

// The VPC to place the cluster in
vpc := ec2.NewVpc(this, jsii.String("AuroraVpc"))

// Create the serverless cluster, provide all values needed to customise the database.
cluster := rds.NewDatabaseCluster(this, jsii.String("AuroraClusterV2"), &DatabaseClusterProps{
	Engine: rds.DatabaseClusterEngine_AuroraPostgres(&AuroraPostgresClusterEngineProps{
		Version: rds.AuroraPostgresEngineVersion_VER_15_5(),
	}),
	Credentials: map[string]*string{
		"username": jsii.String("clusteradmin"),
	},
	ClusterIdentifier: jsii.String("db-endpoint-test"),
	Writer: rds.ClusterInstance_ServerlessV2(jsii.String("writer")),
	ServerlessV2MinCapacity: jsii.Number(2),
	ServerlessV2MaxCapacity: jsii.Number(10),
	Vpc: Vpc,
	DefaultDatabaseName: jsii.String("demos"),
	EnableDataApi: jsii.Boolean(true),
})
rdsDS := api.AddRdsDataSourceV2(jsii.String("rds"), cluster, secret, jsii.String("demos"))

// Set up a resolver for an RDS query.
rdsDS.CreateResolver(jsii.String("QueryGetDemosRdsResolver"), &BaseResolverProps{
	TypeName: jsii.String("Query"),
	FieldName: jsii.String("getDemosRds"),
	RequestMappingTemplate: appsync.MappingTemplate_FromString(jsii.String(`
	  {
	    "version": "2018-05-29",
	    "statements": [
	      "SELECT * FROM demos"
	    ]
	  }
	  `)),
	ResponseMappingTemplate: appsync.MappingTemplate_*FromString(jsii.String(`
	    $utils.toJson($utils.rds.toJsonObject($ctx.result)[0])
	  `)),
})

// Set up a resolver for an RDS mutation.
rdsDS.CreateResolver(jsii.String("MutationAddDemoRdsResolver"), &BaseResolverProps{
	TypeName: jsii.String("Mutation"),
	FieldName: jsii.String("addDemoRds"),
	RequestMappingTemplate: appsync.MappingTemplate_*FromString(jsii.String(`
	  {
	    "version": "2018-05-29",
	    "statements": [
	      "INSERT INTO demos VALUES (:id, :version)",
	      "SELECT * WHERE id = :id"
	    ],
	    "variableMap": {
	      ":id": $util.toJson($util.autoId()),
	      ":version": $util.toJson($ctx.args.version)
	    }
	  }
	  `)),
	ResponseMappingTemplate: appsync.MappingTemplate_*FromString(jsii.String(`
	    $utils.toJson($utils.rds.toJsonObject($ctx.result)[1][0])
	  `)),
})

HTTP Endpoints

GraphQL schema file schema.graphql:

type job {
  id: String!
  version: String!
}

input DemoInput {
  version: String!
}

type Mutation {
  callStepFunction(input: DemoInput!): job
}

GraphQL request mapping template request.vtl:

{
  "version": "2018-05-29",
  "method": "POST",
  "resourcePath": "/",
  "params": {
    "headers": {
      "content-type": "application/x-amz-json-1.0",
      "x-amz-target":"AWSStepFunctions.StartExecution"
    },
    "body": {
      "stateMachineArn": "<your step functions arn>",
      "input": "{ \"id\": \"$context.arguments.id\" }"
    }
  }
}

GraphQL response mapping template response.vtl:

{
  "id": "${context.result.id}"
}

CDK stack file app-stack.ts:

api := appsync.NewGraphqlApi(this, jsii.String("api"), &GraphqlApiProps{
	Name: jsii.String("api"),
	Definition: appsync.Definition_FromFile(path.join(__dirname, jsii.String("schema.graphql"))),
})

httpDs := api.AddHttpDataSource(jsii.String("ds"), jsii.String("https://states.amazonaws.com"), &HttpDataSourceOptions{
	Name: jsii.String("httpDsWithStepF"),
	Description: jsii.String("from appsync to StepFunctions Workflow"),
	AuthorizationConfig: &AwsIamConfig{
		SigningRegion: jsii.String("us-east-1"),
		SigningServiceName: jsii.String("states"),
	},
})

httpDs.CreateResolver(jsii.String("MutationCallStepFunctionResolver"), &BaseResolverProps{
	TypeName: jsii.String("Mutation"),
	FieldName: jsii.String("callStepFunction"),
	RequestMappingTemplate: appsync.MappingTemplate_FromFile(jsii.String("request.vtl")),
	ResponseMappingTemplate: appsync.MappingTemplate_*FromFile(jsii.String("response.vtl")),
})

EventBridge

Integrating AppSync with EventBridge enables developers to use EventBridge rules to route commands for GraphQL mutations that need to perform any one of a variety of asynchronous tasks. More broadly, it enables teams to expose an event bus as a part of a GraphQL schema.

GraphQL schema file schema.graphql:

schema {
    query: Query
    mutation: Mutation
}

type Query {
    event(id:ID!): Event
}

type Mutation {
    emitEvent(id: ID!, name: String): PutEventsResult!
}

type Event {
    id: ID!
    name: String!
}

type Entry {
    ErrorCode: String
    ErrorMessage: String
    EventId: String
}

type PutEventsResult {
    Entries: [Entry!]
    FailedEntry: Int
}

GraphQL request mapping template request.vtl:

{
    "version" : "2018-05-29",
    "operation": "PutEvents",
    "events" : [
        {
            "source": "integ.appsync.eventbridge",
            "detailType": "Mutation.emitEvent",
            "detail": $util.toJson($context.arguments)
        }
    ]
}

GraphQL response mapping template response.vtl:

$util.toJson($ctx.result)'

This response mapping template simply converts the EventBridge PutEvents result to JSON. For details about the response see the documentation. Additional logic can be added to the response template to map the response type, or to error in the event of failed events. More information can be found here.

CDK stack file app-stack.ts:

import events "github.com/aws/aws-cdk-go/awscdk"


api := appsync.NewGraphqlApi(this, jsii.String("EventBridgeApi"), &GraphqlApiProps{
	Name: jsii.String("EventBridgeApi"),
	Definition: appsync.Definition_FromFile(path.join(__dirname, jsii.String("appsync.eventbridge.graphql"))),
})

bus := events.NewEventBus(this, jsii.String("DestinationEventBus"), &EventBusProps{
})

dataSource := api.AddEventBridgeDataSource(jsii.String("NoneDS"), bus)

dataSource.CreateResolver(jsii.String("EventResolver"), &BaseResolverProps{
	TypeName: jsii.String("Mutation"),
	FieldName: jsii.String("emitEvent"),
	RequestMappingTemplate: appsync.MappingTemplate_FromFile(jsii.String("request.vtl")),
	ResponseMappingTemplate: appsync.MappingTemplate_*FromFile(jsii.String("response.vtl")),
})

Amazon OpenSearch Service

AppSync has builtin support for Amazon OpenSearch Service (successor to Amazon Elasticsearch Service) from domains that are provisioned through your AWS account. You can use AppSync resolvers to perform GraphQL operations such as queries, mutations, and subscriptions.

import "github.com/aws/aws-cdk-go/awscdk"

var api graphqlApi


user := iam.NewUser(this, jsii.String("User"))
domain := opensearch.NewDomain(this, jsii.String("Domain"), &DomainProps{
	Version: opensearch.EngineVersion_OPENSEARCH_2_3(),
	RemovalPolicy: awscdk.RemovalPolicy_DESTROY,
	FineGrainedAccessControl: &AdvancedSecurityOptions{
		MasterUserArn: user.UserArn,
	},
	EncryptionAtRest: &EncryptionAtRestOptions{
		Enabled: jsii.Boolean(true),
	},
	NodeToNodeEncryption: jsii.Boolean(true),
	EnforceHttps: jsii.Boolean(true),
})
ds := api.AddOpenSearchDataSource(jsii.String("ds"), domain)

ds.CreateResolver(jsii.String("QueryGetTestsResolver"), &BaseResolverProps{
	TypeName: jsii.String("Query"),
	FieldName: jsii.String("getTests"),
	RequestMappingTemplate: appsync.MappingTemplate_FromString(jSON.stringify(map[string]interface{}{
		"version": jsii.String("2017-02-28"),
		"operation": jsii.String("GET"),
		"path": jsii.String("/id/post/_search"),
		"params": map[string]map[string]interface{}{
			"headers": map[string]interface{}{
			},
			"queryString": map[string]interface{}{
			},
			"body": map[string]*f64{
				"from": jsii.Number(0),
				"size": jsii.Number(50),
			},
		},
	})),
	ResponseMappingTemplate: appsync.MappingTemplate_*FromString(jsii.String(`[
	    #foreach($entry in $context.result.hits.hits)
	    #if( $velocityCount > 1 ) , #end
	    $utils.toJson($entry.get("_source"))
	    #end
	  ]`)),
})

Merged APIs

AppSync supports Merged APIs which can be used to merge multiple source APIs into a single API.

import cdk "github.com/aws/aws-cdk-go/awscdk"


// first source API
firstApi := appsync.NewGraphqlApi(this, jsii.String("FirstSourceAPI"), &GraphqlApiProps{
	Name: jsii.String("FirstSourceAPI"),
	Definition: appsync.Definition_FromFile(path.join(__dirname, jsii.String("appsync.merged-api-1.graphql"))),
})

// second source API
secondApi := appsync.NewGraphqlApi(this, jsii.String("SecondSourceAPI"), &GraphqlApiProps{
	Name: jsii.String("SecondSourceAPI"),
	Definition: appsync.Definition_*FromFile(path.join(__dirname, jsii.String("appsync.merged-api-2.graphql"))),
})

// Merged API
mergedApi := appsync.NewGraphqlApi(this, jsii.String("MergedAPI"), &GraphqlApiProps{
	Name: jsii.String("MergedAPI"),
	Definition: appsync.Definition_FromSourceApis(&SourceApiOptions{
		SourceApis: []sourceApi{
			&sourceApi{
				SourceApi: firstApi,
				MergeType: appsync.MergeType_MANUAL_MERGE,
			},
			&sourceApi{
				SourceApi: secondApi,
				MergeType: appsync.MergeType_AUTO_MERGE,
			},
		},
	}),
})

Merged APIs Across Different Stacks

The SourceApiAssociation construct allows you to define a SourceApiAssociation to a Merged API in a different stack or account. This allows a source API owner the ability to associate it to an existing Merged API itself.

sourceApi := appsync.NewGraphqlApi(this, jsii.String("FirstSourceAPI"), &GraphqlApiProps{
	Name: jsii.String("FirstSourceAPI"),
	Definition: appsync.Definition_FromFile(path.join(__dirname, jsii.String("appsync.merged-api-1.graphql"))),
})

importedMergedApi := appsync.GraphqlApi_FromGraphqlApiAttributes(this, jsii.String("ImportedMergedApi"), &GraphqlApiAttributes{
	GraphqlApiId: jsii.String("MyApiId"),
	GraphqlApiArn: jsii.String("MyApiArn"),
})

importedExecutionRole := iam.Role_FromRoleArn(this, jsii.String("ExecutionRole"), jsii.String("arn:aws:iam::ACCOUNT:role/MyExistingRole"))
appsync.NewSourceApiAssociation(this, jsii.String("SourceApiAssociation2"), &SourceApiAssociationProps{
	SourceApi: sourceApi,
	MergedApi: importedMergedApi,
	MergeType: appsync.MergeType_MANUAL_MERGE,
	MergedApiExecutionRole: importedExecutionRole,
})

Merge Source API Update Within CDK Deployment

The SourceApiAssociationMergeOperation construct available in the awscdk-appsync-utils package provides the ability to merge a source API to a Merged API via a custom resource. If the merge operation fails with a conflict, the stack update will fail and rollback the changes to the source API in the stack in order to prevent merge conflicts and ensure the source API changes are always propagated to the Merged API.

Custom Domain Names

For many use cases you may want to associate a custom domain name with your GraphQL API. This can be done during the API creation.

import acm "github.com/aws/aws-cdk-go/awscdk"
import "github.com/aws/aws-cdk-go/awscdk"

// hosted zone and route53 features
var hostedZoneId string
zoneName := "example.com"


myDomainName := "api.example.com"
certificate := acm.NewCertificate(this, jsii.String("cert"), &CertificateProps{
	DomainName: myDomainName,
})
schema := appsync.NewSchemaFile(&SchemaProps{
	FilePath: jsii.String("mySchemaFile"),
})
api := appsync.NewGraphqlApi(this, jsii.String("api"), &GraphqlApiProps{
	Name: jsii.String("myApi"),
	Definition: appsync.Definition_FromSchema(schema),
	DomainName: &DomainOptions{
		Certificate: *Certificate,
		DomainName: myDomainName,
	},
})

// hosted zone for adding appsync domain
zone := route53.HostedZone_FromHostedZoneAttributes(this, jsii.String("HostedZone"), &HostedZoneAttributes{
	HostedZoneId: jsii.String(HostedZoneId),
	ZoneName: jsii.String(ZoneName),
})

// create a cname to the appsync domain. will map to something like xxxx.cloudfront.net
// create a cname to the appsync domain. will map to something like xxxx.cloudfront.net
route53.NewCnameRecord(this, jsii.String("CnameApiRecord"), &CnameRecordProps{
	RecordName: jsii.String("api"),
	Zone: Zone,
	DomainName: api.appSyncDomainName,
})

Log Group

AppSync automatically create a log group with the name /aws/appsync/apis/<graphql_api_id> upon deployment with log data set to never expire. If you want to set a different expiration period, use the logConfig.retention property.

Also you can choose the log level by setting the logConfig.fieldLogLevel property.

For more information, see CloudWatch logs.

To obtain the GraphQL API's log group as a logs.ILogGroup use the logGroup property of the GraphqlApi construct.

import logs "github.com/aws/aws-cdk-go/awscdk"


appsync.NewGraphqlApi(this, jsii.String("api"), &GraphqlApiProps{
	AuthorizationConfig: &AuthorizationConfig{
	},
	Name: jsii.String("myApi"),
	Definition: appsync.Definition_FromFile(path.join(__dirname, jsii.String("myApi.graphql"))),
	LogConfig: &LogConfig{
		FieldLogLevel: appsync.FieldLogLevel_INFO,
		Retention: logs.RetentionDays_ONE_WEEK,
	},
})

Schema

You can define a schema using from a local file using Definition.fromFile

api := appsync.NewGraphqlApi(this, jsii.String("api"), &GraphqlApiProps{
	Name: jsii.String("myApi"),
	Definition: appsync.Definition_FromFile(path.join(__dirname, jsii.String("schema.graphl"))),
})

ISchema

Alternative schema sources can be defined by implementing the ISchema interface. An example of this is the CodeFirstSchema class provided in awscdk-appsync-utils

Imports

Any GraphQL Api that has been created outside the stack can be imported from another stack into your CDK app. Utilizing the fromXxx function, you have the ability to add data sources and resolvers through a IGraphqlApi interface.

var api graphqlApi
var table table

importedApi := appsync.graphqlApi_FromGraphqlApiAttributes(this, jsii.String("IApi"), &GraphqlApiAttributes{
	GraphqlApiId: api.ApiId,
	GraphqlApiArn: api.Arn,
})
importedApi.AddDynamoDbDataSource(jsii.String("TableDataSource"), table)

If you don't specify graphqlArn in fromXxxAttributes, CDK will autogenerate the expected arn for the imported api, given the apiId. For creating data sources and resolvers, an apiId is sufficient.

Private APIs

By default all AppSync GraphQL APIs are public and can be accessed from the internet. For customers that want to limit access to be from their VPC, the optional API visibility property can be set to Visibility.PRIVATE at creation time. To explicitly create a public API, the visibility property should be set to Visibility.GLOBAL. If visibility is not set, the service will default to GLOBAL.

CDK stack file app-stack.ts:

api := appsync.NewGraphqlApi(this, jsii.String("api"), &GraphqlApiProps{
	Name: jsii.String("MyPrivateAPI"),
	Definition: appsync.Definition_FromFile(path.join(__dirname, jsii.String("appsync.schema.graphql"))),
	Visibility: appsync.Visibility_PRIVATE,
})

See documentation for more details about Private APIs

Authorization

There are multiple authorization types available for GraphQL API to cater to different access use cases. They are:

  • API Keys (AuthorizationType.API_KEY)
  • Amazon Cognito User Pools (AuthorizationType.USER_POOL)
  • OpenID Connect (AuthorizationType.OPENID_CONNECT)
  • AWS Identity and Access Management (AuthorizationType.AWS_IAM)
  • AWS Lambda (AuthorizationType.AWS_LAMBDA)

These types can be used simultaneously in a single API, allowing different types of clients to access data. When you specify an authorization type, you can also specify the corresponding authorization mode to finish defining your authorization. For example, this is a GraphQL API with AWS Lambda Authorization.

import lambda "github.com/aws/aws-cdk-go/awscdk"
var authFunction function


appsync.NewGraphqlApi(this, jsii.String("api"), &GraphqlApiProps{
	Name: jsii.String("api"),
	Definition: appsync.Definition_FromFile(path.join(__dirname, jsii.String("appsync.test.graphql"))),
	AuthorizationConfig: &AuthorizationConfig{
		DefaultAuthorization: &AuthorizationMode{
			AuthorizationType: appsync.AuthorizationType_LAMBDA,
			LambdaAuthorizerConfig: &LambdaAuthorizerConfig{
				Handler: authFunction,
			},
		},
	},
})

Permissions

When using AWS_IAM as the authorization type for GraphQL API, an IAM Role with correct permissions must be used for access to API.

When configuring permissions, you can specify specific resources to only be accessible by IAM authorization. For example, if you want to only allow mutability for IAM authorized access you would configure the following.

In schema.graphql:

type Mutation {
  updateExample(...): ...
    @aws_iam
}

In IAM:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "appsync:GraphQL"
      ],
      "Resource": [
        "arn:aws:appsync:REGION:ACCOUNT_ID:apis/GRAPHQL_ID/types/Mutation/fields/updateExample"
      ]
    }
  ]
}

See documentation for more details.

To make this easier, CDK provides grant API.

Use the grant function for more granular authorization.

var api iGraphqlApi
role := iam.NewRole(this, jsii.String("Role"), &RoleProps{
	AssumedBy: iam.NewServicePrincipal(jsii.String("lambda.amazonaws.com")),
})

api.Grant(role, appsync.IamResource_Custom(jsii.String("types/Mutation/fields/updateExample")), jsii.String("appsync:GraphQL"))

IamResource

In order to use the grant functions, you need to use the class IamResource.

  • IamResource.custom(...arns) permits custom ARNs and requires an argument.
  • IamResouce.ofType(type, ...fields) permits ARNs for types and their fields.
  • IamResource.all() permits ALL resources.

Generic Permissions

Alternatively, you can use more generic grant functions to accomplish the same usage.

These include:

  • grantMutation (use to grant access to Mutation fields)
  • grantQuery (use to grant access to Query fields)
  • grantSubscription (use to grant access to Subscription fields)
var api iGraphqlApi
var role role


// For generic types
api.GrantMutation(role, jsii.String("updateExample"))

// For custom types and granular design
api.Grant(role, appsync.IamResource_OfType(jsii.String("Mutation"), jsii.String("updateExample")), jsii.String("appsync:GraphQL"))

Pipeline Resolvers and AppSync Functions

AppSync Functions are local functions that perform certain operations onto a backend data source. Developers can compose operations (Functions) and execute them in sequence with Pipeline Resolvers.

var api graphqlApi


appsyncFunction := appsync.NewAppsyncFunction(this, jsii.String("function"), &AppsyncFunctionProps{
	Name: jsii.String("appsync_function"),
	Api: Api,
	DataSource: api.AddNoneDataSource(jsii.String("none")),
	RequestMappingTemplate: appsync.MappingTemplate_FromFile(jsii.String("request.vtl")),
	ResponseMappingTemplate: appsync.MappingTemplate_*FromFile(jsii.String("response.vtl")),
})

When using the LambdaDataSource, you can control the maximum number of resolver request inputs that will be sent to a single AWS Lambda function in a BatchInvoke operation by setting the maxBatchSize property.

var api graphqlApi
var lambdaDataSource lambdaDataSource


appsyncFunction := appsync.NewAppsyncFunction(this, jsii.String("function"), &AppsyncFunctionProps{
	Name: jsii.String("appsync_function"),
	Api: Api,
	DataSource: lambdaDataSource,
	MaxBatchSize: jsii.Number(10),
})

AppSync Functions are used in tandem with pipeline resolvers to compose multiple operations.

var api graphqlApi
var appsyncFunction appsyncFunction


pipelineResolver := appsync.NewResolver(this, jsii.String("pipeline"), &ResolverProps{
	Api: Api,
	DataSource: api.AddNoneDataSource(jsii.String("none")),
	TypeName: jsii.String("typeName"),
	FieldName: jsii.String("fieldName"),
	RequestMappingTemplate: appsync.MappingTemplate_FromFile(jsii.String("beforeRequest.vtl")),
	PipelineConfig: []iAppsyncFunction{
		appsyncFunction,
	},
	ResponseMappingTemplate: appsync.MappingTemplate_*FromFile(jsii.String("afterResponse.vtl")),
})

JS Functions and Resolvers

JS Functions and resolvers are also supported. You can use a .js file within your CDK project, or specify your function code inline.

var api graphqlApi


myJsFunction := appsync.NewAppsyncFunction(this, jsii.String("function"), &AppsyncFunctionProps{
	Name: jsii.String("my_js_function"),
	Api: Api,
	DataSource: api.AddNoneDataSource(jsii.String("none")),
	Code: appsync.Code_FromAsset(jsii.String("directory/function_code.js")),
	Runtime: appsync.FunctionRuntime_JS_1_0_0(),
})

appsync.NewResolver(this, jsii.String("PipelineResolver"), &ResolverProps{
	Api: Api,
	TypeName: jsii.String("typeName"),
	FieldName: jsii.String("fieldName"),
	Code: appsync.Code_FromInline(jsii.String(`
	    // The before step
	    export function request(...args) {
	      console.log(args);
	      return {}
	    }

	    // The after step
	    export function response(ctx) {
	      return ctx.prev.result
	    }
	  `)),
	Runtime: appsync.FunctionRuntime_JS_1_0_0(),
	PipelineConfig: []iAppsyncFunction{
		myJsFunction,
	},
})

Learn more about Pipeline Resolvers and AppSync Functions here.

Introspection

By default, AppSync allows you to use introspection queries.

For customers that want to limit access to be introspection queries, the introspectionConfig property can be set to IntrospectionConfig.DISABLED at creation time. If introspectionConfig is not set, the service will default to ENABLED.

api := appsync.NewGraphqlApi(this, jsii.String("api"), &GraphqlApiProps{
	Name: jsii.String("DisableIntrospectionApi"),
	Definition: appsync.Definition_FromFile(path.join(__dirname, jsii.String("appsync.schema.graphql"))),
	IntrospectionConfig: appsync.IntrospectionConfig_DISABLED,
})

Query Depth Limits

By default, queries are able to process an unlimited amount of nested levels. Limiting queries to a specified amount of nested levels has potential implications for the performance and flexibility of your project.

api := appsync.NewGraphqlApi(this, jsii.String("api"), &GraphqlApiProps{
	Name: jsii.String("LimitQueryDepths"),
	Definition: appsync.Definition_FromFile(path.join(__dirname, jsii.String("appsync.schema.graphql"))),
	QueryDepthLimit: jsii.Number(2),
})

Resolver Count Limits

You can control how many resolvers each query can process. By default, each query can process up to 10000 resolvers. By setting a limit AppSync will not handle any resolvers past a certain number limit.

api := appsync.NewGraphqlApi(this, jsii.String("api"), &GraphqlApiProps{
	Name: jsii.String("LimitResolverCount"),
	Definition: appsync.Definition_FromFile(path.join(__dirname, jsii.String("appsync.schema.graphql"))),
	ResolverCountLimit: jsii.Number(2),
})

Environment Variables

To use environment variables in resolvers, you can use the environmentVariables property and the addEnvironmentVariable method.

api := appsync.NewGraphqlApi(this, jsii.String("api"), &GraphqlApiProps{
	Name: jsii.String("api"),
	Definition: appsync.Definition_FromFile(path.join(__dirname, jsii.String("appsync.schema.graphql"))),
	EnvironmentVariables: map[string]*string{
		"EnvKey1": jsii.String("non-empty-1"),
	},
})

api.AddEnvironmentVariable(jsii.String("EnvKey2"), jsii.String("non-empty-2"))

Configure an EventBridge target that invokes an AppSync GraphQL API

Configuring the target relies on the graphQLEndpointArn property.

Use the AppSync event target to trigger an AppSync GraphQL API. You need to create an AppSync.GraphqlApi configured with AWS_IAM authorization mode.

The code snippet below creates a AppSync GraphQL API target that is invoked, calling the publish mutation.

import events "github.com/aws/aws-cdk-go/awscdk"
import targets "github.com/aws/aws-cdk-go/awscdk"

var rule rule
var api graphqlApi


rule.AddTarget(targets.NewAppSync(api, &AppSyncGraphQLApiProps{
	GraphQLOperation: jsii.String("mutation Publish($message: String!){ publish(message: $message) { message } }"),
	Variables: events.RuleTargetInput_FromObject(map[string]*string{
		"message": jsii.String("hello world"),
	}),
}))

Owner Contact

You can set the owner contact information for an API resource. This field accepts any string input with a length of 0 - 256 characters.

api := appsync.NewGraphqlApi(this, jsii.String("OwnerContact"), &GraphqlApiProps{
	Name: jsii.String("OwnerContact"),
	Definition: appsync.Definition_FromSchema(appsync.SchemaFile_FromAsset(path.join(__dirname, jsii.String("appsync.test.graphql")))),
	OwnerContact: jsii.String("test-owner-contact"),
})

# Functions

Import Appsync Function from arn.
Checks if `x` is a construct.
Returns true if the construct was created by CDK, and false otherwise.
Check whether the given construct is a Resource.
Loads the function code from a local disk path.
Inline code for AppSync function.
Checks if `x` is a construct.
Checks if `x` is a construct.
No description provided by the author
Returns `true` if a construct is a stack element (i.e.
Check whether the given object is a CfnResource.
Checks if `x` is a construct.
No description provided by the author
Returns `true` if a construct is a stack element (i.e.
Check whether the given object is a CfnResource.
Checks if `x` is a construct.
No description provided by the author
Returns `true` if a construct is a stack element (i.e.
Check whether the given object is a CfnResource.
Checks if `x` is a construct.
No description provided by the author
Returns `true` if a construct is a stack element (i.e.
Check whether the given object is a CfnResource.
Checks if `x` is a construct.
No description provided by the author
Returns `true` if a construct is a stack element (i.e.
Check whether the given object is a CfnResource.
Checks if `x` is a construct.
No description provided by the author
Returns `true` if a construct is a stack element (i.e.
Check whether the given object is a CfnResource.
Checks if `x` is a construct.
No description provided by the author
Returns `true` if a construct is a stack element (i.e.
Check whether the given object is a CfnResource.
Checks if `x` is a construct.
No description provided by the author
Returns `true` if a construct is a stack element (i.e.
Check whether the given object is a CfnResource.
Checks if `x` is a construct.
No description provided by the author
Returns `true` if a construct is a stack element (i.e.
Check whether the given object is a CfnResource.
Checks if `x` is a construct.
No description provided by the author
Returns `true` if a construct is a stack element (i.e.
Check whether the given object is a CfnResource.
Checks if `x` is a construct.
No description provided by the author
Returns `true` if a construct is a stack element (i.e.
Check whether the given object is a CfnResource.
Checks if `x` is a construct.
No description provided by the author
Returns `true` if a construct is a stack element (i.e.
Check whether the given object is a CfnResource.
Checks if `x` is a construct.
Loads the function code from a local disk path.
Inline code for AppSync function.
Schema from file, allows schema definition through schema.graphql file.
Schema from schema object.
Schema from existing AppSync APIs - used for creating a AppSync Merged API.
Checks if `x` is a construct.
Checks if `x` is a construct.
Checks if `x` is a construct.
No description provided by the author
Import a GraphQL API through this function.
Checks if `x` is a construct.
Returns true if the construct was created by CDK, and false otherwise.
Check whether the given construct is a Resource.
Checks if `x` is a construct.
Returns true if the construct was created by CDK, and false otherwise.
Check whether the given construct is a Resource.
Checks if `x` is a construct.
Generate the resource names that accepts all types: `*`.
Generate the resource names given custom arns.
Generate the resource names given a type and fields.
Loads the function code from a local disk path.
Inline code for AppSync function.
Condition (k, arg).
Condition k BETWEEN arg1 AND arg2, true if k >= arg1 and k <= arg2.
Condition k = arg, true if the key attribute k is equal to the Query argument.
Condition k >= arg, true if the key attribute k is greater or equal to the Query argument.
Condition k > arg, true if the key attribute k is greater than the the Query argument.
Condition k <= arg, true if the key attribute k is less than or equal to the Query argument.
Condition k < arg, true if the key attribute k is less than the Query argument.
Checks if `x` is a construct.
Mapping template to delete a single item from a DynamoDB table.
Mapping template to get a single item from a DynamoDB table.
Mapping template to save a single item to a DynamoDB table.
Mapping template to query a set of items from a DynamoDB table.
Mapping template for a single result item from DynamoDB.
Mapping template for a result list from DynamoDB.
Mapping template to scan a DynamoDB table to fetch all entries.
Create a mapping template from the given file.
Create a mapping template from the given string.
Mapping template to invoke a Lambda function.
Mapping template to return the Lambda result to the caller.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Deprecated: - use `OpenSearchDataSource`.
Deprecated: - use `OpenSearchDataSource`.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Checks if `x` is a construct.
Checks if `x` is a construct.
Allows assigning a value to the partition key.
Allows assigning a value to the partition key.
Checks if `x` is a construct.
Checks if `x` is a construct.
Generate a Schema from file.
Import Appsync Source Api Association from source API, merged api, and merge type.
Checks if `x` is a construct.
Returns true if the construct was created by CDK, and false otherwise.
Check whether the given construct is a Resource.
Allows assigning a value to the specified attribute.
Treats the specified object as a map of assignments, where the property names represent attribute names.

# Constants

API Key authorization type.
AWS IAM authorization type.
Lambda authorization type.
OpenID Connect authorization type.
Cognito User Pool authorization type.
All messages (Debug, Error, Info, and Trace) appear in logs.
Debug, Info, and Error messages, appear in logs.
Only Error messages appear in logs.
Info and Error messages appear in logs.
Resolver logging is disabled.
AppSync JavaScript runtime.
Disable introspection.
Enable introspection.
Auto merge.
Manual merge.
ALLOW access to API.
DENY access to API.
Public, open to the internet.
Only accessible through a VPC.

# Structs

Configuration for API Key authorization in AppSync.
The attributes for imported AppSync Functions.
the CDK properties for AppSync Functions.
Configuration of the API authorization modes.
Interface to specify default or additional authorization(s).
The authorization config in case the HTTP endpoint requires authorization.
properties for an AppSync datasource backed by a resource.
the base properties for AppSync Functions.
Base properties for an AppSync datasource.
Basic properties for an AppSync resolver.
CachingConfig for AppSync resolvers.
An auth mode.
An auth provider for the AppSync API.
Optional authorization configuration for using Amazon Cognito user pools with your API endpoint.
The configuration for an Event Api.
The log config for the AppSync API.
A `LambdaAuthorizerConfig` specifies how to authorize AWS AppSync API access when using the `AWS_LAMBDA` authorizer mode.
Describes an OpenID Connect (OIDC) configuration.
Properties for defining a `CfnApiCache`.
Properties for defining a `CfnApiKey`.
Properties for defining a `CfnApi`.
An auth mode.
Properties for defining a `CfnChannelNamespace`.
The `AuthorizationConfig` property type specifies the authorization type and configuration for an AWS AppSync http data source.
Use the `AwsIamConfig` property type to specify `AwsIamConfig` for a AWS AppSync authorizaton.
Describes a Delta Sync configuration.
The `DynamoDBConfig` property type specifies the `AwsRegion` and `TableName` for an Amazon DynamoDB table in your account for an AWS AppSync data source.
Example: // The code below shows an example of how to instantiate this type.
The data source.
Use the `HttpConfig` property type to specify `HttpConfig` for an AWS AppSync data source.
The `LambdaConfig` property type specifies the Lambda function ARN for an AWS AppSync data source.
The `OpenSearchServiceConfig` property type specifies the `AwsRegion` and `Endpoints` for an Amazon OpenSearch Service domain in your account for an AWS AppSync data source.
Use the `RdsHttpEndpointConfig` property type to specify the `RdsHttpEndpoint` for an AWS AppSync relational database.
Use the `RelationalDatabaseConfig` property type to specify `RelationalDatabaseConfig` for an AWS AppSync data source.
Properties for defining a `CfnDataSource`.
Properties for defining a `CfnDomainNameApiAssociation`.
Properties for defining a `CfnDomainName`.
Describes a runtime used by an AWS AppSync resolver or AWS AppSync function.
The `LambdaConflictHandlerConfig` object when configuring `LAMBDA` as the Conflict Handler.
Describes a Sync configuration for a resolver.
Properties for defining a `CfnFunctionConfiguration`.
Describes an additional authentication provider.
Describes an Amazon Cognito user pool configuration.
Describes an enhanced metrics configuration.
Configuration for AWS Lambda function authorization.
The `LogConfig` property type specifies the logging configuration when writing GraphQL operations and tracing to Amazon CloudWatch for an AWS AppSync GraphQL API.
The `OpenIDConnectConfig` property type specifies the optional authorization configuration for using an OpenID Connect compliant service with your GraphQL endpoint for an AWS AppSync GraphQL API.
The `UserPoolConfig` property type specifies the optional authorization configuration for using Amazon Cognito user pools with your GraphQL endpoint for an AWS AppSync GraphQL API.
Properties for defining a `CfnGraphQLApi`.
Properties for defining a `CfnGraphQLSchema`.
Describes a runtime used by an AWS AppSync resolver or AWS AppSync function.
The caching configuration for a resolver that has caching activated.
The `LambdaConflictHandlerConfig` when configuring LAMBDA as the Conflict Handler.
Use the `PipelineConfig` property type to specify `PipelineConfig` for an AWS AppSync resolver.
Describes a Sync configuration for a resolver.
Properties for defining a `CfnResolver`.
Describes properties used to specify configurations related to a source API.
Properties for defining a `CfnSourceApiAssociation`.
Result of binding `Code` into a `Function`.
Optional configuration for data sources.
Domain name configuration for AppSync.
Properties for an AppSync DynamoDB datasource.
Properties for the Elasticsearch Data Source.
Properties for an AppSync EventBridge datasource.
props used by implementations of BaseDataSource to provide configuration.
Additional property for an AppSync resolver for data source reference.
Attributes for GraphQL imports.
Properties for an AppSync GraphQL API.
Optional configuration for Http data sources.
Properties for an AppSync http datasource.
Configuration for Lambda authorization in AppSync.
Properties for an AppSync Lambda datasource.
Logging configuration for AppSync.
Properties for an AppSync dummy datasource.
Configuration for OpenID Connect authorization in AppSync.
Properties for the OpenSearch Data Source.
Properties for an AppSync RDS datasource Aurora Serverless V1.
Properties for an AppSync RDS datasource Aurora Serverless V2.
Additional property for an AppSync resolver for GraphQL API reference.
Config for binding runtime to a function or resolver.
Used for configuring schema bind behavior.
The options for configuring a schema from an existing file.
Configuration of source API.
The attributes for imported AppSync Source Api Association.
Properties for SourceApiAssociation which associates an AppSync Source API with an AppSync Merged API.
Additional API configuration for creating a AppSync Merged API.
Configuration for Cognito user-pools in AppSync.

# Interfaces

AppSync Functions are local functions that perform certain operations onto a backend data source.
Represents a local file with source code used for an AppSync Function or Resolver.
Utility class representing the assigment of a value to an attribute.
Specifies the attribute value assignments.
Utility class to allow assigning a value to an attribute.
Abstract AppSync datasource implementation.
Abstract AppSync datasource implementation.
Creates a `GraphqlApi` object.
The `AWS::AppSync::ApiCache` resource represents the input of a `CreateApiCache` operation.
The `AWS::AppSync::ApiKey` resource creates a unique key that you can distribute to clients who are executing GraphQL operations with AWS AppSync that require an API key.
Resource schema for AppSync ChannelNamespace.
The `AWS::AppSync::DataSource` resource creates data sources for resolvers in AWS AppSync to connect to, such as Amazon DynamoDB , AWS Lambda , and Amazon OpenSearch Service .
The `AWS::AppSync::DomainName` resource creates a `DomainNameConfig` object to configure a custom domain.
The `AWS::AppSync::DomainNameApiAssociation` resource represents the mapping of your custom domain name to the assigned API URL.
The `AWS::AppSync::FunctionConfiguration` resource defines the functions in GraphQL APIs to perform certain operations.
The `AWS::AppSync::GraphQLApi` resource creates a new AWS AppSync GraphQL API.
The `AWS::AppSync::GraphQLSchema` resource is used for your AWS AppSync GraphQL schema that controls the data model for your API.
The `AWS::AppSync::Resolver` resource defines the logical GraphQL resolver that you attach to fields in a schema.
Describes the configuration of a source API.
Represents source code for an AppSync Function or Resolver.
AppSync definition.
An AppSync datasource backed by a DynamoDB table.
An Appsync datasource backed by Elasticsearch.
An AppSync datasource backed by EventBridge.
Utility class for specifying specific appsync runtime versions.
An AppSync GraphQL API.
Base Class for GraphQL API.
An AppSync datasource backed by a http endpoint.
A class used to generate resource arns for AppSync.
Interface for AppSync Functions.
Interface for GraphQL.
AppSync function code from an inline string.
Interface for implementing your own schema.
Configuration for bound graphql schema.
Interface for AppSync Source Api Association.
Factory class for DynamoDB key conditions.
An AppSync datasource backed by a Lambda function.
MappingTemplates for AppSync resolvers.
An AppSync dummy datasource.
An Appsync datasource backed by OpenSearch.
Specifies the assignment to the partition key.
Utility class to allow assigning a value or an auto-generated id to a partition key.
Specifies the assignment to the primary key.
An AppSync datasource backed by RDS.
An AppSync resolver.
The Schema for a GraphQL Api.
Utility class to allow assigning a value or an auto-generated id to a sort key.
AppSync SourceApiAssociation which associates an AppSync source API to an AppSync Merged API.
Factory class for attribute value assignments.

# Type aliases

enum with all possible values for AppSync authorization type.
log-level for fields in AppSync.
Appsync supported runtimes.
Introspection configuration for a GraphQL API.
Merge type used to associate the source API.
enum with all possible values for Cognito user-pool default actions.
Visibility type for a GraphQL API.