Skip to content

chore: fix offset example and the test #76

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions rest/pagination/api.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,13 @@ type Query {
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,
# Note ECMAScript is only used to generate a mock response with customer objects and offset 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;
const records = 23;

// Pagination field arguments
// Since this is OFFSET pagination
Expand All @@ -161,12 +161,9 @@ type Query {
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);
const endIndex = Math.min(startIndex + limit, records+1);
var customers = []
for (let i = startIndex; i < endIndex; i++) {
customers.push({
Expand All @@ -180,7 +177,7 @@ type Query {
// when pagination is through an offset.
// @rest must be configured to match the REST response layout.
//
// pagination setters defines that the page count
// pagination setters defines that total number of records
// is taken from meta.records
//
// resultroot corresponds to the location that contains the
Expand Down
2 changes: 1 addition & 1 deletion rest/pagination/requests.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ query CustomersPageNumber($first: Int!, $after: String) {
}
}

# Page through customers from a REST API using page numbers
# Page through customers from a REST API using offset
query CustomersOffset($first: Int!, $after: String) {
customersOffset(first: $first, after: $after) {
edges {
Expand Down
4 changes: 2 additions & 2 deletions rest/pagination/tests/Test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ describe(testDescription, function () {
pageInfo: {
endCursor:
"eyJjIjoiTzpRdWVyeTpjdXN0b21lcnNPZmZzZXQiLCJvIjo5fQ==",
hasNextPage: false,
hasNextPage: true,
},
},
},
Expand All @@ -130,7 +130,7 @@ describe(testDescription, function () {
pageInfo: {
endCursor:
"eyJjIjoiTzpRdWVyeTpjdXN0b21lcnNPZmZzZXQiLCJvIjoxOX0=",
hasNextPage: false,
hasNextPage: true,
},
},
},
Expand Down