Closed ChurikiTenna closed 1 month ago
@ChurikiTenna Thanks for submitting the issue, we'll take a look and provide updates here.
Hi @ChurikiTenna ,
The subscribe
API is designed to send the .subscription
GraphQL request. However, we currently do not support adding conditions to subscription GraphQL requests. As an alternative, you can apply filtering on the onUpdate
subscription of the model using a specific identifier. For example:
let onUpdateSubscription = Amplify.Publisher.create(Amplify.API.subscribe(
request: .subscription(to: Post.self, subscriptionType: .onUpdate)
))
let cancellable = onUpdateSubscription.filter {
if case let .data(.success(result)) = $0 {
result.model.identifier == "specific-id"
} else {
false
}
}.sink { complete in
// on subscription end
} receiveValue: { onUpdateEvent in
// your logic
}
Hi @5d !
Thank you for the suggestion. Does that mean it listens all the Post.self updates? Isn't it going to require too much traffics when there are thousands of users?
Hi @ChurikiTenna ,
Does that mean it listens all the Post.self updates? Isn't it going to require too much traffics when there are thousands of users?
It will listen to all authorized Post.self updates. If that's a concern, there's another approach you can try, building a custom GraphQL query. In your case, it might look something like this:
let updateOnSpecificPostIdRequest = GraphQLRequest(
document: """
subscription OnSpecificPostIdUpdated {
onUpdatePost(filter: { id: { eq: "\(postId)" } }) {
id
otherFiled0
otherFiled1
...
}
}
""",
responseType: JSONValue.self
)
let subscription = Amplify.API.subscribe(request: updateOnSpecificPostIdRequest)
We recommend thoroughly reviewing the AWS AppSync documentation before attempting to write your own custom queries.
Thank you @5d ! I'll try the custom GraphQL query approach.
This issue is now closed. Comments on closed issues are hard for our team to see. If you need more assistance, please open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.
Describe the bug
This is not a bug. I think I am misusing the Amplify.API.subscribe.
I want to get UserData with specific id when it's updated.
I tried to use Amplify.API.subscribe like this:
let subscription = Amplify.API.subscribe(request: .list(UserData.self, where: UserData.keys.id.eq(userId)))
But it fails with error:
Do I need to define something first, or is such functionality not available?
Steps To Reproduce
Expected behavior
Get UserData.self with specific id when updated.
Amplify Framework Version
2.39.0
Amplify Categories
API
Dependency manager
Swift PM
Swift version
latest
CLI version
12.12.6
Xcode version
15.4
Relevant log output