-
Notifications
You must be signed in to change notification settings - Fork 26
/
user_type.go
121 lines (117 loc) · 3.16 KB
/
user_type.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
package main
import (
"strconv"
"github.com/graphql-go/graphql"
)
var UserType = graphql.NewObject(graphql.ObjectConfig{
Name: "User",
Fields: graphql.Fields{
"id": &graphql.Field{
Type: graphql.NewNonNull(graphql.ID),
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if user, ok := p.Source.(*User); ok == true {
return user.ID, nil
}
return nil, nil
},
},
"email": &graphql.Field{
Type: graphql.NewNonNull(graphql.String),
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if user, ok := p.Source.(*User); ok == true {
return user.Email, nil
}
return nil, nil
},
},
},
})
func init() {
UserType.AddFieldConfig("post", &graphql.Field{
Type: PostType,
Args: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{
Description: "Post ID",
Type: graphql.NewNonNull(graphql.ID),
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if user, ok := p.Source.(*User); ok == true {
i := p.Args["id"].(string)
id, err := strconv.Atoi(i)
if err != nil {
return nil, err
}
return GetPostByIDAndUser(id, user.ID)
}
return nil, nil
},
})
UserType.AddFieldConfig("posts", &graphql.Field{
Type: graphql.NewNonNull(graphql.NewList(graphql.NewNonNull(PostType))),
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if user, ok := p.Source.(*User); ok == true {
return GetPostsForUser(user.ID)
}
return []Post{}, nil
},
})
UserType.AddFieldConfig("follower", &graphql.Field{
Type: UserType,
Args: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{
Description: "Follower ID",
Type: graphql.NewNonNull(graphql.ID),
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if user, ok := p.Source.(*User); ok == true {
i := p.Args["id"].(string)
id, err := strconv.Atoi(i)
if err != nil {
return nil, err
}
return GetFollowerByIDAndUser(id, user.ID)
}
return nil, nil
},
})
UserType.AddFieldConfig("followers", &graphql.Field{
Type: graphql.NewNonNull(graphql.NewList(graphql.NewNonNull(UserType))),
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if user, ok := p.Source.(*User); ok == true {
return GetFollowersForUser(user.ID)
}
return []User{}, nil
},
})
UserType.AddFieldConfig("followee", &graphql.Field{
Type: UserType,
Args: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{
Description: "Followee ID",
Type: graphql.NewNonNull(graphql.ID),
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if user, ok := p.Source.(*User); ok == true {
i := p.Args["id"].(string)
id, err := strconv.Atoi(i)
if err != nil {
return nil, err
}
return GetFolloweeByIDAndUser(id, user.ID)
}
return nil, nil
},
})
UserType.AddFieldConfig("followees", &graphql.Field{
Type: graphql.NewNonNull(graphql.NewList(graphql.NewNonNull(UserType))),
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if user, ok := p.Source.(*User); ok == true {
return GetFolloweesForUser(user.ID)
}
return []User{}, nil
},
})
}