Best way to limit subscription results?
Which Category is your question related to? Amplify CLI
Amplify CLI Version 4.26.0
I'd like to limit which records are shown to a client in a subscription. For example:
type Todo @model {
id: ID!
name: String!
visibility: TodoVisibility
}
enum TodoVisibility {
OPEN
CLOSED
}
The client is using an onCreateTodo subscription, but I want to make sure that the client only ever sees Todo records with the visibility set to OPEN.
What would be ideal would be if I could do the following, creating a subscription where one of the parameters is set and can't be overridden from the client:
type Subscription {
onCreateOpenTodo(visibility: OPEN): Todo
@aws_subscribe(mutations: ["createTodo"]
}
...but that's not a valid syntax.
What's the best way to accomplish something like this?
Hi @mrgrue
Yes you can do that. You can find the documentation here :https://docs.amplify.aws/cli/graphql-transformer/examples#filter-subscriptions-by-model-fields-andor-relations.
Let me know if this works for you?
Thanks for replying, @akshbhu! Unfortunately, I don't think those docs are doing what I need.
If I understand the documentation correctly, the example allows for filtering of subscriptions based on what the client passes in for "content". I don't want to leave it up to the client. I want the client to only ever be able to receiving subscription events for my Todo model if the visibility field is set to "OPEN". If it's set to "CLOSED", they shouldn't get anything... and most importantly, the client should have no control over this.
Am I misreading the documentation?
Any other ideas here?
Hi @mrgrue I think the best way to do this would be to overwrite the VTL resolver for that subscription with a hardcoded visibility. You can read our docs on how to do that here: https://docs.amplify.aws/cli/graphql-transformer/resolvers#overwriting-resolvers
This issue has been automatically closed because of inactivity. Please open a new issue if you are still encountering problems.
This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.
Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.