Skip to content

Commit

Permalink
Get Next and Previous should not use cursors
Browse files Browse the repository at this point in the history
Summary:
https://developers.facebook.com/docs/graph-api/results/
- next : The Graph API endpoint that will return the next page of data. If not included, this is the last page of data. Due to how pagination works with visibility and privacy, it is possible that a page may be empty but contain a next paging link. Stop paging when the next link no longer appears.
- previous : The Graph API endpoint that will return the previous page of data. If not included, this is the first page of data.

===
For example: If my cursor has total 4 items (default limit is 25)
```
$cursor->setDefaultUseImplicitFetch(true);
foreach ($cursor as $item) {
    $data[] = $item->exportAllData();
}
```

So I need to make one request to get the cursor with 4 items. The current code  **getNext()** will always return a URL and
```
public function fetchAfter() {
    $request = $this->createAfterRequest();
    if (!$request) {
      return;
    }

    $this->appendResponse($request->execute());
  }
```

**fetchAfter()** will **execute** one more request and return an empty data. So, we need to make 2 requests to get for only 4 items.

So if **$content['paging']['next']** is empty, we should return null and do not need to make more requests

X-link: facebook/facebook-php-business-sdk#623

Reviewed By: satwikareddy3

Differential Revision: D67995304

Pulled By: stcheng

fbshipit-source-id: 78e3cc4167e10f228e7a3c4e572c39b87634c602
  • Loading branch information
Khien authored and facebook-github-bot committed Feb 7, 2025
1 parent b19df2b commit c63697a
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 15 deletions.
14 changes: 0 additions & 14 deletions templates/php/src/FacebookAds/Cursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,6 @@ public function getPrevious() {
return $content['paging']['previous'];
}

$before = $this->getBefore();
if ($before !== null) {
$request = $this->createUndirectionalizedRequest();
$request->getQueryParams()->offsetSet('before', $before);
return $request->getUrl();
}

return null;
}

Expand All @@ -293,13 +286,6 @@ public function getNext() {
return $content['paging']['next'];
}

$after = $this->getAfter();
if ($after !== null) {
$request = $this->createUndirectionalizedRequest();
$request->getQueryParams()->offsetSet('after', $after);
return $request->getUrl();
}

return null;
}

Expand Down
3 changes: 2 additions & 1 deletion templates/php/test/FacebookAdsTest/CursorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ protected function createSampleResponseContent() {
'after' => $this->getUniquePageId(),
'before' => $this->getUniquePageId(),
),
'next' => $this->createUnparameterizedUrl() . '?after=' . $this->getUniquePageId(),
'previous' => $this->createUnparameterizedUrl() . '?before=' . $this->getUniquePageId(),
),
) + $this->createEmptyResponseContent();

Expand Down Expand Up @@ -339,7 +341,6 @@ public function testImplicitFetch() {
$response = $this->createResponseChainMock(3);
$cursor = new Cursor($response, $this->objectPrototype);

$count = 0;
while ($cursor->valid()) {
$cursor->prev();
}
Expand Down

0 comments on commit c63697a

Please sign in to comment.