alexdebrie / graphql-dynamodb-single-table

Example application showing how to use single-table design with GraphQL and DynamoDB
35 stars 12 forks source link

Incorrect sort key in example #1

Open Nr18 opened 2 years ago

Nr18 commented 2 years ago

I haven't tested your solution bus was looking at the implementation and I think there is an error in your template:

See: https://github.com/alexdebrie/graphql-dynamodb-single-table/blob/23c5d95511f6207ac6b02d961c6071cac320b30a/lib/mapping-templates/Query.getPostsForSite.request.vtl#L8

Shouldn't the SK be POST#? The post is created with POST#${id} as sort key!

alexdebrie commented 2 years ago

Hey @Nr18, thanks for reaching out.

There are a few ways we could write this, but this should work. My key condition expression is specifying items whose SK is less than (<) the SITE#${domain} sort key, so it will exclude the Site object whose sort key is exactly that.

Let me know if you think I'm still wrong! Taking a quick glance at it but I think it should be correct :)

Nr18 commented 2 years ago

Ah right so because the P comes before the S in the alphabet it is smaller and thus the posts are fetched. Is that assumption correct?

So if that is correct and you would introduce AUTHOR#{id} in the same partition those would also be fetched as a side effect of this query. Therefor I would personally use the begins_with(SK, :sk) and alter the :sk too POST#. This way you guarantee that you are only fetching posts in the getPostsForSite call.

alexdebrie commented 2 years ago

Yep, that's exactly right! And it's a good point -- the begins_with() would be a little safer there :)