aboutsummaryrefslogtreecommitdiffstats
path: root/graphql2/relay.go
diff options
context:
space:
mode:
Diffstat (limited to 'graphql2/relay.go')
-rw-r--r--graphql2/relay.go39
1 files changed, 39 insertions, 0 deletions
diff --git a/graphql2/relay.go b/graphql2/relay.go
new file mode 100644
index 00000000..b037f28b
--- /dev/null
+++ b/graphql2/relay.go
@@ -0,0 +1,39 @@
+package graphql2
+
+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],
+ }
+}
+