github.com/animeshon/gqlgen@v0.13.1-0.20210304133704-3a770431bb6d/example/starwars/models/model.go (about) 1 package models 2 3 import ( 4 "encoding/base64" 5 "fmt" 6 "time" 7 ) 8 9 type CharacterFields struct { 10 ID string 11 Name string 12 FriendIds []string 13 AppearsIn []Episode 14 } 15 16 type Human struct { 17 CharacterFields 18 StarshipIds []string 19 HeightMeters float64 20 Mass float64 21 } 22 23 func (h *Human) Height(unit LengthUnit) float64 { 24 switch unit { 25 case "METER", "": 26 return h.HeightMeters 27 case "FOOT": 28 return h.HeightMeters * 3.28084 29 default: 30 panic("invalid unit") 31 } 32 } 33 34 func (Human) IsCharacter() {} 35 func (Human) IsSearchResult() {} 36 37 type Review struct { 38 Stars int 39 Commentary *string 40 Time time.Time 41 } 42 43 type Droid struct { 44 CharacterFields 45 PrimaryFunction string 46 } 47 48 func (Droid) IsCharacter() {} 49 func (Droid) IsSearchResult() {} 50 51 type FriendsConnection struct { 52 Ids []string 53 From int 54 To int 55 } 56 57 func (f *FriendsConnection) TotalCount() int { 58 return len(f.Ids) 59 } 60 61 func (f *FriendsConnection) PageInfo() PageInfo { 62 return PageInfo{ 63 StartCursor: EncodeCursor(f.From), 64 EndCursor: EncodeCursor(f.To - 1), 65 HasNextPage: f.To < len(f.Ids), 66 } 67 } 68 69 func EncodeCursor(i int) string { 70 return base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("cursor%d", i+1))) 71 }