-
Notifications
You must be signed in to change notification settings - Fork 17
Introduce a dedicated PaginatedResource object #316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
30b94ee to
d35cb34
Compare
Greptile OverviewGreptile SummaryIntroduced Key Changes:
Benefits:
Testing:
Confidence Score: 5/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant Client as Client Code
participant Service as Service Class<br/>(e.g., UserManagement)
participant API as WorkOS API
participant PR as PaginatedResource
participant Resource as Resource Class<br/>(e.g., User)
Client->>Service: listUsers(limit, after, etc.)
Service->>API: GET /user_management/users
API-->>Service: JSON response with data array<br/>and list_metadata
Service->>PR: constructFromResponse(response, User::class, 'users')
PR->>PR: parsePaginationArgs(response)
Note over PR: Extract before/after cursors
loop For each item in response["data"]
PR->>Resource: constructFromResponse(item)
Resource-->>PR: User object
end
PR->>PR: new PaginatedResource(before, after, users, 'users')
PR-->>Service: PaginatedResource instance
Service-->>Client: PaginatedResource
Note over Client: Access Pattern 1: Bare destructuring
Client->>PR: [$before, $after, $users] = result
PR-->>Client: offsetGet(0), offsetGet(1), offsetGet(2)
Note over Client: Access Pattern 2: Named destructuring
Client->>PR: ["users" => $users] = result
PR-->>Client: offsetGet('users')
Note over Client: Access Pattern 3: Fluent access
Client->>PR: result->users
PR-->>Client: __get('users')
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
11 files reviewed, no comments
Previously, when returning data from a resource that was paginated, we
would parse the pagination args off the resposne, loop over the "data"
value in the response, and map each item in the JSON array to a
specific resource. An example of this would be in the listUsers() method
of the UserManagement class (truncated example below):
```php
$users = [];
list($before, $after) = Util\Request::parsePaginationArgs($response);
foreach ($response["data"] as $responseData) {
\array_push($users, Resource\User::constructFromResponse($responseData));
}
return [$before, $after, $users];
```
Performing this pattern over and over again resulted in a lot of
duplicate code that was doing basically nothing more than an array_map.
Additionally, this return is extremely limited and forces the user into
a limited and specific pattern of either bare array destructuring:
```php
[$before, $after, $users] = $userManagement->listUsers();
```
Or dealing with 0-indexed array values:
```php
$result = $userManagement->listUsers();
```
If for example they just want the first 5 users and don't care about paginating,
this means they'd to either write destructuring that has empty values:
```php
// Huh?
[,,$users] = $userManagement->listUsers(limit: 5);
```
Or they'd have to drop down to
```php
$results = $userManagement->listUsers(limit: 5);
// How do I discover or know what this index is?
$users = $results[2];
```
To fix both of these issues, without affecting current library
consumers, I'm proposing that we create a `Resource\PaginatedResource` class that:
1. DRYs and standardizes the creation of a paginated resource
2. Handles the resource mapping from the data array
3. Continues to allow for bare destructuring (backwards compatible)
4. Introduces named destructuring (e.g `$result["after"]` or
`["users" => $fiveUsers] = $userManagement->listUsers(limit:5)`)
5. Introduces fluent property access (e.g. `$result->after` or
`$result->users`)
The change is fully backwards compatible, cleans up existing resource
code and allows for developers to use the library in whichever code
style is consistent with their project.
For example, it lets you turn this code:
```php
[$before, $after, $users] = $userManagement->listUsers();
while ($after) {
[$before, $after, $currentPage] = $sso->listConnections(
limit: 100,
after: $after,
order: "desc"
);
$users = array_merge($users, $currentPage);
}
```
Into this code:
```php
$users = [];
$after = null;
do {
$result = $userManagement->listUsers(after: $after, limit: 10);
$users = array_merge($allUsers, $result->users);
$after = $result->after;
} while ($after !== null);
```
This was the only paginated method not using the new PaginatedResource class introduced in 011e80c. Now all paginated methods consistently return PaginatedResource with support for bare destructuring, named destructuring, and fluent property access.
d35cb34 to
1450ea8
Compare
|
With 1450ea8, this is now ready to ship imo. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No files reviewed, no comments
Description
Previously, when returning data from a resource that was paginated, we would parse the pagination args off the response, loop over the "data" value in the response, and map each item in the JSON array to a specific resource. An example of this would be in the
listUsers()method of theUserManagementclass (truncated example below):Performing this pattern over and over again resulted in a lot of duplicate code that was doing basically nothing more than an array_map.
Additionally, this return is extremely limited and forces the user into a limited and specific pattern of either bare array destructuring:
Or dealing with 0-indexed array values:
If for example they just want the first 5 users and don't care about paginating, this means they'd need to either write a destructuring expression that has empty values:
Or they'd have to drop down to:
To fix both of these issues, without affecting current library consumers, I'm proposing that we create a
Resource\PaginatedResourceclass that:Resource\PaginatedResource::constructFromResponse($response, Resource\User::class, 'users');)$result["after"]or["users" => $fiveUsers] = $userManagement->listUsers(limit:5))$result->afteror$result->users)The change is fully backwards compatible, cleans up existing resource code and allows for developers to use the library in whichever code style is consistent with their project.
For example, it lets you turn this code:
Into this code:
Documentation
Does this require changes to the WorkOS Docs? E.g. the API Reference or code snippets need updates.
If yes, link a related docs PR and add a docs maintainer as a reviewer. Their approval is required.