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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
package social
import (
"context"
"errors"
"fmt"
"strings"
"time"
"github.com/graph-gophers/graphql-go"
)
const Schema = `
schema {
query: Query
}
type Query {
admin(id: ID!, role: Role = ADMIN): Admin!
user(id: ID!): User!
search(text: String!): [SearchResult]!
}
interface Admin {
id: ID!
name: String!
role: Role!
}
interface Person {
name: String!
}
scalar Time
type User implements Admin & Person {
id: ID!
name: String!
email: String!
role: Role!
phone: String!
address: [String!]
friends(page: Pagination): [User]
createdAt: Time!
}
input Pagination {
first: Int
last: Int
}
enum Role {
ADMIN
USER
}
union SearchResult = User
`
type page struct {
First *float64
Last *float64
}
type admin interface {
ID() graphql.ID
Name() string
Role() string
}
type adminResolver struct {
admin
}
func (r *adminResolver) ToUser() (*user, bool) {
n, ok := r.admin.(user)
return &n, ok
}
type searchResult struct {
result interface{}
}
func (r *searchResult) ToUser() (*user, bool) {
res, ok := r.result.(*user)
return res, ok
}
type contact struct {
Email string
Phone string
}
type user struct {
IDField string
NameField string
RoleField string
Address *[]string
FriendsField *[]*user
CreatedAt graphql.Time
contact
}
func (u user) ID() graphql.ID {
return graphql.ID(u.IDField)
}
func (u user) Name() string {
return u.NameField
}
func (u user) Role() string {
return u.RoleField
}
func (u user) Friends(args struct{ Page *page }) (*[]*user, error) {
var from int
numFriends := len(*u.FriendsField)
to := numFriends
if args.Page != nil {
if args.Page.First != nil {
from = int(*args.Page.First)
if from > numFriends {
return nil, errors.New("not enough users")
}
}
if args.Page.Last != nil {
to = int(*args.Page.Last)
if to == 0 || to > numFriends {
to = numFriends
}
}
}
friends := (*u.FriendsField)[from:to]
return &friends, nil
}
var users = []*user{
{
IDField: "0x01",
NameField: "Albus Dumbledore",
RoleField: "ADMIN",
Address: &[]string{"Office @ Hogwarts", "where Horcruxes are"},
CreatedAt: graphql.Time{Time: time.Now()},
contact: contact{
Email: "Albus@hogwarts.com",
Phone: "000-000-0000",
},
},
{
IDField: "0x02",
NameField: "Harry Potter",
RoleField: "USER",
Address: &[]string{"123 dorm room @ Hogwarts", "456 random place"},
CreatedAt: graphql.Time{Time: time.Now()},
contact: contact{
Email: "harry@hogwarts.com",
Phone: "000-000-0001",
},
},
{
IDField: "0x03",
NameField: "Hermione Granger",
RoleField: "USER",
Address: &[]string{"233 dorm room @ Hogwarts", "786 @ random place"},
CreatedAt: graphql.Time{Time: time.Now()},
contact: contact{
Email: "hermione@hogwarts.com",
Phone: "000-000-0011",
},
},
{
IDField: "0x04",
NameField: "Ronald Weasley",
RoleField: "USER",
Address: &[]string{"411 dorm room @ Hogwarts", "981 @ random place"},
CreatedAt: graphql.Time{Time: time.Now()},
contact: contact{
Email: "ronald@hogwarts.com",
Phone: "000-000-0111",
},
},
}
var usersMap = make(map[string]*user)
func init() {
users[0].FriendsField = &[]*user{users[1]}
users[1].FriendsField = &[]*user{users[0], users[2], users[3]}
users[2].FriendsField = &[]*user{users[1], users[3]}
users[3].FriendsField = &[]*user{users[1], users[2]}
for _, usr := range users {
usersMap[usr.IDField] = usr
}
}
type Resolver struct{}
func (r *Resolver) Admin(ctx context.Context, args struct {
ID string
Role string
},
) (*adminResolver, error) {
if usr, ok := usersMap[args.ID]; ok {
if usr.RoleField == args.Role {
return &adminResolver{*usr}, nil
}
}
err := fmt.Errorf("user with id=%s and role=%s does not exist", args.ID, args.Role)
return nil, err
}
func (r *Resolver) User(ctx context.Context, args struct{ ID string }) (user, error) {
if usr, ok := usersMap[args.ID]; ok {
return *usr, nil
}
err := fmt.Errorf("user with id=%s does not exist", args.ID)
return user{}, err
}
func (r *Resolver) Search(ctx context.Context, args struct{ Text string }) ([]*searchResult, error) {
var result []*searchResult
for _, usr := range users {
if strings.Contains(usr.NameField, args.Text) {
result = append(result, &searchResult{usr})
}
}
return result, nil
}
|