2018-06-27 16:11:32 +03:00
|
|
|
module Hasura.GraphQL.Validate.InputValue
|
|
|
|
( validateInputValue
|
|
|
|
, jsonParser
|
|
|
|
, valueParser
|
|
|
|
, constValueParser
|
2019-01-28 15:38:38 +03:00
|
|
|
, pPrintValueC
|
2018-06-27 16:11:32 +03:00
|
|
|
) where
|
|
|
|
|
|
|
|
import Hasura.Prelude
|
|
|
|
|
|
|
|
import Data.Has
|
2020-05-27 18:02:58 +03:00
|
|
|
import Data.List.Extended (duplicates)
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
import qualified Data.Aeson as J
|
|
|
|
import qualified Data.HashMap.Strict as Map
|
2018-10-12 13:36:47 +03:00
|
|
|
import qualified Data.HashMap.Strict.InsOrd as OMap
|
2019-01-28 15:38:38 +03:00
|
|
|
import qualified Data.Text as T
|
2018-06-27 16:11:32 +03:00
|
|
|
import qualified Data.Vector as V
|
|
|
|
import qualified Language.GraphQL.Draft.Syntax as G
|
|
|
|
|
2019-07-22 15:47:13 +03:00
|
|
|
import qualified Hasura.RQL.Types as RQL
|
|
|
|
|
2018-06-27 16:11:32 +03:00
|
|
|
import Hasura.GraphQL.Utils
|
|
|
|
import Hasura.GraphQL.Validate.Context
|
|
|
|
import Hasura.GraphQL.Validate.Types
|
|
|
|
import Hasura.RQL.Types
|
|
|
|
import Hasura.SQL.Value
|
|
|
|
|
2019-03-20 09:31:49 +03:00
|
|
|
newtype P a = P { unP :: Maybe (Either (G.Variable, AnnInpVal) a)}
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
pNull :: (Monad m) => m (P a)
|
|
|
|
pNull = return $ P Nothing
|
|
|
|
|
|
|
|
pVal :: (Monad m) => a -> m (P a)
|
|
|
|
pVal = return . P . Just . Right
|
|
|
|
|
|
|
|
resolveVar
|
|
|
|
:: ( MonadError QErr m
|
|
|
|
, MonadReader ValidationCtx m)
|
2019-03-20 09:31:49 +03:00
|
|
|
=> G.Variable -> m AnnInpVal
|
2018-06-27 16:11:32 +03:00
|
|
|
resolveVar var = do
|
|
|
|
varVals <- _vcVarVals <$> ask
|
|
|
|
onNothing (Map.lookup var varVals) $
|
|
|
|
throwVE $ "no such variable defined in the operation: "
|
|
|
|
<> showName (G.unVariable var)
|
|
|
|
|
|
|
|
pVar
|
|
|
|
:: ( MonadError QErr m
|
|
|
|
, MonadReader ValidationCtx m)
|
|
|
|
=> G.Variable -> m (P a)
|
|
|
|
pVar var = do
|
|
|
|
annInpVal <- resolveVar var
|
2019-03-20 09:31:49 +03:00
|
|
|
return . P . Just $ Left (var, annInpVal)
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
data InputValueParser a m
|
|
|
|
= InputValueParser
|
|
|
|
{ getScalar :: a -> m (P J.Value)
|
|
|
|
, getList :: a -> m (P [a])
|
|
|
|
, getObject :: a -> m (P [(G.Name, a)])
|
|
|
|
, getEnum :: a -> m (P G.EnumValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonParser :: (MonadError QErr m) => InputValueParser J.Value m
|
|
|
|
jsonParser =
|
|
|
|
InputValueParser jScalar jList jObject jEnum
|
|
|
|
where
|
|
|
|
jEnum (J.String t) = pVal $ G.EnumValue $ G.Name t
|
|
|
|
jEnum J.Null = pNull
|
|
|
|
jEnum _ = throwVE "expecting a JSON string for Enum"
|
|
|
|
|
2018-06-29 07:47:23 +03:00
|
|
|
jList (J.Array l) = pVal $ V.toList l
|
2018-06-27 16:11:32 +03:00
|
|
|
jList J.Null = pNull
|
2018-06-29 07:47:23 +03:00
|
|
|
jList v = pVal [v]
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
jObject (J.Object m) = pVal [(G.Name t, v) | (t, v) <- Map.toList m]
|
|
|
|
jObject J.Null = pNull
|
|
|
|
jObject _ = throwVE "expecting a JSON object"
|
|
|
|
|
|
|
|
jScalar J.Null = pNull
|
|
|
|
jScalar v = pVal v
|
|
|
|
|
2018-12-04 17:21:58 +03:00
|
|
|
toJValue :: (MonadError QErr m) => G.Value -> m J.Value
|
|
|
|
toJValue = \case
|
|
|
|
G.VVariable _ ->
|
|
|
|
throwVE "variables are not allowed in scalars"
|
|
|
|
G.VInt i -> return $ J.toJSON i
|
|
|
|
G.VFloat f -> return $ J.toJSON f
|
|
|
|
G.VString (G.StringValue t) -> return $ J.toJSON t
|
|
|
|
G.VBoolean b -> return $ J.toJSON b
|
|
|
|
G.VNull -> return J.Null
|
|
|
|
G.VEnum (G.EnumValue n) -> return $ J.toJSON n
|
|
|
|
G.VList (G.ListValueG vals) ->
|
|
|
|
J.toJSON <$> mapM toJValue vals
|
|
|
|
G.VObject (G.ObjectValueG objs) ->
|
|
|
|
J.toJSON . Map.fromList <$> mapM toTup objs
|
|
|
|
where
|
|
|
|
toTup (G.ObjectFieldG f v) = (f,) <$> toJValue v
|
|
|
|
|
2018-06-27 16:11:32 +03:00
|
|
|
valueParser
|
|
|
|
:: ( MonadError QErr m
|
|
|
|
, MonadReader ValidationCtx m)
|
|
|
|
=> InputValueParser G.Value m
|
|
|
|
valueParser =
|
|
|
|
InputValueParser pScalar pList pObject pEnum
|
|
|
|
where
|
|
|
|
pEnum (G.VVariable var) = pVar var
|
|
|
|
pEnum (G.VEnum e) = pVal e
|
|
|
|
pEnum G.VNull = pNull
|
|
|
|
pEnum _ = throwVE "expecting an enum"
|
|
|
|
|
|
|
|
pList (G.VVariable var) = pVar var
|
|
|
|
pList (G.VList lv) = pVal $ G.unListValue lv
|
|
|
|
pList G.VNull = pNull
|
2018-06-29 07:47:23 +03:00
|
|
|
pList v = pVal [v]
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
pObject (G.VVariable var) = pVar var
|
|
|
|
pObject (G.VObject ov) = pVal
|
|
|
|
[(G._ofName oFld, G._ofValue oFld) | oFld <- G.unObjectValue ov]
|
|
|
|
pObject G.VNull = pNull
|
|
|
|
pObject _ = throwVE "expecting an object"
|
|
|
|
|
|
|
|
-- scalar json
|
|
|
|
pScalar (G.VVariable var) = pVar var
|
|
|
|
pScalar G.VNull = pNull
|
|
|
|
pScalar (G.VInt v) = pVal $ J.Number $ fromIntegral v
|
2020-04-21 16:56:15 +03:00
|
|
|
pScalar (G.VFloat v) = pVal $ J.Number v
|
2018-06-27 16:11:32 +03:00
|
|
|
pScalar (G.VBoolean b) = pVal $ J.Bool b
|
|
|
|
pScalar (G.VString sv) = pVal $ J.String $ G.unStringValue sv
|
|
|
|
pScalar (G.VEnum _) = throwVE "unexpected enum for a scalar"
|
2018-12-04 17:21:58 +03:00
|
|
|
pScalar v = pVal =<< toJValue v
|
|
|
|
|
2019-01-28 15:38:38 +03:00
|
|
|
pPrintValueC :: G.ValueConst -> Text
|
|
|
|
pPrintValueC = \case
|
|
|
|
G.VCInt i -> T.pack $ show i
|
|
|
|
G.VCFloat f -> T.pack $ show f
|
|
|
|
G.VCString (G.StringValue t) -> T.pack $ show t
|
|
|
|
G.VCBoolean b -> bool "false" "true" b
|
|
|
|
G.VCNull -> "null"
|
|
|
|
G.VCEnum (G.EnumValue n) -> G.unName n
|
|
|
|
G.VCList (G.ListValueG vals) -> withSquareBraces $ T.intercalate ", " $ map pPrintValueC vals
|
|
|
|
G.VCObject (G.ObjectValueG objs) -> withCurlyBraces $ T.intercalate ", " $ map ppObjFld objs
|
|
|
|
where
|
|
|
|
ppObjFld (G.ObjectFieldG f v) = G.unName f <> ": " <> pPrintValueC v
|
|
|
|
withSquareBraces t = "[" <> t <> "]"
|
|
|
|
withCurlyBraces t = "{" <> t <> "}"
|
|
|
|
|
|
|
|
|
2018-12-04 17:21:58 +03:00
|
|
|
toJValueC :: G.ValueConst -> J.Value
|
|
|
|
toJValueC = \case
|
|
|
|
G.VCInt i -> J.toJSON i
|
|
|
|
G.VCFloat f -> J.toJSON f
|
|
|
|
G.VCString (G.StringValue t) -> J.toJSON t
|
|
|
|
G.VCBoolean b -> J.toJSON b
|
|
|
|
G.VCNull -> J.Null
|
|
|
|
G.VCEnum (G.EnumValue n) -> J.toJSON n
|
|
|
|
G.VCList (G.ListValueG vals) ->
|
|
|
|
J.toJSON $ map toJValueC vals
|
|
|
|
G.VCObject (G.ObjectValueG objs) ->
|
2019-01-28 15:38:38 +03:00
|
|
|
J.toJSON . OMap.fromList $ map toTup objs
|
2018-12-04 17:21:58 +03:00
|
|
|
where
|
|
|
|
toTup (G.ObjectFieldG f v) = (f, toJValueC v)
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
constValueParser :: (MonadError QErr m) => InputValueParser G.ValueConst m
|
|
|
|
constValueParser =
|
|
|
|
InputValueParser pScalar pList pObject pEnum
|
|
|
|
where
|
|
|
|
pEnum (G.VCEnum e) = pVal e
|
|
|
|
pEnum G.VCNull = pNull
|
|
|
|
pEnum _ = throwVE "expecting an enum"
|
|
|
|
|
|
|
|
pList (G.VCList lv) = pVal $ G.unListValue lv
|
|
|
|
pList G.VCNull = pNull
|
2018-06-29 07:47:23 +03:00
|
|
|
pList v = pVal [v]
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
pObject (G.VCObject ov) = pVal
|
|
|
|
[(G._ofName oFld, G._ofValue oFld) | oFld <- G.unObjectValue ov]
|
|
|
|
pObject G.VCNull = pNull
|
|
|
|
pObject _ = throwVE "expecting an object"
|
|
|
|
|
|
|
|
-- scalar json
|
|
|
|
pScalar G.VCNull = pNull
|
|
|
|
pScalar (G.VCInt v) = pVal $ J.Number $ fromIntegral v
|
2020-04-21 16:56:15 +03:00
|
|
|
pScalar (G.VCFloat v) = pVal $ J.Number v
|
2018-06-27 16:11:32 +03:00
|
|
|
pScalar (G.VCBoolean b) = pVal $ J.Bool b
|
|
|
|
pScalar (G.VCString sv) = pVal $ J.String $ G.unStringValue sv
|
|
|
|
pScalar (G.VCEnum _) = throwVE "unexpected enum for a scalar"
|
2018-12-04 17:21:58 +03:00
|
|
|
pScalar v = pVal $ toJValueC v
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
validateObject
|
|
|
|
:: ( MonadReader r m, Has TypeMap r
|
|
|
|
, MonadError QErr m
|
|
|
|
)
|
|
|
|
=> InputValueParser a m
|
|
|
|
-> InpObjTyInfo -> [(G.Name, a)] -> m AnnGObject
|
|
|
|
validateObject valParser tyInfo flds = do
|
|
|
|
|
2018-10-12 13:36:47 +03:00
|
|
|
-- check duplicates
|
|
|
|
unless (null dups) $
|
2018-06-27 16:11:32 +03:00
|
|
|
throwVE $ "when parsing a value of type: " <> showNamedTy (_iotiName tyInfo)
|
|
|
|
<> ", the following fields are duplicated: "
|
2018-09-04 16:39:48 +03:00
|
|
|
<> showNames dups
|
|
|
|
|
2019-03-25 15:29:52 +03:00
|
|
|
-- make default values object
|
|
|
|
defValObj <- fmap (OMap.fromList . catMaybes) $
|
|
|
|
forM (Map.toList $ _iotiFields tyInfo) $
|
2018-09-04 16:39:48 +03:00
|
|
|
\(fldName, inpValInfo) -> do
|
2018-10-12 13:36:47 +03:00
|
|
|
let ty = _iviType inpValInfo
|
2018-09-04 16:39:48 +03:00
|
|
|
isNotNull = G.isNotNull ty
|
2019-03-25 15:29:52 +03:00
|
|
|
defValM = _iviDefVal inpValInfo
|
|
|
|
hasDefVal = isJust defValM
|
2018-10-12 13:36:47 +03:00
|
|
|
fldPresent = fldName `elem` inpFldNames
|
2018-09-04 16:39:48 +03:00
|
|
|
|
2019-03-25 15:29:52 +03:00
|
|
|
when (not fldPresent && isNotNull && not hasDefVal) $
|
|
|
|
throwVE $ "field " <> G.unName fldName <> " of type "
|
|
|
|
<> G.showGT ty <> " is required, but not found"
|
|
|
|
|
|
|
|
convDefValM <- validateInputValue constValueParser ty `mapM` defValM
|
|
|
|
return $ (fldName,) <$> convDefValM
|
|
|
|
|
|
|
|
-- compute input values object
|
|
|
|
inpValObj <- fmap OMap.fromList $ forM flds $ \(fldName, fldVal) ->
|
2018-10-12 13:36:47 +03:00
|
|
|
withPathK (G.unName fldName) $ do
|
|
|
|
fldTy <- getInpFieldInfo tyInfo fldName
|
|
|
|
convFldVal <- validateInputValue valParser fldTy fldVal
|
|
|
|
return (fldName, convFldVal)
|
|
|
|
|
2019-03-25 15:29:52 +03:00
|
|
|
return $ inpValObj `OMap.union` defValObj
|
|
|
|
|
2018-10-12 13:36:47 +03:00
|
|
|
where
|
|
|
|
inpFldNames = map fst flds
|
|
|
|
dups = duplicates inpFldNames
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
validateNamedTypeVal
|
|
|
|
:: ( MonadReader r m, Has TypeMap r
|
|
|
|
, MonadError QErr m)
|
|
|
|
=> InputValueParser a m
|
2019-03-20 09:31:49 +03:00
|
|
|
-> (G.Nullability, G.NamedType) -> a -> m AnnInpVal
|
|
|
|
validateNamedTypeVal inpValParser (nullability, nt) val = do
|
2018-06-27 16:11:32 +03:00
|
|
|
tyInfo <- getTyInfo nt
|
|
|
|
case tyInfo of
|
|
|
|
-- this should never happen
|
|
|
|
TIObj _ ->
|
2019-01-28 19:45:10 +03:00
|
|
|
throwUnexpTypeErr "object"
|
|
|
|
TIIFace _ ->
|
|
|
|
throwUnexpTypeErr "interface"
|
|
|
|
TIUnion _ ->
|
|
|
|
throwUnexpTypeErr "union"
|
2018-06-27 16:11:32 +03:00
|
|
|
TIInpObj ioti ->
|
2019-03-20 09:31:49 +03:00
|
|
|
withParsed gType (getObject inpValParser) val $
|
2018-09-04 16:39:48 +03:00
|
|
|
fmap (AGObject nt) . mapM (validateObject inpValParser ioti)
|
2018-06-27 16:11:32 +03:00
|
|
|
TIEnum eti ->
|
2019-03-20 09:31:49 +03:00
|
|
|
withParsed gType (getEnum inpValParser) val $
|
2019-07-22 15:47:13 +03:00
|
|
|
fmap (AGEnum nt) . validateEnum eti
|
2020-02-13 20:38:23 +03:00
|
|
|
TIScalar (ScalarTyInfo _ _ pgColTy _) ->
|
2019-03-20 09:31:49 +03:00
|
|
|
withParsed gType (getScalar inpValParser) val $
|
2018-09-04 16:39:48 +03:00
|
|
|
fmap (AGScalar pgColTy) . mapM (validateScalar pgColTy)
|
2018-06-27 16:11:32 +03:00
|
|
|
where
|
2019-01-28 19:45:10 +03:00
|
|
|
throwUnexpTypeErr ty = throw500 $ "unexpected " <> ty <> " type info for: "
|
|
|
|
<> showNamedTy nt
|
2019-07-22 15:47:13 +03:00
|
|
|
|
|
|
|
validateEnum enumTyInfo maybeEnumValue = case (_etiValues enumTyInfo, maybeEnumValue) of
|
|
|
|
(EnumValuesSynthetic _, Nothing) -> pure $ AGESynthetic Nothing
|
|
|
|
(EnumValuesReference reference, Nothing) -> pure $ AGEReference reference Nothing
|
|
|
|
(EnumValuesSynthetic values, Just enumValue)
|
|
|
|
| Map.member enumValue values -> pure $ AGESynthetic (Just enumValue)
|
|
|
|
(EnumValuesReference reference@(EnumReference _ values), Just enumValue)
|
|
|
|
| rqlEnumValue <- RQL.EnumValue . G.unName $ G.unEnumValue enumValue
|
|
|
|
, Map.member rqlEnumValue values
|
|
|
|
-> pure $ AGEReference reference (Just rqlEnumValue)
|
|
|
|
(_, Just enumValue) -> throwVE $
|
|
|
|
"unexpected value " <> showName (G.unEnumValue enumValue) <> " for enum: " <> showNamedTy nt
|
|
|
|
|
|
|
|
validateScalar pgColTy = runAesonParser (parsePGValue pgColTy)
|
2019-03-20 09:31:49 +03:00
|
|
|
gType = G.TypeNamed nullability nt
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
validateList
|
|
|
|
:: (MonadError QErr m, MonadReader r m, Has TypeMap r)
|
|
|
|
=> InputValueParser a m
|
2019-03-20 09:31:49 +03:00
|
|
|
-> (G.Nullability, G.ListType)
|
2018-06-27 16:11:32 +03:00
|
|
|
-> a
|
2019-03-20 09:31:49 +03:00
|
|
|
-> m AnnInpVal
|
|
|
|
validateList inpValParser (nullability, listTy) val =
|
|
|
|
withParsed ty (getList inpValParser) val $ \lM -> do
|
2018-06-27 16:11:32 +03:00
|
|
|
let baseTy = G.unListType listTy
|
2018-06-29 10:21:04 +03:00
|
|
|
AGArray listTy <$>
|
|
|
|
mapM (indexedMapM (validateInputValue inpValParser baseTy)) lM
|
2019-03-20 09:31:49 +03:00
|
|
|
where
|
|
|
|
ty = G.TypeList nullability listTy
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
validateInputValue
|
|
|
|
:: (MonadError QErr m, MonadReader r m, Has TypeMap r)
|
|
|
|
=> InputValueParser a m
|
|
|
|
-> G.GType
|
|
|
|
-> a
|
2019-03-20 09:31:49 +03:00
|
|
|
-> m AnnInpVal
|
2018-06-27 16:11:32 +03:00
|
|
|
validateInputValue inpValParser ty val =
|
|
|
|
case ty of
|
2019-03-20 09:31:49 +03:00
|
|
|
G.TypeNamed nullability nt ->
|
|
|
|
validateNamedTypeVal inpValParser (nullability, nt) val
|
|
|
|
G.TypeList nullability lt ->
|
|
|
|
validateList inpValParser (nullability, lt) val
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
withParsed
|
2019-03-20 09:31:49 +03:00
|
|
|
:: (Monad m, MonadError QErr m)
|
|
|
|
=> G.GType
|
|
|
|
-> (val -> m (P specificVal))
|
2018-06-27 16:11:32 +03:00
|
|
|
-> val
|
|
|
|
-> (Maybe specificVal -> m AnnGValue)
|
2019-03-20 09:31:49 +03:00
|
|
|
-> m AnnInpVal
|
|
|
|
withParsed expectedTy valParser val fn = do
|
2018-06-27 16:11:32 +03:00
|
|
|
parsedVal <- valParser val
|
|
|
|
case unP parsedVal of
|
2019-04-24 10:49:39 +03:00
|
|
|
Nothing ->
|
|
|
|
if G.isNullable expectedTy
|
|
|
|
then AnnInpVal expectedTy Nothing <$> fn Nothing
|
|
|
|
else throwVE $ "null value found for non-nullable type: "
|
|
|
|
<> G.showGT expectedTy
|
2019-03-20 09:31:49 +03:00
|
|
|
Just (Right v) -> AnnInpVal expectedTy Nothing <$> fn (Just v)
|
|
|
|
Just (Left (var, v)) -> do
|
|
|
|
let varTxt = G.unName $ G.unVariable var
|
|
|
|
unless (isTypeAllowed expectedTy $ _aivType v) $
|
|
|
|
throwVE $ "variable " <> varTxt
|
|
|
|
<> " of type " <> G.showGT (_aivType v)
|
|
|
|
<> " is used in position expecting " <> G.showGT expectedTy
|
|
|
|
return $ v { _aivVariable = Just var }
|
|
|
|
where
|
|
|
|
-- is the type 'ofType' allowed at a position of type 'atType'
|
|
|
|
-- Examples:
|
|
|
|
-- . a! is allowed at a
|
|
|
|
-- . [a!]! is allowed at [a]
|
|
|
|
-- . but 'a' is not allowed at 'a!'
|
|
|
|
isTypeAllowed ofType atType =
|
|
|
|
case (ofType, atType) of
|
|
|
|
(G.TypeNamed ofTyN ofNt, G.TypeNamed atTyN atNt) ->
|
|
|
|
checkNullability ofTyN atTyN && (ofNt == atNt)
|
|
|
|
(G.TypeList ofTyN ofLt, G.TypeList atTyN atLt) ->
|
|
|
|
checkNullability ofTyN atTyN &&
|
|
|
|
isTypeAllowed (G.unListType ofLt) (G.unListType atLt)
|
|
|
|
_ -> False
|
|
|
|
|
|
|
|
-- only when 'atType' is non nullable and 'ofType' is nullable,
|
|
|
|
-- this check fails
|
|
|
|
checkNullability (G.Nullability ofNullable) (G.Nullability atNullable) =
|
|
|
|
case (ofNullable, atNullable) of
|
|
|
|
(True, _) -> True
|
|
|
|
(False, False) -> True
|
|
|
|
(False, True) -> False
|