2018-08-06 15:15:08 +03:00
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
{-# LANGUAGE LambdaCase #-}
|
|
|
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
|
|
|
{-# LANGUAGE MultiWayIf #-}
|
|
|
|
{-# LANGUAGE NoImplicitPrelude #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
module Hasura.GraphQL.Resolve.Select
|
|
|
|
( convertSelect
|
|
|
|
) where
|
|
|
|
|
|
|
|
import Data.Has
|
|
|
|
import Hasura.Prelude
|
|
|
|
|
|
|
|
import qualified Data.HashMap.Strict as Map
|
|
|
|
import qualified Language.GraphQL.Draft.Syntax as G
|
|
|
|
|
|
|
|
import qualified Hasura.RQL.DML.Select as RS
|
|
|
|
|
|
|
|
import qualified Hasura.SQL.DML as S
|
|
|
|
|
|
|
|
import Hasura.GraphQL.Resolve.BoolExp
|
|
|
|
import Hasura.GraphQL.Resolve.Context
|
|
|
|
import Hasura.GraphQL.Resolve.InputValue
|
|
|
|
import Hasura.GraphQL.Validate.Field
|
|
|
|
import Hasura.GraphQL.Validate.Types
|
2018-08-06 15:15:08 +03:00
|
|
|
import Hasura.RQL.DML.Internal (onlyPositiveInt)
|
2018-06-27 16:11:32 +03:00
|
|
|
import Hasura.RQL.Types
|
|
|
|
import Hasura.SQL.Types
|
2018-08-06 15:15:08 +03:00
|
|
|
import Hasura.SQL.Value
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
fromSelSet
|
|
|
|
:: G.NamedType
|
|
|
|
-> SelSet
|
|
|
|
-> Convert (Map.HashMap FieldName RS.AnnFld)
|
|
|
|
fromSelSet fldTy flds =
|
|
|
|
fmap Map.fromList $ forM (toList flds) $ \fld -> do
|
|
|
|
let fldName = _fName fld
|
|
|
|
let rqlFldName = FieldName $ G.unName $ G.unAlias $ _fAlias fld
|
|
|
|
case fldName of
|
|
|
|
"__typename" -> return (rqlFldName, RS.FExp $ G.unName $ G.unNamedType fldTy)
|
|
|
|
_ -> do
|
|
|
|
fldInfo <- getFldInfo fldTy fldName
|
|
|
|
case fldInfo of
|
|
|
|
Left (PGColInfo pgCol colTy) -> return (rqlFldName, RS.FCol (pgCol, colTy))
|
2018-08-06 15:15:08 +03:00
|
|
|
Right (relInfo, tableFilter, tableLimit) -> do
|
2018-06-27 16:11:32 +03:00
|
|
|
let relTN = riRTable relInfo
|
2018-08-06 15:15:08 +03:00
|
|
|
relSelData <- fromField relTN tableFilter tableLimit fld
|
2018-06-27 16:11:32 +03:00
|
|
|
let annRel = RS.AnnRel (riName relInfo) (riType relInfo)
|
|
|
|
(riMapping relInfo) relSelData
|
|
|
|
return (rqlFldName, RS.FRel annRel)
|
|
|
|
|
2018-08-06 15:15:08 +03:00
|
|
|
fieldAsPath :: (MonadError QErr m) => Field -> m a -> m a
|
|
|
|
fieldAsPath fld = nameAsPath $ _fName fld
|
|
|
|
|
2018-06-27 16:11:32 +03:00
|
|
|
fromField
|
2018-08-06 15:15:08 +03:00
|
|
|
:: QualifiedTable -> S.BoolExp -> Maybe Int -> Field -> Convert RS.SelectData
|
|
|
|
fromField tn permFilter permLimit fld = fieldAsPath fld $ do
|
2018-06-27 16:11:32 +03:00
|
|
|
whereExpM <- withArgM args "where" $ convertBoolExp tn
|
|
|
|
ordByExpM <- withArgM args "order_by" parseOrderBy
|
2018-08-06 15:15:08 +03:00
|
|
|
limitExpM <- RS.applyPermLimit permLimit
|
|
|
|
<$> withArgM args "limit" parseLimit
|
2018-06-27 16:11:32 +03:00
|
|
|
offsetExpM <- withArgM args "offset" $ asPGColVal >=> prepare
|
|
|
|
annFlds <- fromSelSet (_fType fld) $ _fSelSet fld
|
|
|
|
return $ RS.SelectData annFlds tn (permFilter, whereExpM) ordByExpM
|
|
|
|
[] limitExpM offsetExpM
|
|
|
|
where
|
|
|
|
args = _fArguments fld
|
|
|
|
|
|
|
|
getEnumInfo
|
|
|
|
:: ( MonadError QErr m
|
|
|
|
, MonadReader r m
|
|
|
|
, Has OrdByResolveCtx r
|
|
|
|
)
|
|
|
|
=> G.NamedType -> G.EnumValue -> m OrdByResolveCtxElem
|
|
|
|
getEnumInfo nt v = do
|
|
|
|
-- fldMap <- _gcFieldMap <$> ask
|
|
|
|
ordByCtx <- asks getter
|
|
|
|
onNothing (Map.lookup (nt,v) ordByCtx) $
|
|
|
|
throw500 $ "could not lookup " <> showName (G.unEnumValue v) <> " in " <>
|
|
|
|
showNamedTy nt
|
|
|
|
|
|
|
|
parseOrderBy
|
|
|
|
:: (MonadError QErr m
|
|
|
|
, MonadReader r m
|
|
|
|
, Has OrdByResolveCtx r
|
|
|
|
)
|
|
|
|
=> AnnGValue -> m S.OrderByExp
|
|
|
|
parseOrderBy v = do
|
|
|
|
enums <- withArray (const $ mapM asEnumVal) v
|
|
|
|
fmap S.OrderByExp $ forM enums $ \(nt, ev) ->
|
|
|
|
convOrdByElem <$> getEnumInfo nt ev
|
|
|
|
-- return $ map convOrdByElem enums
|
|
|
|
-- undefined
|
|
|
|
where
|
|
|
|
convOrdByElem (PGColInfo col _, ordTy, nullsOrd) =
|
|
|
|
S.OrderByItem (Left col)
|
|
|
|
(Just $ convOrdTy ordTy)
|
|
|
|
(Just $ convNullsOrd nullsOrd)
|
|
|
|
|
|
|
|
convOrdTy = \case
|
|
|
|
OAsc -> S.OTAsc
|
|
|
|
ODesc -> S.OTDesc
|
|
|
|
|
|
|
|
convNullsOrd = \case
|
|
|
|
NFirst -> S.NFirst
|
|
|
|
NLast -> S.NLast
|
|
|
|
|
2018-08-06 15:15:08 +03:00
|
|
|
parseLimit :: ( MonadError QErr m ) => AnnGValue -> m Int
|
|
|
|
parseLimit v = do
|
|
|
|
(_, pgColVal) <- asPGColVal v
|
|
|
|
limit <- maybe noIntErr return $ pgColValueToInt pgColVal
|
|
|
|
-- validate int value
|
|
|
|
onlyPositiveInt limit
|
|
|
|
return limit
|
|
|
|
where
|
|
|
|
noIntErr = throw400 Unexpected "expecting Integer value for \"limit\""
|
|
|
|
|
2018-06-27 16:11:32 +03:00
|
|
|
convertSelect
|
2018-08-06 15:15:08 +03:00
|
|
|
:: QualifiedTable -> S.BoolExp -> Maybe Int -> Field -> Convert RespTx
|
|
|
|
convertSelect qt permFilter permLimit fld = do
|
|
|
|
selData <- withPathK "selectionSet" $
|
|
|
|
fromField qt permFilter permLimit fld
|
2018-06-27 16:11:32 +03:00
|
|
|
prepArgs <- get
|
|
|
|
return $ RS.selectP2 (selData, prepArgs)
|