main.go
1.1 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
package main
import (
"fmt"
"log"
"net/http"
"github.com/bradford-hamilton/go-graphql-api/gql"
"github.com/bradford-hamilton/go-graphql-api/postgres"
"github.com/bradford-hamilton/go-graphql-api/server"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/render"
"github.com/graphql-go/graphql"
)
func main() {
router, db := initializeAPI()
defer db.Close()
log.Fatal(http.ListenAndServe(":4000", router))
}
func initializeAPI() (*chi.Mux, *postgres.Db) {
router := chi.NewRouter()
db, err := postgres.New(
postgres.ConnString("localhost", 5432, "bradford", "go_graphql_db"),
)
if err != nil {
log.Fatal(err)
}
rootQuery := gql.NewRoot(db)
sc, err := graphql.NewSchema(
graphql.SchemaConfig{Query: rootQuery.Query},
)
if err != nil {
fmt.Println("Error creating schema: ", err)
}
s := server.Server{
GqlSchema: &sc,
}
router.Use(
render.SetContentType(render.ContentTypeJSON),
middleware.Logger,
middleware.StripSlashes,
middleware.Recoverer,
)
router.Post("/graphql", s.GraphQL())
return router, db
}