connection_template.go

  1package connections
  2
  3import (
  4	"fmt"
  5
  6	"github.com/cheekybits/genny/generic"
  7
  8	"github.com/git-bug/git-bug/api/graphql/models"
  9)
 10
 11// Name define the name of the connection
 12type Name generic.Type
 13
 14// NodeType define the node type handled by this relay connection
 15type NodeType generic.Type
 16
 17// EdgeType define the edge type handled by this relay connection
 18type EdgeType generic.Type
 19
 20// ConnectionType define the connection type handled by this relay connection
 21type ConnectionType generic.Type
 22
 23// NameEdgeMaker define a function that take a NodeType and an offset and
 24// create an Edge.
 25type NameEdgeMaker func(value NodeType, offset int) Edge
 26
 27// NameConMaker define a function that create a ConnectionType
 28type NameConMaker func(
 29	edges []*EdgeType,
 30	nodes []NodeType,
 31	info *models.PageInfo,
 32	totalCount int) (*ConnectionType, error)
 33
 34// NameCon will paginate a source according to the input of a relay connection
 35func NameCon(source []NodeType, edgeMaker NameEdgeMaker, conMaker NameConMaker, input models.ConnectionInput) (*ConnectionType, error) {
 36	var nodes []NodeType
 37	var edges []*EdgeType
 38	var cursors []string
 39	var pageInfo = &models.PageInfo{}
 40	var totalCount = len(source)
 41
 42	emptyCon, _ := conMaker(edges, nodes, pageInfo, 0)
 43
 44	offset := 0
 45
 46	if input.After != nil {
 47		for i, value := range source {
 48			edge := edgeMaker(value, i)
 49			if edge.GetCursor() == *input.After {
 50				// remove all previous element including the "after" one
 51				source = source[i+1:]
 52				offset = i + 1
 53				pageInfo.HasPreviousPage = true
 54				break
 55			}
 56		}
 57	}
 58
 59	if input.Before != nil {
 60		for i, value := range source {
 61			edge := edgeMaker(value, i+offset)
 62
 63			if edge.GetCursor() == *input.Before {
 64				// remove all after element including the "before" one
 65				pageInfo.HasNextPage = true
 66				break
 67			}
 68
 69			e := edge.(EdgeType)
 70			edges = append(edges, &e)
 71			cursors = append(cursors, edge.GetCursor())
 72			nodes = append(nodes, value)
 73		}
 74	} else {
 75		edges = make([]*EdgeType, len(source))
 76		cursors = make([]string, len(source))
 77		nodes = source
 78
 79		for i, value := range source {
 80			edge := edgeMaker(value, i+offset)
 81			e := edge.(EdgeType)
 82			edges[i] = &e
 83			cursors[i] = edge.GetCursor()
 84		}
 85	}
 86
 87	if input.First != nil {
 88		if *input.First < 0 {
 89			return emptyCon, fmt.Errorf("first less than zero")
 90		}
 91
 92		if len(edges) > *input.First {
 93			// Slice result to be of length first by removing edges from the end
 94			edges = edges[:*input.First]
 95			cursors = cursors[:*input.First]
 96			nodes = nodes[:*input.First]
 97			pageInfo.HasNextPage = true
 98		}
 99	}
100
101	if input.Last != nil {
102		if *input.Last < 0 {
103			return emptyCon, fmt.Errorf("last less than zero")
104		}
105
106		if len(edges) > *input.Last {
107			// Slice result to be of length last by removing edges from the start
108			edges = edges[len(edges)-*input.Last:]
109			cursors = cursors[len(cursors)-*input.Last:]
110			nodes = nodes[len(nodes)-*input.Last:]
111			pageInfo.HasPreviousPage = true
112		}
113	}
114
115	// Fill up pageInfo cursors
116	if len(cursors) > 0 {
117		pageInfo.StartCursor = cursors[0]
118		pageInfo.EndCursor = cursors[len(cursors)-1]
119	}
120
121	return conMaker(edges, nodes, pageInfo, totalCount)
122}