aws-amplify / aws-amplify/amplify-android
[API] Subscribe with arguments (real time filtering)
- Dominant language
- Java
- Stars
- 287
- Forks
- 132
- Avg merge
- 2d 2h
- Merged PRs (30d)
- 45
Description
There should be a way to pass arguments to subscriptions so that only a subset is subscribed.
Model schema -
```graphql
type MyData @model {
id: ID!
x: Int
other_data: String
}
```
Subscription method `onUpdateMyData` which take an input parameter `x` -
```graphql
type Subscription {
onUpdateMyData(x: Int): MyData
@aws_subscribe(mutations: ["updateMyData"])
}
```
Mutation method `updateMyData` -
```graphql
type Mutation {
updateMyData(input: UpdateMyDataInput!, condition: ModelMyDataConditionInput): MyData
}
```
Following the [official documentation](https://docs.amplify.aws/lib/graphqlapi/subscribe-data/q/platform/android), I wrote the below code for subscription. Works as expected, i.e, gets triggered whenever the `updateMyData` mutation is called, irrespective of the value of x.
```kotlin
Amplify.API.subscribe(ModelSubscription.onUpdate(MyData::class.java),
{ Log.i("ApiQuickStart", "Subscription established") },
fun(onUpdate: GraphQLResponse) {
if (onUpdate.hasData()) {
Log.i("ApiQuickStart", "Update: " + onUpdate.data.toString())
} else if (onUpdate.hasErrors()) {
Log.e("ApiQuickStart", "Error: " + onUpdate.errors.joinToString { it.message })
} else {
Log.i("ApiQuickStart", "Null update")
}
},
{ onFailure -> Log.e("ApiQuickStart", "Subscription failed", onFailure) },
{ Log.i("ApiQuickStart", "Subscription completed") }
)
```
But the problem is, I only want to subscribe for a specific value of x, say 5.
So, I tried by modifying the request -
```kotlin
ModelSubscription.onUpdate(MyData::class.java).apply {
putVariable("x", 5)
}
```
This made no difference. I'm still receiving updates for other values of x.
This is how I call `updateMyData` mutation from AppSync console -
```graphql
mutation {
updateMyData( input: {
id: 1,
x: 5,
other_data: "some updated info"
} ) {
id, x, other_data
}
}
```
Subscription in AppSync console works like a charm, i.e, it gets triggered only for a subset where `x = 5` -
```graphql
subscription {
onUpdateMyData(x: 5) {
id, x, other_data
}
}
```
Just to mention, DynamoDB is used as the data source.
Contributor guide
Research direction
Start at ModelSubscription.onUpdate and Amplify.API.subscribe, then compare the generated request with the AppSync subscription that uses onUpdateMyData(x: 5). Trace whether putVariable("x", 5) reaches the subscription request and confirm that updates with other x values are excluded; verify against the DynamoDB-backed example described in the issue.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, graphql, kotlin
- Domain
- api, backend
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100