-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_db.go
More file actions
66 lines (60 loc) · 1.59 KB
/
Copy pathquery_db.go
File metadata and controls
66 lines (60 loc) · 1.59 KB
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
//go:build ignore
// +build ignore
package main
import (
"fmt"
"log"
"net/http"
"bytes"
"encoding/json"
"io"
)
type gremlinRequest struct {
Gremlin string `json:"gremlin"`
Bindings map[string]any `json:"bindings"`
Language string `json:"language"`
Aliases map[string]string `json:"aliases"`
}
func main() {
// Let's run a query to insert repo:repo-test
query := "g.V(m0_id).fold().coalesce(unfold(), addV(m0_lbl).property(id, m0_id)).property('name', 'repo-test')"
bindings := map[string]any{
"m0_id": "repo:repo-test",
"m0_lbl": "Repository",
}
payload := gremlinRequest{
Gremlin: query,
Bindings: bindings,
Language: "gremlin-groovy",
Aliases: map[string]string{
"graph": "hugegraph",
"g": "__g_hugegraph",
},
}
reqBody, _ := json.Marshal(payload)
resp, err := http.Post(gremlinURL, "application/json", bytes.NewReader(reqBody))
if err != nil {
log.Fatalf("POST failed: %v", err)
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
fmt.Printf("Insert Response: %s\n", string(body))
// Now check if it exists
checkPayload := gremlinRequest{
Gremlin: "g.V(vid)",
Bindings: map[string]any{"vid": "repo:repo-test"},
Language: "gremlin-groovy",
Aliases: map[string]string{
"graph": "hugegraph",
"g": "__g_hugegraph",
},
}
reqBody2, _ := json.Marshal(checkPayload)
resp2, err := http.Post(gremlinURL, "application/json", bytes.NewReader(reqBody2))
if err != nil {
log.Fatalf("POST failed: %v", err)
}
defer resp2.Body.Close()
body2, _ := io.ReadAll(resp2.Body)
fmt.Printf("Check Response: %s\n", string(body2))
}