Reputation: 11
I'm currently working with AWS AppSync and found a web tutorial. (Link: https://www.youtube.com/watch?v=0Xbt7VqkJNc)
All in all, it's a nice example for beginners but I have some problems with creating the demonstrated mutation/query of the tutorial.
The issue is that it's not possible to set an individual ID number (for example "12345") because the output is always an auto-generated ID number. Is there an option to change the config in DynamoDB or is this problem connected with resolvers in AWS AppSync?
Code example:
mutation createTodo {
createTodo(input: {
name:"Get milk",
completed: false
}) {
id}
}
query getTodo {
getTodo(id: "afda5d05-bad0-4436-9f8b-76e92d1228c3" ) {
id name completed} }
Output:
{
"data": {
"getTodo": {
"id": "afda5d05-bad0-4436-9f8b-76e92d1228c3",
"name": "Get milk",
"completed": false
}
}
}
Upvotes: 1
Views: 395
Reputation: 4546
the example demonstrated in the video was accepting id
, name
and completed
fields in the input. If you want a randomly generated id each time, you can modify your schema to just pass in name
and completed
in the query createTodo
. You would then need to edit your resolver for this query field (you can do this by clicking on the createTodo
link under the Resolvers section on your Schemas page). You can edit the Request Mapping Template with the following definition, and this way, a random id is generated each time you create a Todo:
{
"version": "2017-02-28",
"operation": "PutItem",
"key": { "id": { "S": "$util.autoId()"} },
"attributeValues": {
"name": { "S": "$context.arguments.name" },
"completed": { "S": "$context.arguments.completed" }
}
}
If you see line #5 in the above example, we make use of an in-built utility function to generate an autoId: $util.autoId()
. This enables you to randomly set ids for your Mutations.
You can find this, plus some of our other supported VTL utility functions here.
Upvotes: 2