aboutsummaryrefslogtreecommitdiffstats
path: root/graphql/connections/connection_template.go
diff options
context:
space:
mode:
Diffstat (limited to 'graphql/connections/connection_template.go')
-rw-r--r--graphql/connections/connection_template.go25
1 files changed, 19 insertions, 6 deletions
diff --git a/graphql/connections/connection_template.go b/graphql/connections/connection_template.go
index 511f48d3..523141cd 100644
--- a/graphql/connections/connection_template.go
+++ b/graphql/connections/connection_template.go
@@ -16,9 +16,9 @@ type EdgeType generic.Type
// ConnectionType define the connection type handled by this relay connection
type ConnectionType generic.Type
-// NodeTypeEdger define a function that take a NodeType and an offset and
+// NodeTypeEdgeMaker define a function that take a NodeType and an offset and
// create an Edge.
-type NodeTypeEdger func(value NodeType, offset int) Edge
+type NodeTypeEdgeMaker func(value NodeType, offset int) Edge
// NodeTypeConMaker define a function that create a ConnectionType
type NodeTypeConMaker func(
@@ -28,9 +28,10 @@ type NodeTypeConMaker func(
totalCount int) (ConnectionType, error)
// NodeTypeCon will paginate a source according to the input of a relay connection
-func NodeTypeCon(source []NodeType, edger NodeTypeEdger, conMaker NodeTypeConMaker, input models.ConnectionInput) (ConnectionType, error) {
+func NodeTypeCon(source []NodeType, edgeMaker NodeTypeEdgeMaker, conMaker NodeTypeConMaker, input models.ConnectionInput) (ConnectionType, error) {
var nodes []NodeType
var edges []EdgeType
+ var cursors []string
var pageInfo models.PageInfo
emptyCon, _ := conMaker(edges, nodes, pageInfo, 0)
@@ -39,7 +40,7 @@ func NodeTypeCon(source []NodeType, edger NodeTypeEdger, conMaker NodeTypeConMak
if input.After != nil {
for i, value := range source {
- edge := edger(value, i)
+ edge := edgeMaker(value, i)
if edge.GetCursor() == *input.After {
// remove all previous element including the "after" one
source = source[i+1:]
@@ -51,7 +52,7 @@ func NodeTypeCon(source []NodeType, edger NodeTypeEdger, conMaker NodeTypeConMak
if input.Before != nil {
for i, value := range source {
- edge := edger(value, i+offset)
+ edge := edgeMaker(value, i+offset)
if edge.GetCursor() == *input.Before {
// remove all after element including the "before" one
@@ -59,14 +60,18 @@ func NodeTypeCon(source []NodeType, edger NodeTypeEdger, conMaker NodeTypeConMak
}
edges = append(edges, edge.(EdgeType))
+ cursors = append(cursors, edge.GetCursor())
nodes = append(nodes, value)
}
} else {
edges = make([]EdgeType, len(source))
+ cursors = make([]string, len(source))
nodes = source
for i, value := range source {
- edges[i] = edger(value, i+offset).(EdgeType)
+ edge := edgeMaker(value, i+offset)
+ edges[i] = edge.(EdgeType)
+ cursors[i] = edge.GetCursor()
}
}
@@ -78,6 +83,7 @@ func NodeTypeCon(source []NodeType, edger NodeTypeEdger, conMaker NodeTypeConMak
if len(edges) > *input.First {
// Slice result to be of length first by removing edges from the end
edges = edges[:*input.First]
+ cursors = cursors[:*input.First]
nodes = nodes[:*input.First]
pageInfo.HasNextPage = true
}
@@ -91,10 +97,17 @@ func NodeTypeCon(source []NodeType, edger NodeTypeEdger, conMaker NodeTypeConMak
if len(edges) > *input.Last {
// Slice result to be of length last by removing edges from the start
edges = edges[len(edges)-*input.Last:]
+ cursors = cursors[len(cursors)-*input.Last:]
nodes = nodes[len(nodes)-*input.Last:]
pageInfo.HasPreviousPage = true
}
}
+ // Fill up pageInfo cursors
+ if len(cursors) > 0 {
+ pageInfo.StartCursor = cursors[0]
+ pageInfo.EndCursor = cursors[len(cursors)-1]
+ }
+
return conMaker(edges, nodes, pageInfo, len(source))
}