DEV Community

Cover image for Data Modeling in Depth with GraphQL & AWS Amplify - 17 Data Access Patterns

Data Modeling in Depth with GraphQL & AWS Amplify - 17 Data Access Patterns

Nader Dabit on November 22, 2019

Thanks to Richard Threlkeld and Attila Hajdrik for their help writing this article. How to implement a real-world & comprehensive data model...
Collapse
 
ricardo1980 profile image
Ricardo Ruiz Lopez

Hello Nader,

Thanks a lot for your article, very interesting.
However, I don't know if I can do what I want to do using DynamoDB. It seems what I want is more complex. Can you review this?
Imagine I want to develop a dating app, when a user (entity) likes (relationship) another user (entity), is liked by another user, blocks another user, is blocked by another user, dislikes another user....
That kind of relationships may occur thousands of times, for example, you could like 10000 users.
Can I model that using DynamoDB? Using different tables I guess I can do it. But what I don't know is how to execute this request:

  • Get users that never blocked me, I never liked them, I never dislike them, I never blocked them. Close my area (I guess this is just testing longitude and attitude inside a range) and sorted by creation date (or any other field). Paginated.

Can I do that using DynamoDB or do I have to use Aurora? I know how to do that using SQL, but I don't know how to model this using DynamoDB (NoSQL) or even if it is possible.

Can I have your opinion about this?
Thanks a lot for suggestions.

Ricardo.

Collapse
 
codemochi profile image
Code Mochi

Hey Ricardo,
I'm obviously not Nader, but what if you created two tables "users" and "userLikes"? When someone clicks the like button, you create a new "userLike" document where you have a liker and a likee field? You could then easily do lookups based on the liker and likee, especially if you added them as one to many relationships on the "user" document. This would be the basic relationships:

user {
   admirers: [userLikes]
   crushes: [userLikes]
}

userLikes {
   liker: user
   likee: user
   isBlocked: boolean/string
}

You could add latitude/longitude and use the between that Nader mentions above and then use booleans for some of those other specifications such as blocked.

Collapse
 
petroslafaz profile image
Petros Lafazanidis

Thank you Nader this is a fantastic resource.

A bit late here in London, so I might be missing the obvious, but anyways here is a pedestrian question about item 14.
The same can be achieved by

listEmployees(filter: {jobTitle: {eq: "Manager"}})

with the benefit of being able to use filter with other operators. So what is the benefit of your approach? Is is performance mainly?

thanks
Petros

Collapse
 
dabit3 profile image
Nader Dabit

So either can be used, but the query on index is preferred because a filter with scan will scan the entire database and then discard the items based on the filter, while the query will only query the items needed so the operation is less expensive in general.

Collapse
 
rafaelfaria profile image
Rafael Cardoso

Really good resource Nader.

What I find challenging is when I model my graphql schema, without Auth they work well, as soon as I add authentication, then I start getting problems with it. Mostly with many to many relationship with nested models.

Have you come across any of that? Would you be able to add this example and introduce auth to it?

Thanks

Collapse
 
alexwhite24 profile image
AN

You can break the Many to Many Schema with a intermediate Table.
Example Group to Users
Model:

  • Group
  • User
  • GroupUser This will have ID for Both the Group and User as Linkage

Also, just wondering does AWS Amplify charges link to DB Model Operation? Can't find it any place like how much is model we create how much data operation happens with the model.

-Regards
Alex

Collapse
 
willdent profile image
Will Dent

Nader,
I'm struggling a little on how you represent Order Line Items. In the original documentation it has a Order Line Items table to represent more than one product on the order. It would be fantastic if you could show how that would work in this context. Many Thanks.

Collapse
 
clydet profile image
Clyde Tedrick

guess that's a no

Collapse
 
alexwhite24 profile image
AN • Edited

Hey !

Just wondering what will happen to existing data if the data model is changed?

-Regards
Alex