diff options
Diffstat (limited to 'graphql/connections')
-rw-r--r-- | graphql/connections/connection_template.go | 3 | ||||
-rw-r--r-- | graphql/connections/connections.go | 5 | ||||
-rw-r--r-- | graphql/connections/gen_identity.go | 27 | ||||
-rw-r--r-- | graphql/connections/gen_lazy_bug.go (renamed from graphql/connections/gen_bug.go) | 0 | ||||
-rw-r--r-- | graphql/connections/gen_lazy_identity.go | 110 |
5 files changed, 129 insertions, 16 deletions
diff --git a/graphql/connections/connection_template.go b/graphql/connections/connection_template.go index e98f53eb..412eb318 100644 --- a/graphql/connections/connection_template.go +++ b/graphql/connections/connection_template.go @@ -3,8 +3,9 @@ package connections import ( "fmt" - "github.com/MichaelMure/git-bug/graphql/models" "github.com/cheekybits/genny/generic" + + "github.com/MichaelMure/git-bug/graphql/models" ) // Name define the name of the connection diff --git a/graphql/connections/connections.go b/graphql/connections/connections.go index cdc6d07f..d54a5068 100644 --- a/graphql/connections/connections.go +++ b/graphql/connections/connections.go @@ -1,5 +1,6 @@ -//go:generate genny -in=connection_template.go -out=gen_bug.go gen "Name=LazyBug NodeType=string EdgeType=LazyBugEdge ConnectionType=models.BugConnection" -//go:generate genny -in=connection_template.go -out=gen_identity.go gen "Name=LazyIdentity NodeType=string EdgeType=LazyIdentityEdge ConnectionType=models.IdentityConnection" +//go:generate genny -in=connection_template.go -out=gen_lazy_bug.go gen "Name=LazyBug NodeType=string EdgeType=LazyBugEdge ConnectionType=models.BugConnection" +//go:generate genny -in=connection_template.go -out=gen_lazy_identity.go gen "Name=LazyIdentity NodeType=string EdgeType=LazyIdentityEdge ConnectionType=models.IdentityConnection" +//go:generate genny -in=connection_template.go -out=gen_identity.go gen "Name=Identity NodeType=identity.Interface EdgeType=models.IdentityEdge ConnectionType=models.IdentityConnection" //go:generate genny -in=connection_template.go -out=gen_operation.go gen "Name=Operation NodeType=bug.Operation EdgeType=models.OperationEdge ConnectionType=models.OperationConnection" //go:generate genny -in=connection_template.go -out=gen_comment.go gen "Name=Comment NodeType=bug.Comment EdgeType=models.CommentEdge ConnectionType=models.CommentConnection" //go:generate genny -in=connection_template.go -out=gen_timeline.go gen "Name=TimelineItem NodeType=bug.TimelineItem EdgeType=models.TimelineItemEdge ConnectionType=models.TimelineItemConnection" diff --git a/graphql/connections/gen_identity.go b/graphql/connections/gen_identity.go index 28501171..2ba2f98f 100644 --- a/graphql/connections/gen_identity.go +++ b/graphql/connections/gen_identity.go @@ -8,23 +8,24 @@ import ( "fmt" "github.com/MichaelMure/git-bug/graphql/models" + "github.com/MichaelMure/git-bug/identity" ) -// StringEdgeMaker define a function that take a string and an offset and +// IdentityInterfaceEdgeMaker define a function that take a identity.Interface and an offset and // create an Edge. -type LazyIdentityEdgeMaker func(value string, offset int) Edge +type IdentityEdgeMaker func(value identity.Interface, offset int) Edge -// LazyIdentityConMaker define a function that create a models.IdentityConnection -type LazyIdentityConMaker func( - edges []LazyIdentityEdge, - nodes []string, +// IdentityConMaker define a function that create a models.IdentityConnection +type IdentityConMaker func( + edges []models.IdentityEdge, + nodes []identity.Interface, info models.PageInfo, totalCount int) (models.IdentityConnection, error) -// LazyIdentityCon will paginate a source according to the input of a relay connection -func LazyIdentityCon(source []string, edgeMaker LazyIdentityEdgeMaker, conMaker LazyIdentityConMaker, input models.ConnectionInput) (models.IdentityConnection, error) { - var nodes []string - var edges []LazyIdentityEdge +// IdentityCon will paginate a source according to the input of a relay connection +func IdentityCon(source []identity.Interface, edgeMaker IdentityEdgeMaker, conMaker IdentityConMaker, input models.ConnectionInput) (models.IdentityConnection, error) { + var nodes []identity.Interface + var edges []models.IdentityEdge var cursors []string var pageInfo models.PageInfo var totalCount = len(source) @@ -56,18 +57,18 @@ func LazyIdentityCon(source []string, edgeMaker LazyIdentityEdgeMaker, conMaker break } - edges = append(edges, edge.(LazyIdentityEdge)) + edges = append(edges, edge.(models.IdentityEdge)) cursors = append(cursors, edge.GetCursor()) nodes = append(nodes, value) } } else { - edges = make([]LazyIdentityEdge, len(source)) + edges = make([]models.IdentityEdge, len(source)) cursors = make([]string, len(source)) nodes = source for i, value := range source { edge := edgeMaker(value, i+offset) - edges[i] = edge.(LazyIdentityEdge) + edges[i] = edge.(models.IdentityEdge) cursors[i] = edge.GetCursor() } } diff --git a/graphql/connections/gen_bug.go b/graphql/connections/gen_lazy_bug.go index ba0a65fa..ba0a65fa 100644 --- a/graphql/connections/gen_bug.go +++ b/graphql/connections/gen_lazy_bug.go diff --git a/graphql/connections/gen_lazy_identity.go b/graphql/connections/gen_lazy_identity.go new file mode 100644 index 00000000..28501171 --- /dev/null +++ b/graphql/connections/gen_lazy_identity.go @@ -0,0 +1,110 @@ +// This file was automatically generated by genny. +// Any changes will be lost if this file is regenerated. +// see https://github.com/cheekybits/genny + +package connections + +import ( + "fmt" + + "github.com/MichaelMure/git-bug/graphql/models" +) + +// StringEdgeMaker define a function that take a string and an offset and +// create an Edge. +type LazyIdentityEdgeMaker func(value string, offset int) Edge + +// LazyIdentityConMaker define a function that create a models.IdentityConnection +type LazyIdentityConMaker func( + edges []LazyIdentityEdge, + nodes []string, + info models.PageInfo, + totalCount int) (models.IdentityConnection, error) + +// LazyIdentityCon will paginate a source according to the input of a relay connection +func LazyIdentityCon(source []string, edgeMaker LazyIdentityEdgeMaker, conMaker LazyIdentityConMaker, input models.ConnectionInput) (models.IdentityConnection, error) { + var nodes []string + var edges []LazyIdentityEdge + var cursors []string + var pageInfo models.PageInfo + var totalCount = len(source) + + emptyCon, _ := conMaker(edges, nodes, pageInfo, 0) + + offset := 0 + + if input.After != nil { + for i, value := range source { + edge := edgeMaker(value, i) + if edge.GetCursor() == *input.After { + // remove all previous element including the "after" one + source = source[i+1:] + offset = i + 1 + pageInfo.HasPreviousPage = true + break + } + } + } + + if input.Before != nil { + for i, value := range source { + edge := edgeMaker(value, i+offset) + + if edge.GetCursor() == *input.Before { + // remove all after element including the "before" one + pageInfo.HasNextPage = true + break + } + + edges = append(edges, edge.(LazyIdentityEdge)) + cursors = append(cursors, edge.GetCursor()) + nodes = append(nodes, value) + } + } else { + edges = make([]LazyIdentityEdge, len(source)) + cursors = make([]string, len(source)) + nodes = source + + for i, value := range source { + edge := edgeMaker(value, i+offset) + edges[i] = edge.(LazyIdentityEdge) + cursors[i] = edge.GetCursor() + } + } + + if input.First != nil { + if *input.First < 0 { + return emptyCon, fmt.Errorf("first less than zero") + } + + 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 + } + } + + if input.Last != nil { + if *input.Last < 0 { + return emptyCon, fmt.Errorf("last less than zero") + } + + 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, totalCount) +} |