Open
Conversation
30b94ee to
d35cb34
Compare
Contributor
Author
Contributor
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')
|
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
Contributor
Author
Contributor
Author
|
With 1450ea8, this is now ready to ship imo. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.