0

The problem: My batch request returns passed group events and the current batch request will scale badly with users creating future events.

I have the following batch request:

{
   "batch": [
   {
       "method": "GET",
       "relative_url": "me"
   },
   {
       "method": "GET",
       "relative_url": "me/groups?fields=events{id,name,start_time}"
   },
   {
        "method": "GET",
        "relative_url": "me/accounts"
   }
   ],
  "include_headers": "false"
}

This request will fetch (among other things) all user group events. I only need upcoming events.

Question: How can I apply filters in a Batch request?

The following query URL is not working when used as a relative_url:

me/groups?fields=events{id,name,start_time}&since=1599036240

The filter parameter is:

since=MY_CURRENT_TIME

The documentation I read wasn't of much help. I don't even know if it is possible to add filters (fields) to a batch request. ( https://developers.facebook.com/docs/graph-api/making-multiple-requests/ )

https://developers.facebook.com/community/threads/325616225418947/?notif_id=1599002283105969&notif_t=developer_community_forum_thread_update&ref=notif

I also asked the same question on the FBs dev forum with no answer (so far): https://developers.facebook.com/community/threads/325616225418947/?notif_id=1599002283105969&notif_t=developer_community_forum_thread_update&ref=notif

UPDATE 1 (02.09.2020.):

@CBroe pointed out that the Batch Requests URLs should work the same as standalone requests. This leads me to believe that my relative_url does not filter properly. Testing:

me/groups?fields=events{id,name,start_time}&since=1599036240

with output:

events": {
        "data": [
          {
            "id": "324058568947509",
            "name": "Testing FB Graph API",
            "start_time": "2020-08-25T15:00:00+0200"
          }
        ],

conforms that filtering does not work with &since=1599036240 and the URL containing {}.

Darkwonder
  • 1,149
  • 1
  • 13
  • 26
  • _“The following query URL is not working when used a relative_url”_ - that’s the user’s events now, did you mean those here, or are we talking about group events? – CBroe Sep 02 '20 at 06:43
  • 1
    Batching requests should not change how the individual requests work. If an endpoint can be filtered, then that should work the same way in batch. But `me/groups?fields=events{id,name,start_time}&since=1598509165` on its own returns events from 2017 & 2018 for me already. (I don’t know if that could be due to a lack of more current events; I don’t know if the API decides to go past `since` if there is not data otherwise?) – CBroe Sep 02 '20 at 06:47
  • @CBroe thank you for the quick response and feedback. I copied the wrong URL. Updated the answer with group events URL. – Darkwonder Sep 02 '20 at 08:53
  • But `me/groups?fields=events{id,name,start_time}&since=1599036240` executed directly, not inside a batch, gives you the correct results you are expecting? – CBroe Sep 02 '20 at 08:54
  • @CBroe A request with the correct time (now) returns an empty array. I have tested this with user events. The user events URL looks like this: me/events?fields=end_time,start_time,id,name&since=1599036240 – Darkwonder Sep 02 '20 at 09:01
  • @CBroe No. It returns a similar result like yours (incorrect). I updated the answer. It seems that filtering does not work the same way (for different resources). – Darkwonder Sep 02 '20 at 09:09
  • It’s a matter of syntax, I added an answer that explains the details. With the correct syntax, it should work the same way in a batch request as well. – CBroe Sep 02 '20 at 09:33

1 Answers1

1

The format in which the parameters are supplied is the issue here.

If you were going via the events endpoint directly, then supplying the since parameter this way would work: /group-id/events?since=…

But you are not using that format, you are going via me/groups?fields=events - and in that case, specifying since as an additional GET parameter on the same “level” as fields won’t work.
The reason is that you could be requesting data from more than one endpoint this way -
?fields=events,somethingelse&since=… - do we want to filter the results of events or somethingelse by the since value here, that would be ambigous.

So, field expansion syntax needs to be used here:

?fields=events.since(1599036240){id,name,start_time}

This way, that since parameter is explicitly tied to the events endpoint here (and you could specify somethingelse.since(…) differently here for a second endpoint.)

(And the parameters need to be in this order, events{id,name,start_time}.since(…) would give a syntax error, the field list first with {…} and then the dot syntax isn’t understood by the query parser.)

CBroe
  • 91,630
  • 14
  • 92
  • 150
  • Thank you "over 9000". Is there a in-depth tutorial about Facebook Graph API? Would you expect an error for my incorrect request or just the incorrect response(which I got based on my incorrect request)? – Darkwonder Sep 02 '20 at 09:59
  • 1
    Field expansion is covered under https://developers.facebook.com/docs/graph-api/using-graph-api/#field-expansion and https://developers.facebook.com/docs/graph-api/advanced#fieldexpansion – CBroe Sep 02 '20 at 10:21
  • The “incorrect” request does not give any syntax errors, nor is that to be expected, it was just a combination of parameters that did not achieve the desired result in this instance, but it wasn’t an actual syntax error. Plus, the API is generally “forgiving” about additional GET parameters being added, you can do `me?fields=id,name&foo=bar` and that will not result in an error either. – CBroe Sep 02 '20 at 10:24