Search code examples
facebook-graph-apielixir

Elixir - efficiently fetching list of comments of a Facebook page via Graph API


I want to display a list of comments of a Facebook page on my web app and implement some sort of lazy load. In order to fetch all comments, I must first fetch a list of posts. Both the /comments and the /posts would return a response with this shape:

{
  "data": [...],
  "paging": {
    "cursors": {...},
    "next": "https://graph.facebook.com/v12.0/..."
  }
}

I've thought of parallelizing the fetching of comments via Task.async/1 and Task.await/2 like this:

posts = MyAppGetter.fetch_posts("https://graph.facebook.com/v12.0/123456789/posts")
comments =
  posts
  |> Enum.map(fn(p) -> Task.async(fn -> 
    MyAppGetter.fetch_comments("https://graph.facebook.com/v12.0/#{p.id}/comments") 
  end) end)
  |> Enum.map(&Task.await/1)

My dilemma is not knowing when to request for the "next" page, since the /posts endpoint doesn't return the total number of comments each post has. Ideally, I would like to display a total of 100 comments initially and subsequently. However, with the lack of information there could be 0 comments for the first page of posts and 200 comments in the next page.


Solution

  • You can get the comments count by explicitly asking for it.

    page-id/feed?fields=comments.summary(1)
    

    will get you a structure like

      "comments": {
        "data": [
          ...
        ],
        "summary": {
          "order": "ranked",
          "total_count": 123,
          "can_comment": true
        }