aws-amplify / amplify-cli

The AWS Amplify CLI is a toolchain for simplifying serverless web and mobile development.
Apache License 2.0
2.82k stars 820 forks source link

How to filter an array property when making graphl query #8076

Closed homosape closed 3 years ago

homosape commented 3 years ago

Note: If your question is regarding the AWS Amplify Console service, please log it in the AWS Amplify Console repository

Which Category is your question related to? GraphQL queries

Amplify CLI Version 5.3.0 You can use amplify -v to check the amplify cli version on your system

What AWS Services are you utilizing? S3, DynamoDB, Cognito, AppSync, Amplify

Provide additional details e.g. code snippets This is a post entity from my graphql schema

Screenshot 2021-09-01 at 12 44 29

Post contains property "favorited" which will be an array of user ids (id when you get upon registration);

Upon creation of a post "favorited" is an empty array but if someone marks a post as his/her favorite, post's "favorited" array will be updated and user's ID will be added on top of the existing list of IDs (if any);

Let's say app has 100 posts from different users and I added to my favorites only 5 posts.

If I want to only see posts that I marked as my favorite, how will I be able to query them? Can I list posts where "favorited" array contains my ID?

Is something like this possible? let filter = { favorited: { contains: "my ID" } };

await API.graphql({ query: listPosts, variables: { filter: filter}}));

danielleadams commented 3 years ago

Hi @irakli-g, it's not possible to use contains on an array list, but have you looked at using the @searchable directive? This may solve the use case you are trying to address. (Docs: https://docs.amplify.aws/cli/graphql-transformer/searchable/#usage)

homosape commented 3 years ago

Hi @danielleadams, thanks for suggestion.

I've checked it. It's a fancy feature but I am not sure how useful it can be for my case. I assume creating many to many relationship in my schema would do the trick.

It's mentioned in this tutorial starting from 20:00. I will test this case. https://www.youtube.com/watch?v=eUQvsuO6EnU&list=WL&index=4&ab_channel=EnlearAcademy

danielleadams commented 3 years ago

I think creating a many-to-many relationship would help too. I talked more with team members, and it looks like using contains actually does work when querying an array in a GraphQL endpoint too.

homosape commented 3 years ago

I confirm that using many to many relationship does help to solve this problem.

github-actions[bot] commented 3 years ago

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.