-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
286 lines (225 loc) · 5.82 KB
/
main.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package main
import (
"log"
"net/http"
"github.com/rs/cors"
"github.com/graphql-go/graphql"
"github.com/graphql-go/handler"
"github.com/mnmtanish/go-graphiql"
driver "github.com/johnnadratowski/golang-neo4j-bolt-driver"
)
type NeoConfig struct {
Username string
Password string
Host string
Port string
}
// Neo4j config
var conf = NeoConfig{
"neo4j",
"over1234",
"neo4j",
"7687",
}
type Person struct {
ID int64 `json:"id"`
Name string `json:"name"`
From string `json:"from"`
Friends []Person `json:"friends"`
}
type Hobby struct {
ID int64 `json:"id"`
Name string `json:"name"`
}
func getConnection() (driver.Conn, error) {
//Init Neo driver
db, err := driver.NewDriver().OpenNeo("bolt://"+conf.Username+":"+conf.Password+"@"+conf.Host+":"+conf.Port)
if err != nil { log.Println("error connecting to neo4j", err) }
return db, err
}
func getPeople() []Person {
db, _ := getConnection()
defer db.Close()
cypher := `MATCH (n:Person) RETURN ID(n) as id, n.name as name, n.from LIMIT {limit}`
data, _, _, err := db.QueryNeoAll(cypher, map[string]interface {}{ "limit": 25})
if err != nil {
log.Println("error querying person:", err)
// w.WriteHeader(500)
// w.Write([]byte("an error occured querying the DB"))
// return
} else if len(data) == 0 {
// w.WriteHeader(404)
// return
}
results := mapPeople(data)
return results
}
func getPerson(name string) Person {
db, _ := getConnection()
defer db.Close()
cypher := "MATCH (p:Person) WHERE p.name = {name} RETURN ID(p) as id, p.name, p.from"
data, err := db.QueryNeo(cypher, map[string] interface{}{"name": name})
if err != nil { log.Println("error looking up person")
} else if data == nil { log.Println("cant find person") }
fields, _, err := data.NextNeo()
result := mapPerson(fields)
return result
}
func mapPeople(rows [][]interface{}) ([]Person) {
people := make([]Person, len(rows))
for idx, row := range rows {
people[idx] =
mapPerson(row)
}
return people
}
func mapPerson(row []interface{}) (Person) {
person := Person{
ID: row[0].(int64),
Name: row[1].(string),
From: row[2].(string),
}
return person
}
func getFriends(id int) []Person {
db, _ := getConnection()
defer db.Close()
cypher := "MATCH (p:Person)-[r :FRIEND]->(friend) WHERE ID(p) = {id} RETURN ID(friend) as id, friend.name, friend.from"
data, _, _, err := db.QueryNeoAll(cypher, map[string] interface{}{"id": id})
if err != nil { log.Println("error looking up person")
} else if data == nil { log.Println("cant find person") }
friends := make([]Person, len(data))
for idx, row := range data {
friends[idx] =
mapPerson(row)
}
return friends
}
func getHobby(name string) Hobby {
db, _ := getConnection()
defer db.Close()
cypher := "MATCH (h:Hobby) WHERE h.name = {name} RETURN ID(h) as id, h.name"
data, err := db.QueryNeo(cypher, map[string] interface{}{"name": name})
if err != nil { log.Println("error looking up hobby")
} else if data == nil { log.Println("cant find hobby") }
fields, _, err := data.NextNeo()
result := Hobby {
ID: fields[0].(int64),
Name: fields[1].(string),
}
return result
}
var hobbyType = graphql.NewObject(
graphql.ObjectConfig{
Name: "Hobby",
Fields: graphql.Fields {
"id" : &graphql.Field{
Type: graphql.Int,
},
"name" : &graphql.Field{
Type: graphql.String,
},
},
},
)
func compilePersonType() (*graphql.Object) {
personType := graphql.NewObject(
graphql.ObjectConfig{
Name: "Person",
Fields: graphql.Fields {
"id" : &graphql.Field {
Type: graphql.Int,
},
"name": &graphql.Field {
Type: graphql.String,
},
"from": &graphql.Field {
Type: graphql.String,
},
},
},
)
//TODO export the field to make it reusable
personType.AddFieldConfig(
"friends",
&graphql.Field {
Type: graphql.NewList(personType),
Args: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{
Type: graphql.Int,
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return getFriends(p.Args["id"].(int)), nil
},
},
)
return personType
}
var personType = compilePersonType()
var queryType = graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"People": &graphql.Field{
Type: graphql.NewList(personType),
Description: "List of people",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return getPeople(), nil
},
},
"Person": &graphql.Field{
Type: personType,
Args: graphql.FieldConfigArgument{
"name": &graphql.ArgumentConfig{
Type: graphql.String,
},
},
Description: "A person",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return getPerson(p.Args["name"].(string)), nil
},
},
"Friends": &graphql.Field{
Type: graphql.NewList(personType),
Args: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{
Type: graphql.Int,
},
},
Description: "List of the person's friends",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return getFriends(p.Args["id"].(int)), nil
},
},
"Hobby": &graphql.Field{
Type: hobbyType,
Args: graphql.FieldConfigArgument{
"name": &graphql.ArgumentConfig{
Type:graphql.String,
},
},
Description: "A hobby",
Resolve: func(h graphql.ResolveParams) (interface{}, error) {
return getHobby(h.Args["name"].(string)), nil
},
},
},
})
var Schema, _ = graphql.NewSchema(graphql.SchemaConfig{
Query: queryType,
})
func main() {
c := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
})
h := handler.New(&handler.Config{
Schema: &Schema,
Pretty: true,
})
// serve HTTP
serveMux := http.NewServeMux()
// serveMux.HandleFunc("/neo", neo4jHandler)
serveMux.Handle("/graphql", c.Handler(h))
serveMux.HandleFunc("/graphiql", graphiql.ServeGraphiQL)
http.ListenAndServe(":8080", serveMux)
}