diff options
author | Michael Muré <michael.mure@consensys.net> | 2019-03-31 21:44:14 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2019-04-01 14:03:13 +0200 |
commit | 15c258cdc4ba37820362a44dfc2636ed1ff92b4c (patch) | |
tree | 0790e36bf4846b12b367f12670540eec6ee023f3 /graphql/connections | |
parent | e028b895aa21553937d732044b7bb0a15c5276f6 (diff) | |
download | git-bug-15c258cdc4ba37820362a44dfc2636ed1ff92b4c.tar.gz |
graphql: expose allIdentities, identities and userIdentity in the repo
Diffstat (limited to 'graphql/connections')
-rw-r--r-- | graphql/connections/connection_template.go | 13 | ||||
-rw-r--r-- | graphql/connections/connections.go | 9 | ||||
-rw-r--r-- | graphql/connections/gen_bug.go | 10 | ||||
-rw-r--r-- | graphql/connections/gen_comment.go | 10 | ||||
-rw-r--r-- | graphql/connections/gen_identity.go | 110 | ||||
-rw-r--r-- | graphql/connections/gen_operation.go | 10 | ||||
-rw-r--r-- | graphql/connections/gen_timeline.go | 10 | ||||
-rw-r--r-- | graphql/connections/lazy_identity.go | 12 |
8 files changed, 155 insertions, 29 deletions
diff --git a/graphql/connections/connection_template.go b/graphql/connections/connection_template.go index e20375b8..e98f53eb 100644 --- a/graphql/connections/connection_template.go +++ b/graphql/connections/connection_template.go @@ -7,6 +7,9 @@ import ( "github.com/cheekybits/genny/generic" ) +// Name define the name of the connection +type Name generic.Type + // NodeType define the node type handled by this relay connection type NodeType generic.Type @@ -18,17 +21,17 @@ type ConnectionType generic.Type // NodeTypeEdgeMaker define a function that take a NodeType and an offset and // create an Edge. -type NodeTypeEdgeMaker func(value NodeType, offset int) Edge +type NameEdgeMaker func(value NodeType, offset int) Edge -// NodeTypeConMaker define a function that create a ConnectionType -type NodeTypeConMaker func( +// NameConMaker define a function that create a ConnectionType +type NameConMaker func( edges []EdgeType, nodes []NodeType, info models.PageInfo, totalCount int) (ConnectionType, error) -// NodeTypeCon will paginate a source according to the input of a relay connection -func NodeTypeCon(source []NodeType, edgeMaker NodeTypeEdgeMaker, conMaker NodeTypeConMaker, input models.ConnectionInput) (ConnectionType, error) { +// NameCon will paginate a source according to the input of a relay connection +func NameCon(source []NodeType, edgeMaker NameEdgeMaker, conMaker NameConMaker, input models.ConnectionInput) (ConnectionType, error) { var nodes []NodeType var edges []EdgeType var cursors []string diff --git a/graphql/connections/connections.go b/graphql/connections/connections.go index 68608116..cdc6d07f 100644 --- a/graphql/connections/connections.go +++ b/graphql/connections/connections.go @@ -1,7 +1,8 @@ -//go:generate genny -in=connection_template.go -out=gen_bug.go gen "NodeType=string EdgeType=LazyBugEdge ConnectionType=models.BugConnection" -//go:generate genny -in=connection_template.go -out=gen_operation.go gen "NodeType=bug.Operation EdgeType=models.OperationEdge ConnectionType=models.OperationConnection" -//go:generate genny -in=connection_template.go -out=gen_comment.go gen "NodeType=bug.Comment EdgeType=models.CommentEdge ConnectionType=models.CommentConnection" -//go:generate genny -in=connection_template.go -out=gen_timeline.go gen "NodeType=bug.TimelineItem EdgeType=models.TimelineItemEdge ConnectionType=models.TimelineItemConnection" +//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_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" // Package connections implement a generic GraphQL relay connection package connections diff --git a/graphql/connections/gen_bug.go b/graphql/connections/gen_bug.go index 000ff109..ba0a65fa 100644 --- a/graphql/connections/gen_bug.go +++ b/graphql/connections/gen_bug.go @@ -12,17 +12,17 @@ import ( // StringEdgeMaker define a function that take a string and an offset and // create an Edge. -type StringEdgeMaker func(value string, offset int) Edge +type LazyBugEdgeMaker func(value string, offset int) Edge -// StringConMaker define a function that create a models.BugConnection -type StringConMaker func( +// LazyBugConMaker define a function that create a models.BugConnection +type LazyBugConMaker func( edges []LazyBugEdge, nodes []string, info models.PageInfo, totalCount int) (models.BugConnection, error) -// StringCon will paginate a source according to the input of a relay connection -func StringCon(source []string, edgeMaker StringEdgeMaker, conMaker StringConMaker, input models.ConnectionInput) (models.BugConnection, error) { +// LazyBugCon will paginate a source according to the input of a relay connection +func LazyBugCon(source []string, edgeMaker LazyBugEdgeMaker, conMaker LazyBugConMaker, input models.ConnectionInput) (models.BugConnection, error) { var nodes []string var edges []LazyBugEdge var cursors []string diff --git a/graphql/connections/gen_comment.go b/graphql/connections/gen_comment.go index 5c8b0eea..6df21c58 100644 --- a/graphql/connections/gen_comment.go +++ b/graphql/connections/gen_comment.go @@ -13,17 +13,17 @@ import ( // BugCommentEdgeMaker define a function that take a bug.Comment and an offset and // create an Edge. -type BugCommentEdgeMaker func(value bug.Comment, offset int) Edge +type CommentEdgeMaker func(value bug.Comment, offset int) Edge -// BugCommentConMaker define a function that create a models.CommentConnection -type BugCommentConMaker func( +// CommentConMaker define a function that create a models.CommentConnection +type CommentConMaker func( edges []models.CommentEdge, nodes []bug.Comment, info models.PageInfo, totalCount int) (models.CommentConnection, error) -// BugCommentCon will paginate a source according to the input of a relay connection -func BugCommentCon(source []bug.Comment, edgeMaker BugCommentEdgeMaker, conMaker BugCommentConMaker, input models.ConnectionInput) (models.CommentConnection, error) { +// CommentCon will paginate a source according to the input of a relay connection +func CommentCon(source []bug.Comment, edgeMaker CommentEdgeMaker, conMaker CommentConMaker, input models.ConnectionInput) (models.CommentConnection, error) { var nodes []bug.Comment var edges []models.CommentEdge var cursors []string diff --git a/graphql/connections/gen_identity.go b/graphql/connections/gen_identity.go new file mode 100644 index 00000000..28501171 --- /dev/null +++ b/graphql/connections/gen_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) +} diff --git a/graphql/connections/gen_operation.go b/graphql/connections/gen_operation.go index 9a1a53b7..3fbd724f 100644 --- a/graphql/connections/gen_operation.go +++ b/graphql/connections/gen_operation.go @@ -13,17 +13,17 @@ import ( // BugOperationEdgeMaker define a function that take a bug.Operation and an offset and // create an Edge. -type BugOperationEdgeMaker func(value bug.Operation, offset int) Edge +type OperationEdgeMaker func(value bug.Operation, offset int) Edge -// BugOperationConMaker define a function that create a models.OperationConnection -type BugOperationConMaker func( +// OperationConMaker define a function that create a models.OperationConnection +type OperationConMaker func( edges []models.OperationEdge, nodes []bug.Operation, info models.PageInfo, totalCount int) (models.OperationConnection, error) -// BugOperationCon will paginate a source according to the input of a relay connection -func BugOperationCon(source []bug.Operation, edgeMaker BugOperationEdgeMaker, conMaker BugOperationConMaker, input models.ConnectionInput) (models.OperationConnection, error) { +// OperationCon will paginate a source according to the input of a relay connection +func OperationCon(source []bug.Operation, edgeMaker OperationEdgeMaker, conMaker OperationConMaker, input models.ConnectionInput) (models.OperationConnection, error) { var nodes []bug.Operation var edges []models.OperationEdge var cursors []string diff --git a/graphql/connections/gen_timeline.go b/graphql/connections/gen_timeline.go index 4d417a45..1a4b2fe5 100644 --- a/graphql/connections/gen_timeline.go +++ b/graphql/connections/gen_timeline.go @@ -13,17 +13,17 @@ import ( // BugTimelineItemEdgeMaker define a function that take a bug.TimelineItem and an offset and // create an Edge. -type BugTimelineItemEdgeMaker func(value bug.TimelineItem, offset int) Edge +type TimelineItemEdgeMaker func(value bug.TimelineItem, offset int) Edge -// BugTimelineItemConMaker define a function that create a models.TimelineItemConnection -type BugTimelineItemConMaker func( +// TimelineItemConMaker define a function that create a models.TimelineItemConnection +type TimelineItemConMaker func( edges []models.TimelineItemEdge, nodes []bug.TimelineItem, info models.PageInfo, totalCount int) (models.TimelineItemConnection, error) -// BugTimelineItemCon will paginate a source according to the input of a relay connection -func BugTimelineItemCon(source []bug.TimelineItem, edgeMaker BugTimelineItemEdgeMaker, conMaker BugTimelineItemConMaker, input models.ConnectionInput) (models.TimelineItemConnection, error) { +// TimelineItemCon will paginate a source according to the input of a relay connection +func TimelineItemCon(source []bug.TimelineItem, edgeMaker TimelineItemEdgeMaker, conMaker TimelineItemConMaker, input models.ConnectionInput) (models.TimelineItemConnection, error) { var nodes []bug.TimelineItem var edges []models.TimelineItemEdge var cursors []string diff --git a/graphql/connections/lazy_identity.go b/graphql/connections/lazy_identity.go new file mode 100644 index 00000000..34ba579a --- /dev/null +++ b/graphql/connections/lazy_identity.go @@ -0,0 +1,12 @@ +package connections + +// LazyIdentityEdge is a special relay edge used to implement a lazy loading connection +type LazyIdentityEdge struct { + Id string + Cursor string +} + +// GetCursor return the cursor of a LazyIdentityEdge +func (lbe LazyIdentityEdge) GetCursor() string { + return lbe.Cursor +} |