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"
|
2020-04-11 04:22:22 +02:00
|
|
|
"os"
|
|
|
|
"time"
|
2020-04-10 04:40:22 +02:00
|
|
|
|
2020-04-11 04:22: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-20 00:25:58 +02:00
|
|
|
"github.com/jordanknott/project-citadel/internal/auth"
|
|
|
|
"github.com/jordanknott/project-citadel/internal/config"
|
|
|
|
"github.com/jordanknott/project-citadel/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 {
|
2020-04-11 04:22:22 +02:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
}))
|
2020-04-11 04:22:22 +02:00
|
|
|
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 {
|
2020-04-11 04:22:22 +02:00
|
|
|
return playground.Handler("GraphQL Playground", endpoint)
|
2020-04-10 04:40:22 +02:00
|
|
|
}
|
2020-07-17 02:40:23 +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
|
|
|
|
}
|
2020-07-17 02:40:23 +02:00
|
|
|
|
|
|
|
func GetRestrictedMode(ctx context.Context) (auth.RestrictedMode, bool) {
|
|
|
|
restricted, ok := ctx.Value("restricted_mode").(auth.RestrictedMode)
|
|
|
|
return restricted, ok
|
|
|
|
}
|