Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(appsync): add L2 constuct for AppSync #4438

Merged
merged 19 commits into from
Nov 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions packages/@aws-cdk/aws-appsync/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,94 @@
<!--END STABILITY BANNER-->

This module is part of the [AWS Cloud Development Kit](https://github.com/aws/aws-cdk) project.

## Usage Example

Given the following GraphQL schema file `schema.graphql`:

```graphql
type Customer {
id: String!
name: String!
}

input SaveCustomerInput {
name: String!
}

type Query {
getCustomers: [Customer]
getCustomer(id: String): Customer
}

type Mutation {
addCustomer(customer: SaveCustomerInput!): Customer
saveCustomer(id: String!, customer: SaveCustomerInput!): Customer
removeCustomer(id: String!): Customer
}
```

the following CDK app snippet will create a complete CRUD AppSync API:

```ts
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,
},
userPoolConfig: {
userPool,
defaultAction: UserPoolDefaultAction.ALLOW,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this required / what would the default be?

},
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',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are these something we'd want in an enum? I'm assuming they're finite & have a few values that are valid

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

THis is a free text and could be anything the user wants

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, finally started reading the user guide to get caught up here

fieldName: 'getCustomers',
requestMappingTemplate: MappingTemplate.dynamoDbScanTable(),
responseMappingTemplate: MappingTemplate.dynamoDbResultList(),
});
customerDS.createResolver({
typeName: 'Query',
fieldName: 'getCustomer',
requestMappingTemplate: MappingTemplate.dynamoDbGetItem('id', 'id'),
responseMappingTemplate: MappingTemplate.dynamoDbResultItem(),
Comment on lines +86 to +87
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe these could also point to an S3 location or accept them directly as string right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also write MappingTemplate.fromFile() or MappingTemplate.fromString()

});
customerDS.createResolver({
typeName: 'Mutation',
fieldName: 'addCustomer',
requestMappingTemplate: MappingTemplate.dynamoDbPutItem('id', 'customer'),
responseMappingTemplate: MappingTemplate.dynamoDbResultItem(),
});
customerDS.createResolver({
typeName: 'Mutation',
fieldName: 'saveCustomer',
requestMappingTemplate: MappingTemplate.dynamoDbPutItem('id', 'customer', 'id'),
responseMappingTemplate: MappingTemplate.dynamoDbResultItem(),
});
customerDS.createResolver({
typeName: 'Mutation',
fieldName: 'removeCustomer',
requestMappingTemplate: MappingTemplate.dynamoDbDeleteItem('id', 'id'),
responseMappingTemplate: MappingTemplate.dynamoDbResultItem(),
});
}
}
```
Loading