-
Notifications
You must be signed in to change notification settings - Fork 4
chore: update OFFSET pagination snippet #69
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -117,4 +117,82 @@ type Query { | |
} | ||
""" | ||
) | ||
|
||
""" | ||
`customersOffset` pages through twenty three generated `Customer` objects. | ||
The data is supplied by a simulated REST API using offset pagination | ||
but is exposed using standard GraphQL pagination. | ||
|
||
Typically the `endpoint` argument of `@rest` would have query | ||
parameters that set the pagination arguments expected by the | ||
REST API from the field arguments `first` ( ) and `after` (offset defaults to 0 if empty), | ||
for example: `?limt=$first&offset=$after`. | ||
""" | ||
customersOffset(first: Int!, after: String = ""): CustomerConnection | ||
@rest( | ||
# pagination sets the type of pagination the REST API uses | ||
# and for OFFSET requires a setter that declares | ||
# where the total number of records are in the JSON response. | ||
pagination: { | ||
type: OFFSET | ||
setters: [{ field: "total", path: "meta.records" }] | ||
} | ||
|
||
# resultroot indicates where root in the JSON response | ||
# for the values that will populate the nodes. | ||
# Note this does not affect pagination setters, they | ||
# are always relative to the root of the response. | ||
resultroot: "values[]" | ||
|
||
# Ecmascript (with empty endpoint) is used to mimic the response from a REST api. | ||
# Note ECMAScript is only used to generate a mock response with customer objects and page number metadata, | ||
# using @rest against a real endpoint would not typically require any ECMAScript. | ||
endpoint: "stepzen:empty" | ||
ecmascript: """ | ||
function transformREST() { | ||
// A total of 23 items will be returned | ||
const totalItems = 23; | ||
|
||
// Pagination field arguments | ||
// Since this is OFFSET pagination | ||
// "after" is decoded by StepZen from the opaque string value | ||
// and passed into @rest as a integer offset value, | ||
// with the first page being 1. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. with the offset of the first record being zero. |
||
const limit = get('first'); | ||
const offset = get('after'); | ||
|
||
// metadata - total number of records | ||
const records = Math.ceil(totalItems / limit) | ||
|
||
// generate customers for nodes based on the limit and offset values | ||
const startIndex = offset+1 || 1; | ||
const endIndex = Math.min(startIndex + limit, totalItems+1); | ||
var customers = [] | ||
for (let i = startIndex; i < endIndex; i++) { | ||
customers.push({ | ||
id: i, | ||
name: 'name-' + i, | ||
email: 'user-' + i + '@example.com' | ||
}); | ||
} | ||
|
||
// This returns a typical layout of a REST response | ||
// when pagination is through an offset. | ||
// @rest must be configured to match the REST response layout. | ||
// | ||
// pagination setters defines that the page count | ||
// is taken from meta.records | ||
// | ||
// resultroot corresponds to the location that contains the | ||
// data values. Note the REST API returns the customer objects, | ||
// StepZen automatically creates the connection/edges structure | ||
// for the values. | ||
return ({ | ||
meta: { 'records': records }, | ||
values: customers | ||
} | ||
); | ||
} | ||
""" | ||
) | ||
} |
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
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
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.
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.
empty parens?