Package Exports
- @aws-cdk/aws-appsync
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@aws-cdk/aws-appsync) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
AWS AppSync Construct Library
This is a developer preview (public beta) module.
All classes with the
Cfn
prefix in this module (CFN Resources) are auto-generated from CloudFormation. They are stable and safe to use.However, all other classes, i.e., higher level constructs, are under active development and subject to non-backward compatible changes or removal in any future version. These are not subject to the Semantic Versioning model. This means that while you may use them, you may need to update your source code when upgrading to a newer version of this package.
This module is part of the AWS Cloud Development Kit project.
Usage Example
Given the following GraphQL schema file schema.graphql
:
type Customer {
id: String!
name: String!
}
input SaveCustomerInput {
name: String!
}
type Order {
customer: String!
order: String!
}
type Query {
getCustomers: [Customer]
getCustomer(id: String): Customer
}
input FirstOrderInput {
product: String!
quantity: Int!
}
type Mutation {
addCustomer(customer: SaveCustomerInput!): Customer
saveCustomer(id: String!, customer: SaveCustomerInput!): Customer
removeCustomer(id: String!): Customer
saveCustomerWithFirstOrder(customer: SaveCustomerInput!, order: FirstOrderInput!, referral: String): Order
}
the following CDK app snippet will create a complete CRUD AppSync API:
export class ApiStack extends Stack {
constructor(scope: Construct, id: string) {
super(scope, id);
const userPool = new UserPool(this, 'UserPool', {
signInType: SignInType.USERNAME,
});
const api = new GraphQLApi(this, 'Api', {
name: `demoapi`,
logConfig: {
fieldLogLevel: FieldLogLevel.ALL,
},
authorizationConfig: {
defaultAuthorization: {
userPool,
defaultAction: UserPoolDefaultAction.ALLOW,
},
additionalAuthorizationModes: [
{
apiKeyDesc: 'My API Key',
},
],
},
schemaDefinitionFile: './schema.graphql',
});
const customerTable = new Table(this, 'CustomerTable', {
billingMode: BillingMode.PAY_PER_REQUEST,
partitionKey: {
name: 'id',
type: AttributeType.STRING,
},
});
const customerDS = api.addDynamoDbDataSource('Customer', 'The customer data source', customerTable);
customerDS.createResolver({
typeName: 'Query',
fieldName: 'getCustomers',
requestMappingTemplate: MappingTemplate.dynamoDbScanTable(),
responseMappingTemplate: MappingTemplate.dynamoDbResultList(),
});
customerDS.createResolver({
typeName: 'Query',
fieldName: 'getCustomer',
requestMappingTemplate: MappingTemplate.dynamoDbGetItem('id', 'id'),
responseMappingTemplate: MappingTemplate.dynamoDbResultItem(),
});
customerDS.createResolver({
typeName: 'Mutation',
fieldName: 'addCustomer',
requestMappingTemplate: MappingTemplate.dynamoDbPutItem(
PrimaryKey.partition('id').auto(),
Values.projecting('customer')),
responseMappingTemplate: MappingTemplate.dynamoDbResultItem(),
});
customerDS.createResolver({
typeName: 'Mutation',
fieldName: 'saveCustomer',
requestMappingTemplate: MappingTemplate.dynamoDbPutItem(
PrimaryKey.partition('id').is('id'),
Values.projecting('customer')),
responseMappingTemplate: MappingTemplate.dynamoDbResultItem(),
});
customerDS.createResolver({
typeName: 'Mutation',
fieldName: 'saveCustomerWithFirstOrder',
requestMappingTemplate: MappingTemplate.dynamoDbPutItem(
PrimaryKey
.partition('order').auto()
.sort('customer').is('customer.id'),
Values
.projecting('order')
.attribute('referral').is('referral')),
responseMappingTemplate: MappingTemplate.dynamoDbResultItem(),
});
customerDS.createResolver({
typeName: 'Mutation',
fieldName: 'removeCustomer',
requestMappingTemplate: MappingTemplate.dynamoDbDeleteItem('id', 'id'),
responseMappingTemplate: MappingTemplate.dynamoDbResultItem(),
});
}
}