2019-08-09 12:19:17 +03:00
|
|
|
module Hasura.GraphQL.Schema.Common
|
|
|
|
( qualObjectToName
|
2019-07-22 15:47:13 +03:00
|
|
|
, addTypeSuffix
|
2019-08-09 12:19:17 +03:00
|
|
|
, fromInpValL
|
|
|
|
|
|
|
|
, mkColName
|
2019-07-22 15:47:13 +03:00
|
|
|
, mkColumnType
|
2019-08-09 12:19:17 +03:00
|
|
|
, mkRelName
|
|
|
|
, mkAggRelName
|
|
|
|
|
|
|
|
, SelField
|
|
|
|
|
|
|
|
, mkTableTy
|
2019-07-22 15:47:13 +03:00
|
|
|
, mkTableEnumType
|
2019-08-09 12:19:17 +03:00
|
|
|
, mkTableAggTy
|
|
|
|
|
|
|
|
, mkColumnEnumVal
|
|
|
|
) where
|
|
|
|
|
|
|
|
import qualified Data.HashMap.Strict as Map
|
|
|
|
import qualified Language.GraphQL.Draft.Syntax as G
|
|
|
|
|
|
|
|
import Hasura.GraphQL.Validate.Types
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.Types
|
|
|
|
import Hasura.SQL.Types
|
|
|
|
|
2019-08-11 18:34:38 +03:00
|
|
|
type SelField = Either PGColumnInfo (RelInfo, Bool, AnnBoolExpPartialSQL, Maybe Int, Bool)
|
2019-08-09 12:19:17 +03:00
|
|
|
|
|
|
|
qualObjectToName :: (ToTxt a) => QualifiedObject a -> G.Name
|
|
|
|
qualObjectToName = G.Name . snakeCaseQualObject
|
|
|
|
|
2019-07-22 15:47:13 +03:00
|
|
|
addTypeSuffix :: Text -> G.NamedType -> G.NamedType
|
|
|
|
addTypeSuffix suffix baseType = G.NamedType $ G.unNamedType baseType <> G.Name suffix
|
|
|
|
|
2019-08-09 12:19:17 +03:00
|
|
|
fromInpValL :: [InpValInfo] -> Map.HashMap G.Name InpValInfo
|
|
|
|
fromInpValL = mapFromL _iviName
|
|
|
|
|
|
|
|
mkRelName :: RelName -> G.Name
|
|
|
|
mkRelName rn = G.Name $ relNameToTxt rn
|
|
|
|
|
|
|
|
mkAggRelName :: RelName -> G.Name
|
|
|
|
mkAggRelName rn = G.Name $ relNameToTxt rn <> "_aggregate"
|
|
|
|
|
|
|
|
mkColName :: PGCol -> G.Name
|
|
|
|
mkColName (PGCol n) = G.Name n
|
|
|
|
|
2019-07-22 15:47:13 +03:00
|
|
|
mkColumnType :: PGColumnType -> G.NamedType
|
|
|
|
mkColumnType = \case
|
|
|
|
PGColumnScalar scalarType -> mkScalarTy scalarType
|
|
|
|
PGColumnEnumReference (EnumReference enumTable _) -> mkTableEnumType enumTable
|
|
|
|
|
2019-08-09 12:19:17 +03:00
|
|
|
mkTableTy :: QualifiedTable -> G.NamedType
|
2019-07-22 15:47:13 +03:00
|
|
|
mkTableTy = G.NamedType . qualObjectToName
|
|
|
|
|
|
|
|
mkTableEnumType :: QualifiedTable -> G.NamedType
|
|
|
|
mkTableEnumType = addTypeSuffix "_enum" . mkTableTy
|
2019-08-09 12:19:17 +03:00
|
|
|
|
|
|
|
mkTableAggTy :: QualifiedTable -> G.NamedType
|
2019-07-22 15:47:13 +03:00
|
|
|
mkTableAggTy = addTypeSuffix "_aggregate" . mkTableTy
|
2019-08-09 12:19:17 +03:00
|
|
|
|
|
|
|
-- used for 'distinct_on' in select and upsert's 'update columns'
|
|
|
|
mkColumnEnumVal :: PGCol -> EnumValInfo
|
|
|
|
mkColumnEnumVal (PGCol col) =
|
|
|
|
EnumValInfo (Just "column name") (G.EnumValue $ G.Name col) False
|