-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolver_root.go
73 lines (64 loc) · 1.56 KB
/
resolver_root.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
package main
import (
"context"
"errors"
graphql "github.com/graph-gophers/graphql-go"
)
const PostgresTimestamptzFormat = "2006-01-02 15:04:05.000000Z"
var (
ErrUserMustBeUnauth = errors.New("user must be unauthenticated")
ErrUserMustBeAuth = errors.New("user must be authenticated")
)
var RootRx = RootResolver{}
type RootResolver struct{}
func (r *RootResolver) Ping(ctx context.Context) string {
return "pong!"
}
// NOTE: Unprotected
func (r *RootResolver) User(ctx context.Context, args struct{ UserID graphql.ID }) (*UserResolver, error) {
// userID, ok := ctx.Value(UserIDKey).(string)
// if !ok {
// return nil, ErrUserMustBeAuth
// }
user := &User{}
err := db.QueryRow(`
select
user_id,
-- created_at,
-- updated_at,
-- email,
-- email_verified,
-- auth_provider,
photo_url,
display_name,
username
from users
where user_id = $1
`, args.UserID).Scan(&user.UserID, &user.PhotoURL, &user.DisplayName, &user.Username)
if err != nil {
return nil, err
}
return &UserResolver{user}, nil
}
// NOTE: Unprotected
func (r *RootResolver) Note(ctx context.Context, args struct{ NoteID graphql.ID }) (*NoteResolver, error) {
// userID, ok := ctx.Value(UserIDKey).(string)
// if !ok {
// return nil, ErrUserMustBeAuth
// }
note := &Note{}
err := db.QueryRow(`
select
user_id,
note_id,
created_at,
updated_at,
data
from notes
where note_id = $1
`, args.NoteID).Scan(¬e.UserID, ¬e.NoteID, ¬e.CreatedAt, ¬e.UpdatedAt, ¬e.Data)
if err != nil {
return nil, err
}
return &NoteResolver{note}, nil
}