forked from gofiber/recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
45 lines (35 loc) · 1.08 KB
/
server.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
package main
import (
"log"
"net/http"
"os"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/recipes/graphql/graph"
"github.com/gofiber/recipes/graphql/graph/generated"
"github.com/valyala/fasthttp/fasthttpadaptor"
)
const defaultPort = "8080"
func main() {
port := os.Getenv("PORT")
if port == "" {
port = defaultPort
}
app := fiber.New()
srv := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{}}))
gqlHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
srv.ServeHTTP(w, r)
})
playground := playground.Handler("GraphQL playground", "/query")
app.All("/query", func(c *fiber.Ctx) error {
fasthttpadaptor.NewFastHTTPHandler(gqlHandler)(c.Context())
return nil
})
app.All("/", func(c *fiber.Ctx) error {
fasthttpadaptor.NewFastHTTPHandler(playground)(c.Context())
return nil
})
log.Printf("connect to http://localhost:%s/ for GraphQL playground", port)
log.Fatal(app.Listen(":" + port))
}