graphql-engine/server/src-lib/Hasura/SQL/Backend.hs
Antoine Leblanc 405b29c82d server: generate backend-related code with TemplateHaskell
GitOrigin-RevId: 251b8dbeb5181e42cca0369abf54bfc2179ad3d8
2021-03-18 20:35:23 +00:00

39 lines
1.1 KiB
Haskell

module Hasura.SQL.Backend
( BackendType(..)
, supportedBackends
) where
import Hasura.Prelude
import Data.Aeson
import Data.Text (unpack)
import Data.Text.Extended
-- | An enum that represents each backend we support.
-- This type MUST be an enumeration (an enumeration consists of one or
-- more nullary, non-GADT constructors).
data BackendType
= Postgres
| MSSQL
deriving (Eq, Ord, Bounded, Enum)
-- | The name of the backend, as we expect it to appear in our metadata and API.
instance ToTxt BackendType where
toTxt Postgres = "postgres"
toTxt MSSQL = "mssql"
-- | The FromJSON instance uses this lookup mechanism to avoid having
-- to duplicate and hardcode the backend string.
instance FromJSON BackendType where
parseJSON = withText "backend type" \name ->
lookup name [(toTxt b, b) | b <- supportedBackends]
`onNothing` fail ("got: " <> unpack name <> ", expected one of: " <> unpack (commaSeparated supportedBackends))
instance ToJSON BackendType where
toJSON = String . toTxt
supportedBackends :: [BackendType]
supportedBackends = [minBound @BackendType .. maxBound]