amplify-cli icon indicating copy to clipboard operation
amplify-cli copied to clipboard

Best way to limit subscription results?

Open mrgrue opened this issue 6 years ago • 6 comments

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?

mrgrue avatar Aug 08 '20 23:08 mrgrue

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?

akshbhu avatar Aug 10 '20 07:08 akshbhu

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?

mrgrue avatar Aug 10 '20 19:08 mrgrue

Any other ideas here?

mrgrue avatar Aug 13 '20 17:08 mrgrue

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

edwardfoyle avatar Aug 17 '20 19:08 edwardfoyle

This issue has been automatically closed because of inactivity. Please open a new issue if you are still encountering problems.

stale[bot] avatar Aug 25 '20 23:08 stale[bot]

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.

github-actions[bot] avatar May 25 '21 23:05 github-actions[bot]