-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathroot-resolver.go
145 lines (117 loc) · 2.97 KB
/
root-resolver.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
package main
import (
"context"
"encoding/base64"
"fmt"
"strconv"
"strings"
graphql "github.com/graph-gophers/graphql-go"
"github.com/pkg/errors"
)
// Resolver is the root resolver
type Resolver struct {
db *DB
}
// GetUser resolves the getUser query
func (r *Resolver) GetUser(ctx context.Context, args struct{ ID graphql.ID }) (*UserResolver, error) {
id, err := gqlIDToUint(args.ID)
if err != nil {
return nil, errors.Wrap(err, "GetPet")
}
user, err := r.db.getUser(ctx, id)
if err != nil {
return nil, errors.Wrap(err, "GetUser")
}
s := UserResolver{
db: r.db,
m: *user,
}
return &s, nil
}
// GetPet resolves the getPet query
func (r *Resolver) GetPet(ctx context.Context, args struct{ ID graphql.ID }) (*PetResolver, error) {
id, err := gqlIDToUint(args.ID)
if err != nil {
return nil, errors.Wrap(err, "GetPet")
}
pet, err := r.db.getPet(ctx, id)
if err != nil {
return nil, err
}
s := PetResolver{
db: r.db,
m: *pet,
}
return &s, nil
}
// GetTag resolves the getTag query
func (r *Resolver) GetTag(ctx context.Context, args struct{ Title string }) (*TagResolver, error) {
tag, err := r.db.getTagBytTitle(ctx, args.Title)
if err != nil {
return nil, errors.Wrap(err, "GetTag")
}
s := TagResolver{
db: r.db,
m: *tag,
}
return &s, nil
}
// petInput has everything needed to do adds and updates on a pet
type petInput struct {
ID *graphql.ID
OwnerID int32
Name string
TagIDs *[]*int32
}
// AddPet Resolves the addPet mutation
func (r *Resolver) AddPet(ctx context.Context, args struct{ Pet petInput }) (*PetResolver, error) {
pet, err := r.db.addPet(ctx, args.Pet)
if err != nil {
return nil, errors.Wrap(err, "AddPet")
}
s := PetResolver{
db: r.db,
m: *pet,
}
return &s, nil
}
// UpdatePet takes care of updating any field on the pet
func (r *Resolver) UpdatePet(ctx context.Context, args struct{ Pet petInput }) (*PetResolver, error) {
pet, err := r.db.updatePet(ctx, &args.Pet)
if err != nil {
return nil, errors.Wrap(err, "UpdatePet")
}
s := PetResolver{
db: r.db,
m: *pet,
}
return &s, nil
}
// DeletePet takes care of deleting a pet record
func (r *Resolver) DeletePet(ctx context.Context, args struct{ UserID, PetID graphql.ID }) (*bool, error) {
petID, err := gqlIDToUint(args.PetID)
if err != nil {
return nil, errors.Wrap(err, "DeletePet")
}
userID, err := gqlIDToUint(args.UserID)
if err != nil {
return nil, errors.Wrap(err, "DeletePet")
}
return r.db.deletePet(ctx, userID, petID)
}
// encode cursor encodes the cursot position in base64
func encodeCursor(i int) graphql.ID {
return graphql.ID(base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("cursor%d", i))))
}
// decode cursor decodes the base 64 encoded cursor and resturns the integer
func decodeCursor(s string) (int, error) {
b, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return 0, err
}
i, err := strconv.Atoi(strings.TrimPrefix(string(b), "cursor"))
if err != nil {
return 0, err
}
return i, nil
}