-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
59 lines (47 loc) · 1.54 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
package main
import (
"net/http"
"github.com/JonMallozzi/Go_Fiber_GraphQL_DEMO/graph/postgres"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/JonMallozzi/Go_Fiber_GraphQL_DEMO/graph"
"github.com/JonMallozzi/Go_Fiber_GraphQL_DEMO/graph/generated"
"github.com/gofiber/fiber"
"github.com/valyala/fasthttp/fasthttpadaptor"
//"github.com/arsmn/gqlgen" //provides GrahpQL with fasthttp
//graph/model/generated
)
//An example set up for basic fiber
func main() {
db := postgres.PostgresConnect()
app := fiber.New()
app.Get("/", func(c *fiber.Ctx) {
c.Type("html")
c.Send("<h1> Hello World! </h1>")
})
app.Get("/hello/:name", func(c *fiber.Ctx) {
c.Type("html")
if c.Params("name") != "" {
c.Send("<h1> Hello " + c.Params("name") + "! </h1>")
} else {
c.Send("Please Provide A Name to Be Greeted")
}
})
//configuring data for the resolvers
config := generated.Config{Resolvers: &graph.Resolver{
UsersRepo: postgres.UserRepo{DB: db},
}}
app.Post("/query", func(ctx *fiber.Ctx) {
h := handler.NewDefaultServer(generated.NewExecutableSchema(config))
fasthttpadaptor.NewFastHTTPHandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
h.ServeHTTP(writer, request)
})(ctx.Fasthttp)
})
app.Get("/playground", func(ctx *fiber.Ctx) {
h := playground.Handler("GraphQL", "/query")
fasthttpadaptor.NewFastHTTPHandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
h.ServeHTTP(writer, request)
})(ctx.Fasthttp)
})
app.Listen(3000)
}