Comment system with replies to comments

UPDATE! It’s fixed… Not sure how. But I tried deleting the indexes and added it to roles again and that did it! Will be releasing a blog about creating a serverless website with a comment system on my new website! Thanks for any future help I might get:D

Hi! I am trying to make a comment system with the ability to reply to comments. I have come pretty far but I am stuck and have no idea why this does not work. Could someone please give me some guidance? Thanks!

Query(
  Lambda(
    "article",
    Let(
      {
        comments: Map(
          Paginate(Match(Index("comments_by_article"), Var("article"))),
          Lambda(
            "comment",
            Let(
              {
                commentRef: Select("ref", Get(Var("comment"))),
                comment: Get(Var("comment")),
                name: Select(["data", "name"], Var("comment")),
                body: Select(["data", "body"], Var("comment")),
                replies: Map(
                  Paginate(Match(Index("replies_by_comment"), Var("commentRef"))),
                  Lambda("reply", Get(Var("reply")))
                )
              },
              { ref: Var("commentRef"), name: Var("name"), body: Var("body"), replies: Var("replies") }
            )
          )
        )
      },
      Var("comments")
    )
  )
)

This is the result of calling the function:

Call(Function("GetCommentsWithReplies"), "framework-of-the-future")

{
  data: [
    {
      ref: Ref(Collection("Comments"), "304543435045470785"),
      name: "Name",
      body: "body",
      replies: {
        data: []
      }
    },
    {
      ref: Ref(Collection("Comments"), "304545450038395457"),
      name: "Name",
      body: "body",
      replies: {
        data: []
      }
    }
  ]
}

If I am not totally blind… I have made sure that all the refs exists and are correct.

Thanks again!

1 Like

Hi @procrates That’s awesome you are moving forward. I look forward to an announcement for the blog post! :grinning_face_with_smiling_eyes:

Remember that each Get call will cost a Read Op. As a potential optimization, you can use an index with the values you need, rather than Get and Select the fields on the fly. It’s more storage, but it may be possible to save a bunch on Read and Compute ops if there are lots of comments to fetch. No guarantees :slight_smile: It’s up to you of course to see what optimizations actually work best with your data.

Cheers!

Query(
  Lambda(
    "article",
    Let(
      {
        comments: Map(
          Paginate(Match(Index("comments_by_article_values"), Var("article"))),
          Lambda(
            ["commentRef", "name", "body"],
            Let(
              {
                replies: Map(
                  Paginate(Match(Index("replies_by_comment_values"), Var("commentRef"))),
                  Lambda(
                    ["replyRef", "name", "body"],
                    { ref: Var("replyRef"), name: Var("name"), body: Var("body") }
                  )
                )
              },
              { ref: Var("commentRef"), name: Var("name"), body: Var("body"), replies: Var("replies") }
            )
          )
        )
      },
      Var("comments")
    )
  )
)

I could not agree more @ptpaterson

I love to optimize, but as of now I just get things rolling. I will try to implement your solution.

Thank you very much :smiley:

1 Like

Haha yes it’s easy for me to look at just one of the many queries you shared and say, “hey you can optimize here!”. You of course have lots of things to work on :relaxed:

1 Like

Implemented your solution and it is beautiful :heart: . Love the work you do with Fauna. No words for how much I appreciate the framework you at Fauna has built! Thank you very much all of you :heart_eyes:

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.