graphql-recipes icon indicating copy to clipboard operation
graphql-recipes copied to clipboard

No way to display likes (Instagram Clone)

Open davidbiller opened this issue 5 years ago • 10 comments

If you build it that way, we don't know if the user has already liked a post?!

davidbiller avatar Dec 06 '19 21:12 davidbiller

@dabit3 i am right here?

davidbiller avatar Jun 13 '20 19:06 davidbiller

Have you found a way to do so?

rpn31 avatar Jan 14 '21 03:01 rpn31

@patrianova I guess there is no out of the box way for this.

likes: [PostLike] @connection(keyName: "postLikesByPostId", fields: ["id"])

this is a bad idea. if there are more likes than 1MB Data, than you can't count here the likes, and you don't know if your user has liked this post.

So you could use a table for likesCount. Every time a user likes a post, you can count up in this table for this post. And for the "Show if user liked it already" problem, you could save a document in a table with: user_id+post_id as id.

every post has to check than if there is a entry with this id.

else you could use elasticSearch for this. But this is a more expansive and self scaling way.

Any idea else?

davidbiller avatar Jan 14 '21 12:01 davidbiller

@davidbiller reading a bit more, I believe you could use resolvers to do this. Pipeline resolvers seems to be an option where upon a request to posts you could then rapidly search if that particular post is liked by the user requesting and create a field on the document on the fly indicating it.

You'd keep the PostLike relationship and that's where you execute your mutation (create a record) and on the queries to fetch the result you implement the custom resolver.

This said, I haven't done anything related to this yet and it would be great to have @dabit3 giving us some direction if possible.

rpn31 avatar Jan 14 '21 15:01 rpn31

@davidbiller reading a bit more, I believe you could use resolvers to do this. Pipeline resolvers seems to be an option where upon a request to posts you could then rapidly search if that particular post is liked by the user requesting and create a field on the document on the fly indicating it.

You'd keep the PostLike relationship and that's where you execute your mutation (create a record) and on the queries to fetch the result you implement the custom resolver.

This said, I haven't done anything related to this yet and it would be great to have @dabit3 giving us some direction if possible.

But how would you search? as a scan? this takes to long in dynamoDB if there are 100k entries. When the user scrolls through the images.

davidbiller avatar Jan 14 '21 16:01 davidbiller

you wouldn't do this per post but rather on the query that returns them.

if you get the first 100 posts on a query, on the resolver of this query is where you'd iterate through the 100 posts and search if it has been liked or not.

see below https://docs.aws.amazon.com/appsync/latest/devguide/pipeline-resolvers.html

rpn31 avatar Jan 14 '21 16:01 rpn31

you wouldn't do this per post but rather on the query that returns them.

if you get the first 100 posts on a query, on the resolver of this query is where you'd iterate through the 100 posts and search if it has been liked or not.

see below https://docs.aws.amazon.com/appsync/latest/devguide/pipeline-resolvers.html

But the pipeline resolver is using the scan operator of dynamodb?

davidbiller avatar Jan 14 '21 16:01 davidbiller

@davidbiller I'm not quite there yet :) but I'm assuming you could run two graphql queries on the resolver and merge them programmatically. I'll do some reading and let you know what I find out. If you do find an answer, please share here.

rpn31 avatar Jan 14 '21 16:01 rpn31

@davidbiller I'm not quite there yet :) but I'm assuming you could run two graphql queries on the resolver and merge them programmatically. I'll do some reading and let you know what I find out. If you do find an answer, please share here.

Ja, this is the problem here, you only can search in dynamoDB with a index key or with scans. And scans are expansive and it takes to long.

davidbiller avatar Jan 14 '21 17:01 davidbiller

@dabit3 any soultion ?

biller-aivy avatar Feb 17 '22 21:02 biller-aivy