/
graphQL_handler.go
49 lines (42 loc) · 1.22 KB
/
graphQL_handler.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
package http
import (
"log"
"net/http"
"time"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/99designs/gqlgen/handler"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/josephnp732/Stocks-GraphQL/apq"
"github.com/josephnp732/Stocks-GraphQL/graph"
"github.com/josephnp732/Stocks-GraphQL/graph/generated"
)
// PlaygroundHandler returns the handler for the GraphQL playground
func PlaygroundHandler() gin.HandlerFunc {
h := playground.Handler("GraphQL playground", "/graphQL")
return func(c *gin.Context) {
h.ServeHTTP(c.Writer, c.Request)
}
}
// GraphQLHandler return sthe handler for the graphQL URL
func GraphQLHandler() gin.HandlerFunc {
cache, err := apq.NewCache(24 * time.Hour)
if err != nil {
log.Fatalf("cannot create APQ redis cache: %v", err)
}
c := generated.Config{Resolvers: &graph.Resolver{}}
srv := handler.GraphQL(
generated.NewExecutableSchema(c),
handler.EnablePersistedQueryCache(cache),
handler.WebsocketKeepAliveDuration(19*time.Second),
handler.WebsocketUpgrader(websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
EnableCompression: true,
}),
)
return func(c *gin.Context) {
srv.ServeHTTP(c.Writer, c.Request)
}
}