-
-
Notifications
You must be signed in to change notification settings - Fork 569
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new docs page for the ApplyTransforms step. Add a new section i…
…n the each step docs with more example usage. Add a new section in PgSelect to link to each and applyTransforms to describe additional behavior a user may be interested in
- Loading branch information
Steven Barsam
committed
Aug 26, 2024
1 parent
b54dd33
commit 638366e
Showing
3 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
51 changes: 51 additions & 0 deletions
51
grafast/website/grafast/step-library/standard-steps/applyTransforms.md
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# applyTransforms | ||
|
||
Takes a step as the first argument and returns a step that guarntees all `listItem` transforms have occurred. | ||
|
||
This step is useful for when you need all of: | ||
* the `listItem` transforms to have already taken place (e.g. you're going to | ||
* send the result to an external service) rather than processing them through | ||
* the GraphQL response | ||
|
||
This is very useful for modifying the values of an opaque step! | ||
|
||
|
||
## Type | ||
|
||
```ts | ||
function applyTransforms( | ||
$step: ExecutableStep | ||
): ExecutableStep<any>; | ||
``` | ||
|
||
### Example | ||
|
||
Let's say you have a `PgSelect` step and you want to apply logic to the values of said step. You may be inclined to do something like this: | ||
|
||
```ts | ||
const $users = usersResource.find(); | ||
const tbl = $users.alias; | ||
$users.where(sql`${tbl}.username = 'Benjie'`); | ||
// Options that could be used here include: loadOne, loadMany, lambda | ||
return lambda($users, (users) => { | ||
return users.map(user => ({ | ||
username: 'USER-' + user.username, | ||
...user, | ||
})) | ||
}, true); | ||
``` | ||
|
||
Due to `PgSelect` being an opaque step, this will not work! The values of `$users` will not be loaded by the time the `lambda` step is run. In order to guarantee that those values are available, you can wrap the `$users` step in an `applyTransforms`! | ||
|
||
```ts | ||
const $users = usersResource.find(); | ||
const tbl = $users.alias; | ||
$users.where(sql`${tbl}.username = 'Benjie'`); | ||
// By using applyTransforms, it guarantees these values will be available | ||
return lambda(applyTransforms($users), (users) => { | ||
return users.map(user => ({ | ||
username: 'USER-' + user.username, | ||
...user, | ||
})) | ||
}, true); | ||
``` |
This file contains 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