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