Amplify API should add support for bachOperations - i.e. generate the appropriate resolvers and schema.graphql for DynamoDB (and possibly Aurora Serverless in the future).
The support could follow the AWS own AppSync tutorial:
https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-dynamodb-batch.html
Once supported, the applications can use those batch operations to replace suboptimal multiple single item operations.
The current graphql subscriptions should not be broken (i.e. when multiple items are created/updated/deleted via batch operation, the application should still get the appropriate notifications about those items via subcription).
What could be discussed - best between AWS Amplify API and AWS AppSync teams - is whether some new "batch" subcriptions can be added to the API (so that application can get notification about multiple created/updated/deleted items via one subscription message).
Thanks for the feedback. I will record this feature request so that it can get prioritized. Philosophically there is no reason we can't support batch operations out of the box but doing so will also require adding support via the @auth & @versioned directives as well. As a mitigation, you will be able to write your own resolvers that target the full AppSync surface area as soon as this PR is merged (#581).
What could be discussed - best between AWS Amplify API and AWS AppSync teams - is whether some new "batch" subcriptions can be added to the API (so that application can get notification about multiple created/updated/deleted items via one subscription message).
You can do this today as long as you specify the same return types for the mutation and subscription. For example you can do the following:
type Mutation {
batchPut(items: [ItemInput]): [Item]
}
type Subscription {
onBatchPut: [Item] @aws_subscribe(mutations:"batchPut")
}
@silvesteradamik
We've added functionality for custom resolvers. Please use npm install -g @aws-amplify/cli
to install the latest version of the CLI. For documentation regarding it, please refer to https://aws-amplify.github.io/docs/cli/graphql#overwrite-a-resolver-generated-by-the-graphql-transform
Here's the launch announcement for the same - https://aws.amazon.com/blogs/mobile/amplify-adds-support-for-multiple-environments-custom-resolvers-larger-data-models-and-iam-roles-including-mfa/
Maybe you could try out custom resolvers and see if it solves your use case for the time being. Let me know if you've any concerns around it.
@mikeparisstuff what would be the best way to overcome the 25 max limit in dynamoDB when working with batch updates? Can you split the operations in the resolver?
You can do this today as long as you specify the same return types for the mutation and subscription. For example you can do the following:
type Mutation { batchPut(items: [ItemInput]): [Item] } type Subscription { onBatchPut: [Item] @aws_subscribe(mutations:"batchPut") }
@mikeparisstuff should this example actually work today? I have this exact thing set up and the subscription does not fire on batch mutations. The batch mutations are working correctly and other subscriptions on single item mutations also fire, but I have not been able to get this to work. Would love to know whether I should continue to troubleshoot this or whether the functionality does not actually exist.
I love amplify so far. As soon as I saw what I need to do to do batch reads/writes I thought to myself, I bet the team will fix this one day (I know you guys are serious about DX). I.e., .vtl
isn't exactly a common language among front-end engineers...
Same here. I tried to read 3 different articles + the docs (this is all I could find on the internet related to batch operations ) and I still have no idea how I can accomplish this. Super complicated and I tried a lot to deep dive into vtl and tried to understand the config file in the stacks folder with a lot of terms I have no idea what they mean. No success.
@nathanqueija following this guide worked for me. Agreed that .vtl
is quite opaque though.
https://medium.com/@jan.hesters/creating-graphql-batch-operations-for-aws-amplify-with-appsync-and-cognito-ecee6938e8ee
Thanks @janhesters
A few gotchas I ran into:
DataSourceName
should have can be found by running amplify console api
and clicking "Data Sources" on the leftYou can do this today as long as you specify the same return types for the mutation and subscription. For example, you can do the following:
```
type Mutation {
batchPut(items: [ItemInput]): [Item]
}type Subscription {
onBatchPut: [Item] @aws_subscribe(mutations:"batchPut")
}
@mikeparisstuff : should this example actually work today? I have this exact thing set up and the subscription does not fire on batch mutations.
Can you please provide a proper solution for subscription on batch mutation?
Most helpful comment
@nathanqueija following this guide worked for me. Agreed that
.vtl
is quite opaque though.https://medium.com/@jan.hesters/creating-graphql-batch-operations-for-aws-amplify-with-appsync-and-cognito-ecee6938e8ee
Thanks @janhesters
A few gotchas I ran into:
DataSourceName
should have can be found by runningamplify console api
and clicking "Data Sources" on the left