Skip to content

Commit 6987877

Browse files
author
rahulgurnani
committed
Emulate user queries in a script
1 parent f4cd0b3 commit 6987877

File tree

5 files changed

+113
-0
lines changed

5 files changed

+113
-0
lines changed

scripts/emulate_queries.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"io/ioutil"
7+
"log"
8+
"net/http"
9+
)
10+
11+
const BASE_URL = "http://localhost:8080"
12+
13+
func post(contentType string, suffixURL string, data []byte) string {
14+
req, err := http.NewRequest("POST", BASE_URL+suffixURL, bytes.NewBuffer(data))
15+
//req.Header.Set("X-Custom-Header", "myvalue")
16+
req.Header.Set("Content-Type", contentType)
17+
18+
client := &http.Client{}
19+
resp, err := client.Do(req)
20+
if err != nil {
21+
panic(err)
22+
}
23+
defer resp.Body.Close()
24+
25+
fmt.Println("response Status:", resp.Status)
26+
fmt.Println("response Headers:", resp.Header)
27+
body, _ := ioutil.ReadAll(resp.Body)
28+
fmt.Println("response Body:", string(body))
29+
30+
return string(body)
31+
}
32+
33+
func read_query(res chan string) {
34+
data, err := ioutil.ReadFile("read_nodes.graphql")
35+
if err != nil {
36+
log.Println("Error")
37+
}
38+
39+
body := post("application/graphql+-", "/query", data)
40+
res <- body
41+
}
42+
43+
func mutate_schema_query(res chan string) {
44+
data, err := ioutil.ReadFile("mutate_schema.graphql")
45+
if err != nil {
46+
log.Println("Error")
47+
}
48+
print(string(data))
49+
body := post("application/json", "/alter", data)
50+
res <- body
51+
}
52+
53+
func write_query(res chan string) {
54+
data, err := ioutil.ReadFile("mutate_data.graphql")
55+
if err != nil {
56+
log.Println("Error")
57+
}
58+
59+
body := post("application/rdf", "/mutate", data)
60+
res <- body
61+
}
62+
63+
func export_query(res chan string) {
64+
data, err := ioutil.ReadFile("export.graphql")
65+
if err != nil {
66+
log.Println("Error")
67+
}
68+
69+
body := post("application/graphql", "/admin", data)
70+
res <- body
71+
}
72+
73+
func main() {
74+
res1 := make(chan string)
75+
res2 := make(chan string)
76+
res3 := make(chan string)
77+
go read_query(res1)
78+
go mutate_schema_query(res2)
79+
go write_query(res3)
80+
//go export_query(res2)
81+
<-res1
82+
<-res2
83+
<-res3
84+
}

scripts/export.graphql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
mutation {
2+
export(input: {format: "rdf"}) {
3+
response {
4+
message
5+
code
6+
}
7+
}
8+
}

scripts/mutate_data.graphql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
set {
3+
_:company1 <name> "alpha" .
4+
_:company1 <dgraph.type> "Company" .
5+
_:company2 <name> "beta" .
6+
_:company2 <dgraph.type> "Company" .
7+
8+
_:company1 <industry> "Machinery" .
9+
10+
_:company2 <industry> "High Tech" .
11+
12+
}
13+
}

scripts/mutate_schema.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"drop_op": "TYPE", "drop_value": "Company"}

scripts/read_nodes.graphql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
data(func: eq(name, "Alice")) {
3+
name
4+
mobile @facets
5+
car @facets
6+
}
7+
}

0 commit comments

Comments
 (0)