-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
model.go
198 lines (170 loc) · 4.07 KB
/
model.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package model
import (
"errors"
"fmt"
"io"
"strconv"
"strings"
"time"
"github.com/99designs/gqlgen/_examples/scalars/external"
"github.com/99designs/gqlgen/graphql"
)
type Banned bool
func (b Banned) MarshalGQL(w io.Writer) {
if b {
w.Write([]byte("true"))
} else {
w.Write([]byte("false"))
}
}
func (b *Banned) UnmarshalGQL(v interface{}) error {
switch v := v.(type) {
case string:
*b = strings.ToLower(v) == "true"
return nil
case bool:
*b = Banned(v)
return nil
default:
return fmt.Errorf("%T is not a bool", v)
}
}
type User struct {
ID external.ObjectID
Name string
Created time.Time // direct binding to builtin types with external Marshal/Unmarshal methods
Modified *time.Time // direct binding to builtin types with external Marshal/Unmarshal methods
ValPrefs Prefs // external un/marshal that act on pointers
PtrPrefs *Prefs
IsBanned Banned
Address Address
Tier Tier
}
// Point is serialized as a simple array, eg [1, 2]
type Point struct {
X int
Y int
}
func (p *Point) UnmarshalGQL(v interface{}) error {
pointStr, ok := v.(string)
if !ok {
return fmt.Errorf("points must be strings")
}
parts := strings.Split(pointStr, ",")
if len(parts) != 2 {
return fmt.Errorf("points must have 2 parts")
}
var err error
if p.X, err = strconv.Atoi(parts[0]); err != nil {
return err
}
if p.Y, err = strconv.Atoi(parts[1]); err != nil {
return err
}
return nil
}
// MarshalGQL implements the graphql.Marshaler interface
func (p Point) MarshalGQL(w io.Writer) {
fmt.Fprintf(w, `"%d,%d"`, p.X, p.Y)
}
// if the type referenced in .gqlgen.yml is a function that returns a marshaller we can use it to
// encode and decode
// onto any existing go type.
func MarshalTimestamp(t time.Time) graphql.Marshaler {
return graphql.WriterFunc(func(w io.Writer) {
io.WriteString(w, strconv.FormatInt(t.Unix(), 10))
})
}
// Unmarshal{Typename} is only required if the scalar appears as an input. The raw values have
// already been decoded
// from json into int/float64/bool/nil/map[string]interface/[]interface
func UnmarshalTimestamp(v interface{}) (time.Time, error) {
if tmpStr, ok := v.(int64); ok {
return time.Unix(tmpStr, 0), nil
}
return time.Time{}, errors.New("time should be a unix timestamp")
}
// Lets redefine the base ID type to use an id from an external library
func MarshalID(id external.ObjectID) graphql.Marshaler {
return graphql.WriterFunc(func(w io.Writer) {
io.WriteString(w, strconv.Quote(fmt.Sprintf("=%d=", id)))
})
}
// And the same for the unmarshaler
func UnmarshalID(v interface{}) (external.ObjectID, error) {
str, ok := v.(string)
if !ok {
return 0, fmt.Errorf("ids must be strings")
}
i, err := strconv.Atoi(str[1 : len(str)-1])
return external.ObjectID(i), err
}
type SearchArgs struct {
Location *Point
CreatedAfter *time.Time
IsBanned Banned
}
// A custom enum that uses integers to represent the values in memory but serialize as string for
// graphql
type Tier uint
const (
TierA Tier = iota
TierB Tier = iota
TierC Tier = iota
)
func TierForStr(str string) (Tier, error) {
switch str {
case "A":
return TierA, nil
case "B":
return TierB, nil
case "C":
return TierC, nil
default:
return 0, fmt.Errorf("%s is not a valid Tier", str)
}
}
func (e Tier) IsValid() bool {
switch e {
case TierA, TierB, TierC:
return true
}
return false
}
func (e Tier) String() string {
switch e {
case TierA:
return "A"
case TierB:
return "B"
case TierC:
return "C"
default:
panic("invalid enum value")
}
}
func (e *Tier) UnmarshalGQL(v interface{}) error {
str, ok := v.(string)
if !ok {
return fmt.Errorf("enums must be strings")
}
var err error
*e, err = TierForStr(str)
return err
}
func (e Tier) MarshalGQL(w io.Writer) {
fmt.Fprint(w, strconv.Quote(e.String()))
}
type Prefs struct {
DarkMode bool
}
func MarshalPreferences(p *Prefs) graphql.Marshaler {
return graphql.MarshalBoolean(p.DarkMode)
}
func UnmarshalPreferences(v interface{}) (*Prefs, error) {
tmp, err := graphql.UnmarshalBoolean(v)
if err != nil {
return nil, err
}
return &Prefs{DarkMode: tmp}, nil
}