graphql-engine/server/src-exec/Main.hs

58 lines
2.0 KiB
Haskell
Raw Normal View History

{-# LANGUAGE RecordWildCards #-}
2018-06-27 16:11:32 +03:00
module Main where
import Data.Text.Conversions (convertText)
import Hasura.App
import Hasura.Logging (Hasura)
2018-06-27 16:11:32 +03:00
import Hasura.Prelude
import Hasura.RQL.DDL.Metadata (fetchMetadata)
import Hasura.RQL.DDL.Schema
import Hasura.RQL.Types
2018-06-27 16:11:32 +03:00
import Hasura.Server.Init
import Hasura.Server.Migrate (dropCatalog)
import Hasura.Server.Version
2018-06-27 16:11:32 +03:00
import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString.Lazy.Char8 as BLC
import qualified Database.PG.Query as Q
2018-06-27 16:11:32 +03:00
main :: IO ()
main = parseArgs >>= unAppM . runApp
runApp :: HGEOptions Hasura -> AppM ()
runApp (HGEOptionsG rci hgeCmd) =
withVersion $$(getVersionFromEnvironment) case hgeCmd of
HCServe serveOptions -> do
(initCtx, initTime) <- initialiseCtx hgeCmd rci
runHGEServer serveOptions initCtx initTime
HCExport -> do
(initCtx, _) <- initialiseCtx hgeCmd rci
res <- runTx' initCtx fetchMetadata
either printErrJExit printJSON res
HCClean -> do
(initCtx, _) <- initialiseCtx hgeCmd rci
res <- runTx' initCtx dropCatalog
either printErrJExit (const cleanSuccess) res
HCExecute -> do
(InitCtx{..}, _) <- initialiseCtx hgeCmd rci
queryBs <- liftIO BL.getContents
let sqlGenCtx = SQLGenCtx False
res <- runAsAdmin _icPgPool sqlGenCtx _icHttpManager do
schemaCache <- buildRebuildableSchemaCache
execQuery queryBs
& runHasSystemDefinedT (SystemDefined False)
& runCacheRWT schemaCache
& fmap fst
either printErrJExit (liftIO . BLC.putStrLn) res
2019-01-28 16:55:28 +03:00
HCVersion -> liftIO $ putStrLn $ "Hasura GraphQL Engine: " ++ convertText currentVersion
where
runTx' initCtx tx =
liftIO $ runExceptT $ Q.runTx (_icPgPool initCtx) (Q.Serializable, Nothing) tx
2019-01-28 16:55:28 +03:00
cleanSuccess = liftIO $ putStrLn "successfully cleaned graphql-engine related data"