Skip to content
v2.274.0

AppSync Class ​

Use an AppSync GraphQL API as a target for Amazon EventBridge rules.

Example ​

ts
import * as appsync from 'aws-cdk-lib/aws-appsync';

const api = new appsync.GraphqlApi(this, 'api', {
  name: 'api',
  definition: appsync.Definition.fromFile('schema.graphql'),
  authorizationConfig: {
    defaultAuthorization: { authorizationType: appsync.AuthorizationType.IAM }
  },
});

const rule = new events.Rule(this, 'Rule', {
  schedule: events.Schedule.rate(cdk.Duration.hours(1)),
});

rule.addTarget(new targets.AppSync(api, {
  graphQLOperation: 'mutation Publish($message: String!){ publish(message: $message) { message } }',
  variables: events.RuleTargetInput.fromObject({
    message: 'hello world',
  }),
}));
python
import aws_cdk.aws_appsync as appsync


api = appsync.GraphqlApi(self, "api",
    name="api",
    definition=appsync.Definition.from_file("schema.graphql"),
    authorization_config=appsync.AuthorizationConfig(
        default_authorization=appsync.AuthorizationMode(authorization_type=appsync.AuthorizationType.IAM)
    )
)

rule = events.Rule(self, "Rule",
    schedule=events.Schedule.rate(cdk.Duration.hours(1))
)

rule.add_target(targets.AppSync(api,
    graph_ql_operation="mutation Publish($message: String!){ publish(message: $message) { message } }",
    variables=events.RuleTargetInput.from_object({
        "message": "hello world"
    })
))
java
import software.amazon.awscdk.services.appsync.*;


GraphqlApi api = GraphqlApi.Builder.create(this, "api")
        .name("api")
        .definition(Definition.fromFile("schema.graphql"))
        .authorizationConfig(AuthorizationConfig.builder()
                .defaultAuthorization(AuthorizationMode.builder().authorizationType(AuthorizationType.IAM).build())
                .build())
        .build();

Rule rule = Rule.Builder.create(this, "Rule")
        .schedule(Schedule.rate(Duration.hours(1)))
        .build();

rule.addTarget(AppSync.Builder.create(api)
        .graphQLOperation("mutation Publish($message: String!){ publish(message: $message) { message } }")
        .variables(RuleTargetInput.fromObject(Map.of(
                "message", "hello world")))
        .build());
csharp
// Install: dotnet add package Amazon.CDK.Lib
using Amazon.CDK.AWS.AppSync;


var api = new GraphqlApi(this, "api", new GraphqlApiProps {
    Name = "api",
    Definition = Definition.FromFile("schema.graphql"),
    AuthorizationConfig = new AuthorizationConfig {
        DefaultAuthorization = new AuthorizationMode { AuthorizationType = AuthorizationType.IAM }
    }
});

var rule = new Rule(this, "Rule", new RuleProps {
    Schedule = Schedule.Rate(Duration.Hours(1))
});

rule.AddTarget(new AppSync(api, new AppSyncGraphQLApiProps {
    GraphQLOperation = "mutation Publish($message: String!){ publish(message: $message) { message } }",
    Variables = RuleTargetInput.FromObject(new Dictionary<string, string> {
        { "message", "hello world" }
    })
}));
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
import "github.com/aws/aws-cdk-go/awscdk"


api := appsync.NewGraphqlApi(this, jsii.String("api"), &GraphqlApiProps{
	Name: jsii.String("api"),
	Definition: appsync.Definition_FromFile(jsii.String("schema.graphql")),
	AuthorizationConfig: &AuthorizationConfig{
		DefaultAuthorization: &AuthorizationMode{
			AuthorizationType: appsync.AuthorizationType_IAM,
		},
	},
})

rule := events.NewRule(this, jsii.String("Rule"), &RuleProps{
	Schedule: events.Schedule_Rate(cdk.Duration_Hours(jsii.Number(1))),
})

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"),
	}),
}))

Initializer ​

ts
new AppSync(appsyncApi: IGraphqlApi, props: AppSyncGraphQLApiProps)
python
AppSync(appsync_api: IGraphqlApi, props: AppSyncGraphQLApiProps)
java
new AppSync(IGraphqlApi appsyncApi, AppSyncGraphQLApiProps props)
csharp
new AppSync(IGraphqlApi appsyncApi, AppSyncGraphQLApiProps props)
go
NewAppSync(AppsyncApi IGraphqlApi, Props AppSyncGraphQLApiProps)

Methods ​

bind(rule: IRuleRef, _id?: string): RuleTargetConfigdef bind(self, rule: IRuleRef, _id: typing.Optional[str] = None) -> RuleTargetConfigpublic RuleTargetConfig bind(IRuleRef rule, String _id)public RuleTargetConfig Bind(IRuleRef rule, string? _id = null)func Bind(Rule IRuleRef, _id *string) RuleTargetConfig#
Returns a RuleTarget that can be used to trigger this AppSync GraphQL API as a result from an EventBridge event.