-
Notifications
You must be signed in to change notification settings - Fork 152
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
Pagination Cursors on Connections (Relay Specification) #282
Merged
Merged
Changes from 21 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
6d80d7b
skip and limit on connections
litewarp c3b3725
add pageinfo and pagecursor definitions
litewarp 079ab96
add totalCount, return connections
litewarp 847a21f
add skip, limit on unions, make all tests pass
litewarp 80ca747
add global node resolution, green on tests
litewarp 655a69c
add copyright header to connection.ts
litewarp f616b4c
minor cleanup
litewarp bbe51c3
cleanup
litewarp 13c130e
remove before/last arguments; move pagination arguments into options
litewarp 89c62a4
use one consistent node interface definition
litewarp 7b2cbff
remove cursor Scalar
litewarp 7427ad8
use isInt on totalCount check in make-augmented-schema connection res…
litewarp 137aa40
remove redundant arraySlice check in createConnectionWithEdge properties
litewarp 8ff4399
remove Node global resolution from this pr
litewarp e9633dc
Merge branch '2.0.0' of github.com:neo4j/graphql into relay-pagination
litewarp 64205d0
remove erroneous yalc stuff, formatting cleanup
litewarp fbc1422
Merge branch '2.0.0' of github.com:neo4j/graphql into relay-pagination
litewarp 5713930
hoist connection args to field level, fix tests from merge, simplify …
litewarp aec58b0
integration test for pagination, fix skipLimitStr
litewarp 2a7bde1
add pagination helper tests, fix off-by-one error in cursor calculation
litewarp 9cb88b3
remove erroneous console
litewarp 5ca9af8
move pagination tests
litewarp 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 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 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 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,86 @@ | ||
/* | ||
* Copyright (c) "Neo4j" | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { getOffsetWithDefault, offsetToCursor } from "graphql-relay/connection/arrayConnection"; | ||
import { Integer, isInt } from "neo4j-driver"; | ||
|
||
/** | ||
* Adapted from graphql-relay-js ConnectionFromArraySlice | ||
*/ | ||
export function createConnectionWithEdgeProperties( | ||
arraySlice: { node: Record<string, any>; [key: string]: any }[], | ||
args: { after?: string; first?: number } = {}, | ||
totalCount: number | ||
) { | ||
const { after, first } = args; | ||
|
||
if ((first as number) < 0) { | ||
throw new Error('Argument "first" must be a non-negative integer'); | ||
} | ||
|
||
// after returns the last cursor in the previous set or -1 if invalid | ||
const lastEdgeCursor = getOffsetWithDefault(after, -1); | ||
|
||
// increment the last cursor position by one for the sliceStart | ||
const sliceStart = lastEdgeCursor + 1; | ||
|
||
const sliceEnd = sliceStart + ((first as number) || arraySlice.length); | ||
|
||
const edges = arraySlice.map((value, index) => { | ||
return { | ||
...value, | ||
cursor: offsetToCursor(sliceStart + index), | ||
}; | ||
}); | ||
|
||
const firstEdge = edges[0]; | ||
const lastEdge = edges[edges.length - 1]; | ||
return { | ||
edges, | ||
pageInfo: { | ||
startCursor: firstEdge.cursor, | ||
endCursor: lastEdge.cursor, | ||
hasPreviousPage: lastEdgeCursor > 0, | ||
hasNextPage: typeof first === "number" ? sliceEnd < totalCount : false, | ||
}, | ||
}; | ||
} | ||
|
||
export function createSkipLimitStr({ skip, limit }: { skip?: number | Integer; limit?: number | Integer }): string { | ||
const hasSkip = typeof skip !== "undefined" && skip !== 0; | ||
const hasLimit = typeof limit !== "undefined" && limit !== 0; | ||
let skipLimitStr = ""; | ||
|
||
if (hasSkip && !hasLimit) { | ||
skipLimitStr = `[${skip}..]`; | ||
} | ||
|
||
if (hasLimit && !hasSkip) { | ||
skipLimitStr = `[..${limit}]`; | ||
} | ||
|
||
if (hasLimit && hasSkip) { | ||
const sliceStart = isInt(skip as Integer) ? (skip as Integer).toNumber() : skip; | ||
const itemsToGrab = isInt(limit as Integer) ? (limit as Integer).toNumber() : limit; | ||
const sliceEnd = (sliceStart as number) + (itemsToGrab as number); | ||
skipLimitStr = `[${skip}..${sliceEnd}]`; | ||
} | ||
|
||
return skipLimitStr; | ||
} | ||
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
Oops, something went wrong.
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.
This should probably be in the
translate
directory, but we can easily shuffle things around when merged, so not a blocker.