-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
65 lines (53 loc) · 1.35 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
package main
import (
"fmt"
"log"
"net/http"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
"github.com/yuuu/gqlgen-echo-sample/graph"
"github.com/yuuu/gqlgen-echo-sample/graph/generated"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
func main() {
db, err := gorm.Open( // 修正
"postgres",
fmt.Sprintf(
"host=%s port=%d user=%s dbname=%s password=%s sslmode=disable",
"127.0.0.1", 5432, "pguser", "gqlgen-echo-sample", "pgpass",
),
)
if err != nil {
log.Fatalln(err)
}
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.GET("/", welcome())
graphqlHandler := handler.NewDefaultServer(
generated.NewExecutableSchema(
generated.Config{Resolvers: &graph.Resolver{DB: db}},
),
)
playgroundHandler := playground.Handler("GraphQL", "/query")
e.POST("/query", func(c echo.Context) error {
graphqlHandler.ServeHTTP(c.Response(), c.Request())
return nil
})
e.GET("/playground", func(c echo.Context) error {
playgroundHandler.ServeHTTP(c.Response(), c.Request())
return nil
})
err = e.Start(":3000")
if err != nil {
log.Fatalln(err)
}
}
func welcome() echo.HandlerFunc {
return func(c echo.Context) error {
return c.String(http.StatusOK, "Welcome!")
}
}