package graph import ( "context" "net/http" "os" "time" "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" "github.com/google/uuid" "github.com/jordanknott/project-citadel/api/internal/auth" "github.com/jordanknott/project-citadel/api/internal/config" "github.com/jordanknott/project-citadel/api/internal/db" ) // NewHandler returns a new graphql endpoint handler. func NewHandler(config config.AppConfig, repo db.Repository) http.Handler { srv := handler.New(NewExecutableSchema(Config{ Resolvers: &Resolver{ Config: config, 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 } // NewPlaygroundHandler returns a new GraphQL Playground handler. func NewPlaygroundHandler(endpoint string) http.Handler { return playground.Handler("GraphQL Playground", endpoint) } func GetUserID(ctx context.Context) (uuid.UUID, bool) { userID, ok := ctx.Value("userID").(uuid.UUID) return userID, ok } func GetRestrictedMode(ctx context.Context) (auth.RestrictedMode, bool) { restricted, ok := ctx.Value("restricted_mode").(auth.RestrictedMode) return restricted, ok }