taskcafe/internal/graph/graph.go

56 lines
1.5 KiB
Go
Raw Normal View History

2020-04-10 04:40:22 +02:00
package graph
import (
2020-04-20 05:02:55 +02:00
"context"
2020-04-10 04:40:22 +02:00
"net/http"
"os"
"time"
2020-04-10 04:40:22 +02:00
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/handler/extension"
"github.com/99designs/gqlgen/graphql/handler/lru"
"github.com/99designs/gqlgen/graphql/handler/transport"
"github.com/99designs/gqlgen/graphql/playground"
2020-04-20 05:02:55 +02:00
"github.com/google/uuid"
2020-07-16 01:20:08 +02:00
"github.com/jordanknott/project-citadel/api/internal/config"
"github.com/jordanknott/project-citadel/api/internal/db"
2020-04-10 04:40:22 +02:00
)
// NewHandler returns a new graphql endpoint handler.
2020-07-16 01:20:08 +02:00
func NewHandler(config config.AppConfig, repo db.Repository) http.Handler {
srv := handler.New(NewExecutableSchema(Config{
2020-04-10 04:40:22 +02:00
Resolvers: &Resolver{
2020-07-16 01:20:08 +02:00
Config: config,
2020-04-10 04:40:22 +02:00
Repository: repo,
},
}))
srv.AddTransport(transport.Websocket{
KeepAlivePingInterval: 10 * time.Second,
})
srv.AddTransport(transport.Options{})
srv.AddTransport(transport.GET{})
srv.AddTransport(transport.POST{})
srv.AddTransport(transport.MultipartForm{})
srv.SetQueryCache(lru.New(1000))
srv.Use(extension.AutomaticPersistedQuery{
Cache: lru.New(100),
})
if isProd := os.Getenv("PRODUCTION") == "true"; isProd {
srv.Use(extension.FixedComplexityLimit(10))
} else {
srv.Use(extension.Introspection{})
}
return srv
2020-04-10 04:40:22 +02:00
}
// NewPlaygroundHandler returns a new GraphQL Playground handler.
func NewPlaygroundHandler(endpoint string) http.Handler {
return playground.Handler("GraphQL Playground", endpoint)
2020-04-10 04:40:22 +02:00
}
2020-04-20 05:02:55 +02:00
func GetUserID(ctx context.Context) (uuid.UUID, bool) {
userID, ok := ctx.Value("userID").(uuid.UUID)
return userID, ok
}