-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Changes from all commits
dd4d586
e818aab
fe4580c
e291489
9d2dab8
b458304
9f19025
b207925
90367e4
af6bfd5
5692dd4
8ea282b
78cd8da
a60c54d
8cc7f10
aaf1724
55dd967
19c71e7
eed95fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
}, | ||
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', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. THis is a free text and could be anything the user wants There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can also write |
||
}); | ||
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(), | ||
}); | ||
} | ||
} | ||
``` |
There was a problem hiding this comment.
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?