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
|
|
|
|
|
2019-09-19 07:47:36 +03:00
|
|
|
, RelationshipFieldInfo(..)
|
|
|
|
, SelField
|
|
|
|
|
2019-07-22 15:47:13 +03:00
|
|
|
, mkColumnType
|
2019-08-09 12:19:17 +03:00
|
|
|
, mkRelName
|
|
|
|
, mkAggRelName
|
|
|
|
|
|
|
|
, mkTableTy
|
2019-07-22 15:47:13 +03:00
|
|
|
, mkTableEnumType
|
2019-08-09 12:19:17 +03:00
|
|
|
, mkTableAggTy
|
|
|
|
|
|
|
|
, mkColumnEnumVal
|
2019-09-17 04:51:11 +03:00
|
|
|
, mkDescriptionWith
|
|
|
|
, mkDescription
|
2019-08-09 12:19:17 +03:00
|
|
|
) where
|
|
|
|
|
|
|
|
import qualified Data.HashMap.Strict as Map
|
2019-09-17 04:51:11 +03:00
|
|
|
import qualified Data.Text as T
|
2019-08-09 12:19:17 +03:00
|
|
|
import qualified Language.GraphQL.Draft.Syntax as G
|
|
|
|
|
2019-09-19 07:47:36 +03:00
|
|
|
import Hasura.GraphQL.Resolve.Types
|
2019-08-09 12:19:17 +03:00
|
|
|
import Hasura.GraphQL.Validate.Types
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.Types
|
|
|
|
import Hasura.SQL.Types
|
|
|
|
|
2019-09-19 07:47:36 +03:00
|
|
|
data RelationshipFieldInfo
|
|
|
|
= RelationshipFieldInfo
|
|
|
|
{ _rfiInfo :: !RelInfo
|
|
|
|
, _rfiAllowAgg :: !Bool
|
|
|
|
, _rfiColumns :: !PGColGNameMap
|
|
|
|
, _rfiPermFilter :: !AnnBoolExpPartialSQL
|
|
|
|
, _rfiPermLimit :: !(Maybe Int)
|
|
|
|
, _rfiIsNullable :: !Bool
|
|
|
|
} deriving (Show, Eq)
|
|
|
|
|
|
|
|
type SelField = Either PGColumnInfo RelationshipFieldInfo
|
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"
|
|
|
|
|
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'
|
2019-09-19 07:47:36 +03:00
|
|
|
mkColumnEnumVal :: G.Name -> EnumValInfo
|
|
|
|
mkColumnEnumVal colName =
|
|
|
|
EnumValInfo (Just "column name") (G.EnumValue colName) False
|
2019-09-17 04:51:11 +03:00
|
|
|
|
|
|
|
mkDescriptionWith :: Maybe PGDescription -> Text -> G.Description
|
|
|
|
mkDescriptionWith descM defaultTxt = G.Description $ case descM of
|
|
|
|
Nothing -> defaultTxt
|
|
|
|
Just (PGDescription descTxt) -> T.unlines [descTxt, "\n", defaultTxt]
|
|
|
|
|
|
|
|
mkDescription :: PGDescription -> G.Description
|
|
|
|
mkDescription = G.Description . getPGDescription
|