graphql-engine/server/src-lib/Hasura/GraphQL/Resolve/Context.hs

156 lines
4.3 KiB
Haskell
Raw Normal View History

2018-06-27 16:11:32 +03:00
module Hasura.GraphQL.Resolve.Context
( FunctionArgItem(..)
, OrdByItem(..)
, UpdPermForIns(..)
, InsCtx(..)
2018-06-27 16:11:32 +03:00
, RespTx
, LazyRespTx
, AnnPGVal(..)
2019-04-17 12:48:41 +03:00
, UnresolvedVal(..)
, resolveValTxt
, InsertTxConflictCtx(..)
2018-06-27 16:11:32 +03:00
, getFldInfo
, getPGColInfo
, getArg
, withArg
, withArgM
, nameAsPath
2019-04-17 12:48:41 +03:00
2018-06-27 16:11:32 +03:00
, PrepArgs
, prepare
, prepareColVal
2019-04-17 12:48:41 +03:00
, withPrepArgs
, txtConverter
2019-04-17 12:48:41 +03:00
, withSelSet
, fieldAsPath
, resolvePGCol
2018-06-27 16:11:32 +03:00
, module Hasura.GraphQL.Utils
, module Hasura.GraphQL.Resolve.Types
2018-06-27 16:11:32 +03:00
) where
import Data.Has
import Hasura.Prelude
import qualified Data.HashMap.Strict as Map
import qualified Data.Sequence as Seq
import qualified Database.PG.Query as Q
import qualified Language.GraphQL.Draft.Syntax as G
2018-06-27 16:11:32 +03:00
import Hasura.GraphQL.Resolve.Types
2018-06-27 16:11:32 +03:00
import Hasura.GraphQL.Utils
import Hasura.GraphQL.Validate.Field
import Hasura.GraphQL.Validate.Types
import Hasura.RQL.DML.Internal (currentSession, sessVarFromCurrentSetting)
2018-06-27 16:11:32 +03:00
import Hasura.RQL.Types
import Hasura.SQL.Types
import Hasura.SQL.Value
import qualified Hasura.SQL.DML as S
2019-04-17 12:48:41 +03:00
2018-06-27 16:11:32 +03:00
getFldInfo
:: (MonadError QErr m, MonadReader r m, Has FieldMap r)
=> G.NamedType -> G.Name
-> m ResolveField
2018-06-27 16:11:32 +03:00
getFldInfo nt n = do
fldMap <- asks getter
onNothing (Map.lookup (nt,n) fldMap) $
throw500 $ "could not lookup " <> showName n <> " in " <>
showNamedTy nt
getPGColInfo
:: (MonadError QErr m, MonadReader r m, Has FieldMap r)
=> G.NamedType -> G.Name -> m PGColumnInfo
2018-06-27 16:11:32 +03:00
getPGColInfo nt n = do
fldInfo <- getFldInfo nt n
case fldInfo of
RFPGColumn pgColInfo -> return pgColInfo
RFRelationship _ -> throw500 $ mkErrMsg "relation"
RFComputedField _ -> throw500 $ mkErrMsg "computed field"
where
mkErrMsg ty =
"found " <> ty <> " when expecting pgcolinfo for "
2018-06-27 16:11:32 +03:00
<> showNamedTy nt <> ":" <> showName n
getArg
:: (MonadError QErr m)
=> ArgsMap
-> G.Name
-> m AnnInpVal
2018-06-27 16:11:32 +03:00
getArg args arg =
onNothing (Map.lookup arg args) $
throw500 $ "missing argument: " <> showName arg
prependArgsInPath
:: (MonadError QErr m)
=> m a -> m a
prependArgsInPath = withPathK "args"
nameAsPath
:: (MonadError QErr m)
=> G.Name -> m a -> m a
nameAsPath name = withPathK (G.unName name)
2018-06-27 16:11:32 +03:00
withArg
:: (MonadError QErr m)
=> ArgsMap
-> G.Name
-> (AnnInpVal -> m a)
2018-06-27 16:11:32 +03:00
-> m a
withArg args arg f = prependArgsInPath $ nameAsPath arg $
2018-06-27 16:11:32 +03:00
getArg args arg >>= f
withArgM
:: (MonadReusability m, MonadError QErr m)
2018-06-27 16:11:32 +03:00
=> ArgsMap
-> G.Name
-> (AnnInpVal -> m a)
2018-06-27 16:11:32 +03:00
-> m (Maybe a)
withArgM args argName f = do
wrappedArg <- for (Map.lookup argName args) $ \arg -> do
when (isJust (_aivVariable arg) && G.isNullable (_aivType arg)) markNotReusable
pure . bool (Just arg) Nothing $ hasNullVal (_aivValue arg)
prependArgsInPath . nameAsPath argName $ traverse f (join wrappedArg)
2018-06-27 16:11:32 +03:00
type PrepArgs = Seq.Seq Q.PrepArg
prepare :: (MonadState PrepArgs m) => AnnPGVal -> m S.SQLExp
prepare (AnnPGVal _ _ scalarValue) = prepareColVal scalarValue
2019-04-17 12:48:41 +03:00
resolveValTxt :: (Applicative f) => UnresolvedVal -> f S.SQLExp
resolveValTxt = \case
UVPG annPGVal -> txtConverter annPGVal
UVSessVar colTy sessVar -> sessVarFromCurrentSetting colTy sessVar
UVSQL sqlExp -> pure sqlExp
UVSession -> pure currentSession
2019-04-17 12:48:41 +03:00
withPrepArgs :: StateT PrepArgs m a -> m (a, PrepArgs)
withPrepArgs m = runStateT m Seq.empty
prepareColVal
:: (MonadState PrepArgs m)
=> WithScalarType PGScalarValue -> m S.SQLExp
prepareColVal (WithScalarType scalarType colVal) = do
2018-06-27 16:11:32 +03:00
preparedArgs <- get
put (preparedArgs Seq.|> binEncoder colVal)
return $ toPrepParam (Seq.length preparedArgs + 1) scalarType
2018-06-27 16:11:32 +03:00
2019-04-17 12:48:41 +03:00
txtConverter :: Applicative f => AnnPGVal -> f S.SQLExp
txtConverter (AnnPGVal _ _ scalarValue) = pure $ toTxtValue scalarValue
2019-04-17 12:48:41 +03:00
withSelSet :: (Monad m) => SelSet -> (Field -> m a) -> m [(Text, a)]
withSelSet selSet f =
forM (toList selSet) $ \fld -> do
res <- f fld
return (G.unName $ G.unAlias $ _fAlias fld, res)
2019-04-17 12:48:41 +03:00
fieldAsPath :: (MonadError QErr m) => Field -> m a -> m a
fieldAsPath = nameAsPath . _fName
resolvePGCol :: (MonadError QErr m)
=> PGColGNameMap -> G.Name -> m PGColumnInfo
resolvePGCol colFldMap fldName =
onNothing (Map.lookup fldName colFldMap) $ throw500 $
"no column associated with name " <> G.unName fldName