-
Notifications
You must be signed in to change notification settings - Fork 26
/
main.go
47 lines (42 loc) · 1006 Bytes
/
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
package main
import (
"database/sql"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"github.com/graphql-go/graphql"
_ "github.com/lib/pq"
)
func handler(schema graphql.Schema) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
query, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
result := graphql.Do(graphql.Params{
Schema: schema,
RequestString: string(query),
})
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(result)
}
}
var db *sql.DB
func main() {
schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: QueryType,
Mutation: MutationType,
})
if err != nil {
log.Fatal(err)
}
db, err = sql.Open("postgres", "postgres://vagrant:vagrant@localhost:5432/graphql")
if err != nil {
log.Fatal(err)
}
http.Handle("/graphql", handler(schema))
log.Fatal(http.ListenAndServe("0.0.0.0:8080", nil))
}