aboutsummaryrefslogtreecommitdiffstats
path: root/graphql/relay.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2018-07-29 18:58:42 +0200
committerMichael Muré <batolettre@gmail.com>2018-07-29 18:58:42 +0200
commit8fa0b258ac89781dae269790a4bde09cbcd2f324 (patch)
treeb9bcf0826f5739f128de52123447cede23291c02 /graphql/relay.go
parent6363518c85cbd8247a5f6507b8a1dd3903cfb71d (diff)
downloadgit-bug-8fa0b258ac89781dae269790a4bde09cbcd2f324.tar.gz
cleaning
Diffstat (limited to 'graphql/relay.go')
-rw-r--r--graphql/relay.go39
1 files changed, 39 insertions, 0 deletions
diff --git a/graphql/relay.go b/graphql/relay.go
new file mode 100644
index 00000000..a73a38ef
--- /dev/null
+++ b/graphql/relay.go
@@ -0,0 +1,39 @@
+package graphql
+
+import (
+ "encoding/base64"
+ "strings"
+)
+
+
+type ResolvedGlobalID struct {
+ Type string `json:"type"`
+ ID string `json:"id"`
+}
+
+// Takes a type name and an ID specific to that type name, and returns a
+// "global ID" that is unique among all types.
+func ToGlobalID(ttype string, id string) string {
+ str := ttype + ":" + id
+ encStr := base64.StdEncoding.EncodeToString([]byte(str))
+ return encStr
+}
+
+// Takes the "global ID" created by toGlobalID, and returns the type name and ID
+// used to create it.
+func FromGlobalID(globalID string) *ResolvedGlobalID {
+ strID := ""
+ b, err := base64.StdEncoding.DecodeString(globalID)
+ if err == nil {
+ strID = string(b)
+ }
+ tokens := strings.Split(strID, ":")
+ if len(tokens) < 2 {
+ return nil
+ }
+ return &ResolvedGlobalID{
+ Type: tokens[0],
+ ID: tokens[1],
+ }
+}
+