aboutsummaryrefslogtreecommitdiffstats
path: root/graphql/connections/connections.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2018-07-29 19:37:06 +0200
committerMichael Muré <batolettre@gmail.com>2018-07-29 19:37:32 +0200
commitc351cfd30d59c9179cc940f9ae15c461462e1a50 (patch)
treef2a218d26f350f265200e2ee89a1c94ca26fd770 /graphql/connections/connections.go
parent8fa0b258ac89781dae269790a4bde09cbcd2f324 (diff)
downloadgit-bug-c351cfd30d59c9179cc940f9ae15c461462e1a50.tar.gz
graphql: directly return a connection, cleaning
Diffstat (limited to 'graphql/connections/connections.go')
-rw-r--r--graphql/connections/connections.go39
1 files changed, 39 insertions, 0 deletions
diff --git a/graphql/connections/connections.go b/graphql/connections/connections.go
new file mode 100644
index 00000000..145bfbbe
--- /dev/null
+++ b/graphql/connections/connections.go
@@ -0,0 +1,39 @@
+//go:generate genny -in=connection_template.go -out=gen_bug.go gen "NodeType=bug.Snapshot EdgeType=models.BugEdge 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"
+
+package connections
+
+import (
+ "encoding/base64"
+ "fmt"
+ "strconv"
+ "strings"
+)
+
+const cursorPrefix = "cursor:"
+
+type Edge interface {
+ GetCursor() string
+}
+
+// Creates the cursor string from an offset
+func OffsetToCursor(offset int) string {
+ str := fmt.Sprintf("%v%v", cursorPrefix, offset)
+ return base64.StdEncoding.EncodeToString([]byte(str))
+}
+
+// Re-derives the offset from the cursor string.
+func CursorToOffset(cursor string) (int, error) {
+ str := ""
+ b, err := base64.StdEncoding.DecodeString(cursor)
+ if err == nil {
+ str = string(b)
+ }
+ str = strings.Replace(str, cursorPrefix, "", -1)
+ offset, err := strconv.Atoi(str)
+ if err != nil {
+ return 0, fmt.Errorf("Invalid cursor")
+ }
+ return offset, nil
+}