1package graphql
2
3import (
4 "encoding/base64"
5 "strings"
6)
7
8type ResolvedGlobalID struct {
9 Type string `json:"type"`
10 ID string `json:"id"`
11}
12
13// Takes a type name and an ID specific to that type name, and returns a
14// "global ID" that is unique among all types.
15func ToGlobalID(ttype string, id string) string {
16 str := ttype + ":" + id
17 encStr := base64.StdEncoding.EncodeToString([]byte(str))
18 return encStr
19}
20
21// Takes the "global ID" created by toGlobalID, and returns the type name and ID
22// used to create it.
23func FromGlobalID(globalID string) *ResolvedGlobalID {
24 strID := ""
25 b, err := base64.StdEncoding.DecodeString(globalID)
26 if err == nil {
27 strID = string(b)
28 }
29 tokens := strings.Split(strID, ":")
30 if len(tokens) < 2 {
31 return nil
32 }
33 return &ResolvedGlobalID{
34 Type: tokens[0],
35 ID: tokens[1],
36 }
37}