forked from callistaenterprise/goblog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accountql.go
214 lines (200 loc) · 6.32 KB
/
accountql.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package service
import (
internalmodel "github.com/aclk/goblog/accountservice/internal/app/model"
"github.com/aclk/goblog/common/model"
"github.com/graphql-go/graphql"
"github.com/sirupsen/logrus"
"log"
"strconv"
"time"
)
var schema graphql.Schema
var schemaInitialized = false
// Used for testing...
var accounts []internalmodel.Account
// init seeds some test data.
func init() {
accounts = make([]internalmodel.Account, 0)
for a := 0; a < 10; a++ {
accountID := strconv.Itoa(120 + a)
quote := internalmodel.Quote{Text: "HEJ", Language: "sv"}
account := internalmodel.Account{ID: accountID, Name: "Test Testsson " + strconv.Itoa(a), ServedBy: "localhost", Quote: quote}
account.AccountEvents = make([]model.AccountEvent, 0)
account.AccountEvents = append(account.AccountEvents, model.AccountEvent{ID: strconv.Itoa(1), AccountID: accountID, EventName: "CREATED", Created: time.Now().Format("2006-01-02T15:04:05")})
account.AccountEvents = append(account.AccountEvents, model.AccountEvent{ID: strconv.Itoa(2), AccountID: accountID, EventName: "UPDATED", Created: time.Now().Format("2006-01-02T15:04:05")})
account.ImageData = model.AccountImage{ID: accountID, URL: "http://fake.path/image.png", ServedBy: "localhost"}
accounts = append(accounts, account)
}
}
func initGraphQL(resolvers GraphQLResolvers) {
if schemaInitialized {
return
}
// ----------- Start declare types ------------------
// quoteType
var quoteType = graphql.NewObject(graphql.ObjectConfig{
Name: "Quote",
Fields: graphql.Fields{
"id": &graphql.Field{
Type: graphql.String,
},
"quote": &graphql.Field{
Type: graphql.String,
},
"ipAddress": &graphql.Field{
Type: graphql.String,
},
"language": &graphql.Field{
Type: graphql.String,
},
},
})
// accountEventType
var accountEventType = graphql.NewObject(graphql.ObjectConfig{
Name: "AccountEvent",
Fields: graphql.Fields{
"id": &graphql.Field{
Type: graphql.String,
},
"eventName": &graphql.Field{
Type: graphql.String,
},
"created": &graphql.Field{
Type: graphql.String,
},
},
})
// accountImageType
var accountImageType = graphql.NewObject(graphql.ObjectConfig{
Name: "AccountImage",
Fields: graphql.Fields{
"id": &graphql.Field{
Type: graphql.String,
},
"url": &graphql.Field{
Type: graphql.String,
},
},
})
// accountType, includes Resolver functions for inner quotes and events.
var accountType = graphql.NewObject(graphql.ObjectConfig{
Name: "Account",
Fields: graphql.Fields{
"id": &graphql.Field{
Type: graphql.String,
},
"name": &graphql.Field{
Type: graphql.String,
},
"servedBy": &graphql.Field{
Type: graphql.String,
},
"quote": &graphql.Field{
Type: quoteType,
Args: graphql.FieldConfigArgument{
"language": &graphql.ArgumentConfig{
Type: graphql.String,
Description: "Two letter ISO language code such as en or sv",
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
logrus.Infof("ENTER - resolve function for quote with params %v", p.Args)
account := p.Source.(internalmodel.Account)
if account.Quote.Language == p.Args["language"] {
return account.Quote, nil
}
return account.Quote, nil
},
},
"events": &graphql.Field{
Type: graphql.NewList(accountEventType),
Args: graphql.FieldConfigArgument{
"eventName": &graphql.ArgumentConfig{
Type: graphql.String,
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
logrus.Infof("ENTER - resolve function for events with params %v", p.Args)
account := p.Source.(internalmodel.Account)
if len(p.Args) == 0 {
return account.AccountEvents, nil
}
response := make([]model.AccountEvent, 0)
for _, item := range account.AccountEvents {
if item.EventName == p.Args["eventName"] {
response = append(response, item)
}
}
return response, nil
},
},
"imageData": &graphql.Field{
Type: accountImageType,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
logrus.Infof("ENTER - resolve function for imageData with params %v", p.Args)
account := p.Source.(internalmodel.Account)
return account.ImageData, nil
},
},
},
})
// Schema
fields := graphql.Fields{
"Account": &graphql.Field{
Type: graphql.Type(accountType),
Args: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{
Type: graphql.String,
},
"name": &graphql.ArgumentConfig{
Type: graphql.String,
},
},
Resolve: resolvers.AccountResolverFunc,
},
//"AllAccounts": &graphql.Field{
// Type: graphql.NewList(accountType),
// Args: graphql.FieldConfigArgument{
// "id": &graphql.ArgumentConfig{
// Type: graphql.String,
// },
// "name": &graphql.ArgumentConfig{
// Type: graphql.String,
// },
// },
// Resolve: resolvers.AllAccountsResolverFunc,
//},
}
rootQuery := graphql.ObjectConfig{Name: "RootQuery", Fields: fields}
schemaConfig := graphql.SchemaConfig{Query: graphql.NewObject(rootQuery)}
var err error
schema, err = graphql.NewSchema(schemaConfig)
if err != nil {
log.Fatalf("failed to create new schema, error: %v", err)
}
logrus.Infoln("Successfully initialized GraphQL")
schemaInitialized = true
}
// GetAccountQL is a HTTP handler function that decodes the supplied Base64-encoded GraphQL query, executes the supplied
// GraphQL query and returns the result as JSON.
//func GetAccountQLBase64(resp http.ResponseWriter, req *http.Request) {
// logrus.Infoln("ENTER - GetAccountQLBase64")
// query := req.URL.Query()["query"]
// logrus.Infof("Read query %v\n", query)
// decodedString, err := base64.URLEncoding.DecodeString(query[0])
// logrus.Infof("Decoded query: %v\n", string(decodedString))
//
// if err != nil {
// writeJSONResponse(resp, 400, []byte(err.Error()))
// return
// }
//
// params := graphql.Params{Schema: schema, RequestString: string(decodedString)}
// r := graphql.Do(params)
// if len(r.Errors) > 0 {
// logrus.Errorf("failed to execute graphql operation, errors: %+v", r.Errors)
// log.Fatalf("failed to execute graphql operation, errors: %+v", r.Errors)
// }
// rJSON, _ := json.Marshal(r)
// writeJSONResponse(resp, 200, rJSON)
//}