2022-04-08 09:48:37 +03:00
|
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
|
|
|
|
2022-05-02 08:03:12 +03:00
|
|
|
module Hasura.Backends.DataConnector.Plan
|
2022-04-08 09:48:37 +03:00
|
|
|
( SourceConfig (..),
|
|
|
|
Plan (..),
|
|
|
|
mkPlan,
|
2022-04-14 05:06:07 +03:00
|
|
|
renderPlan,
|
2022-04-08 09:48:37 +03:00
|
|
|
queryHasRelations,
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
import Data.Aeson qualified as J
|
|
|
|
import Data.Align
|
2022-04-14 05:06:07 +03:00
|
|
|
import Data.ByteString.Lazy qualified as BL
|
2022-04-08 09:48:37 +03:00
|
|
|
import Data.HashMap.Strict qualified as HashMap
|
|
|
|
import Data.List.NonEmpty qualified as NE
|
|
|
|
import Data.Semigroup (Any (..), Min (..))
|
2022-04-28 04:51:58 +03:00
|
|
|
import Data.Text as T
|
2022-04-14 05:06:07 +03:00
|
|
|
import Data.Text.Encoding qualified as TE
|
2022-06-02 05:06:45 +03:00
|
|
|
import Data.Text.Extended ((<>>))
|
2022-04-08 09:48:37 +03:00
|
|
|
import Data.These
|
2022-04-28 04:51:58 +03:00
|
|
|
import Data.Vector qualified as V
|
2022-05-02 08:03:12 +03:00
|
|
|
import Hasura.Backends.DataConnector.API qualified as API
|
|
|
|
import Hasura.Backends.DataConnector.Adapter.Types
|
2022-06-02 05:06:45 +03:00
|
|
|
import Hasura.Backends.DataConnector.IR.Column qualified as IR.C
|
2022-05-02 08:03:12 +03:00
|
|
|
import Hasura.Backends.DataConnector.IR.Export qualified as IR
|
|
|
|
import Hasura.Backends.DataConnector.IR.Expression qualified as IR.E
|
|
|
|
import Hasura.Backends.DataConnector.IR.OrderBy qualified as IR.O
|
|
|
|
import Hasura.Backends.DataConnector.IR.Query qualified as IR.Q
|
|
|
|
import Hasura.Backends.DataConnector.IR.Scalar.Value qualified as IR.S
|
2022-04-08 09:48:37 +03:00
|
|
|
import Hasura.Base.Error
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.IR.BoolExp
|
|
|
|
import Hasura.RQL.IR.OrderBy
|
|
|
|
import Hasura.RQL.IR.Select
|
2022-05-31 01:07:02 +03:00
|
|
|
import Hasura.RQL.IR.Value
|
2022-04-08 09:48:37 +03:00
|
|
|
import Hasura.RQL.Types.Column
|
|
|
|
import Hasura.RQL.Types.Common
|
|
|
|
import Hasura.SQL.Backend
|
|
|
|
import Hasura.Session
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2022-04-28 04:51:58 +03:00
|
|
|
-- | A 'Plan' consists of an 'IR.Q' describing the query to be
|
2022-04-08 09:48:37 +03:00
|
|
|
-- performed by the Agent and a continuation for post processing the
|
|
|
|
-- response. See the 'postProcessResponseRow' haddock for more
|
|
|
|
-- information on why we need a post-processor.
|
|
|
|
data Plan = Plan
|
2022-04-28 04:51:58 +03:00
|
|
|
{ query :: IR.Q.Query,
|
|
|
|
postProcessor :: (API.QueryResponse -> Either ResponseError API.QueryResponse)
|
2022-04-08 09:48:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
-- | Error type for the postProcessor continuation. Failure can occur if the Agent
|
|
|
|
-- returns bad data.
|
|
|
|
data ResponseError
|
|
|
|
= RequiredFieldMissing
|
|
|
|
| UnexpectedFields
|
|
|
|
| ExpectedObject
|
|
|
|
| ExpectedArray
|
|
|
|
| UnexpectedResponseCardinality
|
|
|
|
deriving (Show, Eq)
|
|
|
|
|
2022-04-28 04:51:58 +03:00
|
|
|
-- | Extract the 'IR.Q' from a 'Plan' and render it as 'Text'.
|
2022-04-14 05:06:07 +03:00
|
|
|
--
|
|
|
|
-- NOTE: This is for logging and debug purposes only.
|
|
|
|
renderPlan :: Plan -> Text
|
|
|
|
renderPlan =
|
2022-04-28 04:51:58 +03:00
|
|
|
TE.decodeUtf8 . BL.toStrict . J.encode . IR.queryToAPI . query
|
2022-04-14 05:06:07 +03:00
|
|
|
|
2022-05-02 08:03:12 +03:00
|
|
|
-- | Map a 'QueryDB 'DataConnector' term into a 'Plan'
|
2022-04-08 09:48:37 +03:00
|
|
|
mkPlan ::
|
|
|
|
forall m.
|
|
|
|
MonadError QErr m =>
|
|
|
|
SessionVariables ->
|
|
|
|
SourceConfig ->
|
2022-05-02 08:03:12 +03:00
|
|
|
QueryDB 'DataConnector Void (UnpreparedValue 'DataConnector) ->
|
2022-04-08 09:48:37 +03:00
|
|
|
m Plan
|
2022-05-02 02:01:11 +03:00
|
|
|
mkPlan session (SourceConfig {_scSchema = API.SchemaResponse {..}}) ir = translateQueryDB ir
|
2022-04-08 09:48:37 +03:00
|
|
|
where
|
|
|
|
translateQueryDB ::
|
2022-05-02 08:03:12 +03:00
|
|
|
QueryDB 'DataConnector Void (UnpreparedValue 'DataConnector) ->
|
2022-04-08 09:48:37 +03:00
|
|
|
m Plan
|
|
|
|
translateQueryDB =
|
2022-06-02 05:06:45 +03:00
|
|
|
\case
|
2022-04-08 09:48:37 +03:00
|
|
|
QDBMultipleRows s -> do
|
2022-04-28 04:51:58 +03:00
|
|
|
query <- translateAnnSelect IR.Q.Many s
|
2022-04-08 09:48:37 +03:00
|
|
|
pure $
|
2022-04-28 04:51:58 +03:00
|
|
|
Plan query $ \API.QueryResponse {getQueryResponse = response} ->
|
|
|
|
fmap API.QueryResponse $ traverse (postProcessResponseRow srCapabilities query) response
|
2022-04-08 09:48:37 +03:00
|
|
|
QDBSingleRow s -> do
|
2022-04-28 04:51:58 +03:00
|
|
|
query <- translateAnnSelect IR.Q.OneOrZero s
|
2022-04-08 09:48:37 +03:00
|
|
|
pure $
|
2022-04-28 04:51:58 +03:00
|
|
|
Plan query $ \API.QueryResponse {getQueryResponse = response} ->
|
|
|
|
fmap API.QueryResponse $ traverse (postProcessResponseRow srCapabilities query) response
|
2022-04-08 09:48:37 +03:00
|
|
|
QDBAggregation {} -> throw400 NotSupported "QDBAggregation: not supported"
|
|
|
|
|
|
|
|
translateAnnSelect ::
|
2022-04-28 04:51:58 +03:00
|
|
|
IR.Q.Cardinality ->
|
2022-06-02 05:06:45 +03:00
|
|
|
AnnSimpleSelectG 'DataConnector Void (UnpreparedValue 'DataConnector) ->
|
2022-04-28 04:51:58 +03:00
|
|
|
m IR.Q.Query
|
2022-06-02 05:06:45 +03:00
|
|
|
translateAnnSelect card selectG = do
|
|
|
|
tableName <- case _asnFrom selectG of
|
2022-04-08 09:48:37 +03:00
|
|
|
FromTable tn -> pure tn
|
2022-06-02 05:06:45 +03:00
|
|
|
FromIdentifier _ -> throw400 NotSupported "AnnSelectG: FromIdentifier not supported"
|
|
|
|
FromFunction {} -> throw400 NotSupported "AnnSelectG: FromFunction not supported"
|
|
|
|
fields <- translateFields card (_asnFields selectG)
|
2022-04-08 09:48:37 +03:00
|
|
|
let whereClauseWithPermissions =
|
2022-06-02 05:06:45 +03:00
|
|
|
case _saWhere (_asnArgs selectG) of
|
|
|
|
Just expr -> BoolAnd [expr, _tpFilter (_asnPerm selectG)]
|
|
|
|
Nothing -> _tpFilter (_asnPerm selectG)
|
2022-04-08 09:48:37 +03:00
|
|
|
whereClause <- translateBoolExp whereClauseWithPermissions
|
2022-06-02 05:06:45 +03:00
|
|
|
orderBy <- translateOrderBy (_saOrderBy $ _asnArgs selectG)
|
2022-04-08 09:48:37 +03:00
|
|
|
pure
|
2022-04-28 04:51:58 +03:00
|
|
|
IR.Q.Query
|
2022-04-08 09:48:37 +03:00
|
|
|
{ from = tableName,
|
|
|
|
fields = fields,
|
|
|
|
limit =
|
|
|
|
fmap getMin $
|
|
|
|
foldMap
|
|
|
|
(fmap Min)
|
2022-06-02 05:06:45 +03:00
|
|
|
[ _saLimit (_asnArgs selectG),
|
|
|
|
_tpLimit (_asnPerm selectG)
|
2022-04-08 09:48:37 +03:00
|
|
|
],
|
2022-06-02 05:06:45 +03:00
|
|
|
offset = fmap fromIntegral (_saOffset (_asnArgs selectG)),
|
2022-04-08 09:48:37 +03:00
|
|
|
where_ = Just whereClause,
|
|
|
|
orderBy = orderBy,
|
|
|
|
cardinality = card
|
|
|
|
}
|
|
|
|
|
|
|
|
translateOrderBy ::
|
2022-06-02 05:06:45 +03:00
|
|
|
Maybe (NE.NonEmpty (AnnotatedOrderByItemG 'DataConnector (UnpreparedValue 'DataConnector))) ->
|
2022-04-28 04:51:58 +03:00
|
|
|
m [IR.O.OrderBy]
|
2022-04-08 09:48:37 +03:00
|
|
|
translateOrderBy = \case
|
|
|
|
Nothing -> pure []
|
|
|
|
Just orderBys ->
|
|
|
|
do
|
|
|
|
NE.toList
|
|
|
|
<$> for orderBys \OrderByItemG {..} -> case obiColumn of
|
|
|
|
AOCColumn (ColumnInfo {ciColumn = dynColumnName}) ->
|
|
|
|
pure
|
2022-04-28 04:51:58 +03:00
|
|
|
IR.O.OrderBy
|
2022-04-08 09:48:37 +03:00
|
|
|
{ column = dynColumnName,
|
|
|
|
-- NOTE: Picking a default ordering.
|
2022-04-28 04:51:58 +03:00
|
|
|
ordering = fromMaybe IR.O.Ascending obiType
|
2022-04-08 09:48:37 +03:00
|
|
|
}
|
2022-06-02 05:06:45 +03:00
|
|
|
AOCObjectRelation {} ->
|
|
|
|
throw400 NotSupported "translateOrderBy: AOCObjectRelation unsupported in the Data Connector backend"
|
|
|
|
AOCArrayAggregation {} ->
|
|
|
|
throw400 NotSupported "translateOrderBy: AOCArrayAggregation unsupported in the Data Connector backend"
|
2022-04-08 09:48:37 +03:00
|
|
|
|
|
|
|
translateFields ::
|
2022-04-28 04:51:58 +03:00
|
|
|
IR.Q.Cardinality ->
|
2022-06-02 05:06:45 +03:00
|
|
|
AnnFieldsG 'DataConnector Void (UnpreparedValue 'DataConnector) ->
|
2022-04-28 04:51:58 +03:00
|
|
|
m (HashMap Text IR.Q.Field)
|
2022-06-02 05:06:45 +03:00
|
|
|
translateFields cardinality fields = do
|
|
|
|
translatedFields <- traverse (traverse (translateField cardinality)) fields
|
2022-04-08 09:48:37 +03:00
|
|
|
pure $
|
|
|
|
HashMap.fromList $
|
2022-04-14 05:06:07 +03:00
|
|
|
mapMaybe
|
|
|
|
sequence
|
2022-06-02 05:06:45 +03:00
|
|
|
[(getFieldNameTxt f, field) | (f, field) <- translatedFields]
|
2022-04-08 09:48:37 +03:00
|
|
|
|
|
|
|
translateField ::
|
2022-04-28 04:51:58 +03:00
|
|
|
IR.Q.Cardinality ->
|
2022-06-02 05:06:45 +03:00
|
|
|
AnnFieldG 'DataConnector Void (UnpreparedValue 'DataConnector) ->
|
2022-04-28 04:51:58 +03:00
|
|
|
m (Maybe IR.Q.Field)
|
2022-06-02 05:06:45 +03:00
|
|
|
translateField cardinality = \case
|
|
|
|
AFColumn colField ->
|
|
|
|
-- TODO: make sure certain fields in colField are not in use, since we don't
|
|
|
|
-- support them
|
|
|
|
pure $ Just $ IR.Q.Column (IR.Q.ColumnContents $ _acfColumn colField)
|
|
|
|
AFObjectRelation objRel -> do
|
|
|
|
fields <- translateFields cardinality (_aosFields (_aarAnnSelect objRel))
|
|
|
|
whereClause <- translateBoolExp (_aosTableFilter (_aarAnnSelect objRel))
|
|
|
|
pure . Just . IR.Q.Relationship $
|
|
|
|
IR.Q.RelationshipContents
|
|
|
|
(HashMap.mapKeys IR.Q.PrimaryKey . fmap IR.Q.ForeignKey $ _aarColumnMapping objRel)
|
|
|
|
( IR.Q.Query
|
|
|
|
{ fields = fields,
|
|
|
|
from = _aosTableFrom (_aarAnnSelect objRel),
|
|
|
|
where_ = Just whereClause,
|
|
|
|
limit = Nothing,
|
|
|
|
offset = Nothing,
|
|
|
|
orderBy = [],
|
|
|
|
cardinality = cardinality
|
|
|
|
}
|
|
|
|
)
|
|
|
|
AFArrayRelation (ASSimple arrRel) -> do
|
|
|
|
query <- translateAnnSelect IR.Q.Many (_aarAnnSelect arrRel)
|
|
|
|
pure . Just . IR.Q.Relationship $
|
|
|
|
IR.Q.RelationshipContents
|
|
|
|
(HashMap.mapKeys IR.Q.PrimaryKey $ fmap IR.Q.ForeignKey $ _aarColumnMapping arrRel)
|
|
|
|
query
|
|
|
|
AFArrayRelation (ASAggregate _) ->
|
|
|
|
throw400 NotSupported "translateField: AFArrayRelation ASAggregate not supported"
|
|
|
|
AFExpression _literal ->
|
|
|
|
pure Nothing
|
|
|
|
|
|
|
|
prepareLiterals ::
|
2022-05-02 08:03:12 +03:00
|
|
|
UnpreparedValue 'DataConnector ->
|
2022-06-02 05:06:45 +03:00
|
|
|
m IR.S.Literal
|
|
|
|
prepareLiterals (UVLiteral literal) = pure $ literal
|
|
|
|
prepareLiterals (UVParameter _ e) = pure (IR.S.ValueLiteral (cvValue e))
|
|
|
|
prepareLiterals UVSession = throw400 NotSupported "prepareLiterals: UVSession"
|
|
|
|
prepareLiterals (UVSessionVar _ v) =
|
2022-04-08 09:48:37 +03:00
|
|
|
case getSessionVariableValue v session of
|
2022-06-02 05:06:45 +03:00
|
|
|
Nothing -> throw400 NotSupported ("prepareLiterals: session var not found: " <>> v)
|
|
|
|
Just s -> pure (IR.S.ValueLiteral (IR.S.String s))
|
2022-04-08 09:48:37 +03:00
|
|
|
|
|
|
|
translateBoolExp ::
|
2022-06-02 05:06:45 +03:00
|
|
|
AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector) ->
|
2022-04-28 04:51:58 +03:00
|
|
|
m IR.E.Expression
|
2022-06-02 05:06:45 +03:00
|
|
|
translateBoolExp = \case
|
|
|
|
BoolAnd xs ->
|
|
|
|
IR.E.And <$> traverse (translateBoolExp) xs
|
|
|
|
BoolOr xs ->
|
|
|
|
IR.E.Or <$> traverse (translateBoolExp) xs
|
|
|
|
BoolNot x ->
|
|
|
|
IR.E.Not <$> (translateBoolExp) x
|
|
|
|
BoolFld (AVColumn c xs) ->
|
|
|
|
IR.E.And
|
|
|
|
<$> sequence
|
|
|
|
[translateOp (ciColumn c) x | x <- xs]
|
|
|
|
BoolFld (AVRelationship _ _) ->
|
|
|
|
throw400 NotSupported "The BoolFld AVRelationship expression type is not supported by the Data Connector backend"
|
|
|
|
BoolExists _ ->
|
|
|
|
throw400 NotSupported "The BoolExists expression type is not supported by the Data Connector backend"
|
2022-04-08 09:48:37 +03:00
|
|
|
|
|
|
|
translateOp ::
|
2022-06-02 05:06:45 +03:00
|
|
|
IR.C.Name ->
|
|
|
|
OpExpG 'DataConnector (UnpreparedValue 'DataConnector) ->
|
2022-04-28 04:51:58 +03:00
|
|
|
m IR.E.Expression
|
2022-06-02 05:06:45 +03:00
|
|
|
translateOp columnName opExp = do
|
|
|
|
preparedOpExp <- traverse prepareLiterals $ opExp
|
|
|
|
case preparedOpExp of
|
|
|
|
AEQ _ (IR.S.ValueLiteral value) ->
|
|
|
|
pure $ mkApplyBinaryComparisonOperatorToScalar IR.E.Equal value
|
|
|
|
AEQ _ (IR.S.ArrayLiteral _array) ->
|
|
|
|
throw400 NotSupported "Array literals not supported for AEQ operator"
|
|
|
|
ANE _ (IR.S.ValueLiteral value) ->
|
|
|
|
pure . IR.E.Not $ mkApplyBinaryComparisonOperatorToScalar IR.E.Equal value
|
|
|
|
ANE _ (IR.S.ArrayLiteral _array) ->
|
|
|
|
throw400 NotSupported "Array literals not supported for ANE operator"
|
|
|
|
AGT (IR.S.ValueLiteral value) ->
|
|
|
|
pure $ mkApplyBinaryComparisonOperatorToScalar IR.E.GreaterThan value
|
|
|
|
AGT (IR.S.ArrayLiteral _array) ->
|
|
|
|
throw400 NotSupported "Array literals not supported for AGT operator"
|
|
|
|
ALT (IR.S.ValueLiteral value) ->
|
|
|
|
pure $ mkApplyBinaryComparisonOperatorToScalar IR.E.LessThan value
|
|
|
|
ALT (IR.S.ArrayLiteral _array) ->
|
|
|
|
throw400 NotSupported "Array literals not supported for ALT operator"
|
|
|
|
AGTE (IR.S.ValueLiteral value) ->
|
|
|
|
pure $ mkApplyBinaryComparisonOperatorToScalar IR.E.GreaterThanOrEqual value
|
|
|
|
AGTE (IR.S.ArrayLiteral _array) ->
|
|
|
|
throw400 NotSupported "Array literals not supported for AGTE operator"
|
|
|
|
ALTE (IR.S.ValueLiteral value) ->
|
|
|
|
pure $ mkApplyBinaryComparisonOperatorToScalar IR.E.LessThanOrEqual value
|
|
|
|
ALTE (IR.S.ArrayLiteral _array) ->
|
|
|
|
throw400 NotSupported "Array literals not supported for ALTE operator"
|
|
|
|
ANISNULL ->
|
|
|
|
pure $ IR.E.ApplyUnaryComparisonOperator IR.E.IsNull columnName
|
|
|
|
ANISNOTNULL ->
|
|
|
|
pure $ IR.E.Not (IR.E.ApplyUnaryComparisonOperator IR.E.IsNull columnName)
|
|
|
|
AIN literal -> pure $ inOperator literal
|
|
|
|
ANIN literal -> pure . IR.E.Not $ inOperator literal
|
|
|
|
CEQ rootOrCurrentColumn ->
|
|
|
|
mkApplyBinaryComparisonOperatorToAnotherColumn IR.E.Equal rootOrCurrentColumn
|
|
|
|
CNE rootOrCurrentColumn ->
|
|
|
|
IR.E.Not <$> mkApplyBinaryComparisonOperatorToAnotherColumn IR.E.Equal rootOrCurrentColumn
|
|
|
|
CGT rootOrCurrentColumn ->
|
|
|
|
mkApplyBinaryComparisonOperatorToAnotherColumn IR.E.GreaterThan rootOrCurrentColumn
|
|
|
|
CLT rootOrCurrentColumn ->
|
|
|
|
mkApplyBinaryComparisonOperatorToAnotherColumn IR.E.LessThan rootOrCurrentColumn
|
|
|
|
CGTE rootOrCurrentColumn ->
|
|
|
|
mkApplyBinaryComparisonOperatorToAnotherColumn IR.E.GreaterThanOrEqual rootOrCurrentColumn
|
|
|
|
CLTE rootOrCurrentColumn ->
|
|
|
|
mkApplyBinaryComparisonOperatorToAnotherColumn IR.E.LessThanOrEqual rootOrCurrentColumn
|
|
|
|
ALIKE _literal ->
|
|
|
|
throw400 NotSupported "The ALIKE operator is not supported by the Data Connector backend"
|
|
|
|
ANLIKE _literal ->
|
|
|
|
throw400 NotSupported "The ANLIKE operator is not supported by the Data Connector backend"
|
|
|
|
ACast _literal ->
|
|
|
|
throw400 NotSupported "The ACast operator is not supported by the Data Connector backend"
|
|
|
|
where
|
|
|
|
mkApplyBinaryComparisonOperatorToAnotherColumn :: IR.E.BinaryComparisonOperator -> RootOrCurrentColumn 'DataConnector -> m IR.E.Expression
|
|
|
|
mkApplyBinaryComparisonOperatorToAnotherColumn operator (RootOrCurrentColumn rootOrCurrent otherColumnName) = do
|
|
|
|
case rootOrCurrent of
|
|
|
|
IsRoot -> throw400 NotSupported "Comparing columns on the root table in a BoolExp is not supported by the Data Connector backend"
|
|
|
|
IsCurrent -> pure $ IR.E.ApplyBinaryComparisonOperator operator columnName (IR.E.AnotherColumn otherColumnName)
|
|
|
|
|
|
|
|
inOperator :: IR.S.Literal -> IR.E.Expression
|
|
|
|
inOperator literal =
|
|
|
|
let values = case literal of
|
|
|
|
IR.S.ArrayLiteral array -> IR.E.ScalarValue <$> array
|
|
|
|
IR.S.ValueLiteral value -> [IR.E.ScalarValue value]
|
|
|
|
in IR.E.ApplyBinaryArrayComparisonOperator IR.E.In columnName values
|
|
|
|
|
|
|
|
mkApplyBinaryComparisonOperatorToScalar :: IR.E.BinaryComparisonOperator -> IR.S.Value -> IR.E.Expression
|
|
|
|
mkApplyBinaryComparisonOperatorToScalar operator value =
|
|
|
|
IR.E.ApplyBinaryComparisonOperator operator columnName (IR.E.ScalarValue value)
|
2022-04-08 09:48:37 +03:00
|
|
|
|
|
|
|
-- | We need to modify any JSON substructures which appear as a result
|
|
|
|
-- of fetching object relationships, to peel off the outer array sent
|
|
|
|
-- by the backend.
|
|
|
|
--
|
|
|
|
-- This function takes a response object, and the 'Plan' used to
|
|
|
|
-- fetch it, and modifies any such arrays accordingly.
|
|
|
|
postProcessResponseRow ::
|
2022-04-28 04:51:58 +03:00
|
|
|
API.Capabilities ->
|
|
|
|
IR.Q.Query ->
|
2022-04-08 09:48:37 +03:00
|
|
|
J.Object ->
|
|
|
|
Either ResponseError J.Object
|
2022-04-28 04:51:58 +03:00
|
|
|
postProcessResponseRow capabilities IR.Q.Query {fields} row =
|
2022-04-08 09:48:37 +03:00
|
|
|
sequenceA $ alignWith go fields row
|
|
|
|
where
|
2022-04-28 04:51:58 +03:00
|
|
|
go :: These IR.Q.Field J.Value -> Either ResponseError J.Value
|
2022-04-08 09:48:37 +03:00
|
|
|
go (This field) =
|
|
|
|
case field of
|
2022-04-28 04:51:58 +03:00
|
|
|
IR.Q.Literal literal ->
|
2022-04-08 09:48:37 +03:00
|
|
|
pure (J.String literal)
|
|
|
|
_ ->
|
|
|
|
Left RequiredFieldMissing
|
|
|
|
go That {} =
|
|
|
|
Left UnexpectedFields
|
|
|
|
go (These field value) =
|
|
|
|
case field of
|
2022-04-28 04:51:58 +03:00
|
|
|
IR.Q.Literal {} ->
|
2022-04-08 09:48:37 +03:00
|
|
|
Left UnexpectedFields
|
2022-04-28 04:51:58 +03:00
|
|
|
IR.Q.Column {} ->
|
2022-04-08 09:48:37 +03:00
|
|
|
pure value
|
2022-04-28 04:51:58 +03:00
|
|
|
IR.Q.Relationship (IR.Q.RelationshipContents _ subquery@IR.Q.Query {cardinality}) ->
|
2022-04-08 09:48:37 +03:00
|
|
|
case value of
|
|
|
|
J.Array rows -> do
|
|
|
|
processed <- traverse (postProcessResponseRow capabilities subquery <=< parseObject) (toList rows)
|
|
|
|
applyCardinalityToResponse cardinality processed
|
|
|
|
other
|
2022-04-28 04:51:58 +03:00
|
|
|
| API.dcRelationships capabilities ->
|
2022-04-08 09:48:37 +03:00
|
|
|
Left ExpectedArray
|
|
|
|
| otherwise ->
|
|
|
|
pure other
|
|
|
|
|
|
|
|
parseObject :: J.Value -> Either ResponseError J.Object
|
|
|
|
parseObject = \case
|
|
|
|
J.Object obj -> pure obj
|
|
|
|
_ -> Left ExpectedObject
|
|
|
|
|
|
|
|
-- | If a fk-to-pk lookup comes from an object relationship then we
|
|
|
|
-- expect 0 or 1 items in the response and we should return it as an object.
|
|
|
|
-- if it's an array, we have to send back all of the results
|
2022-04-28 04:51:58 +03:00
|
|
|
applyCardinalityToResponse :: IR.Q.Cardinality -> [J.Object] -> Either ResponseError J.Value
|
|
|
|
applyCardinalityToResponse IR.Q.OneOrZero = \case
|
2022-04-08 09:48:37 +03:00
|
|
|
[] -> pure J.Null
|
|
|
|
[x] -> pure $ J.Object x
|
|
|
|
_ -> Left UnexpectedResponseCardinality
|
2022-04-28 04:51:58 +03:00
|
|
|
applyCardinalityToResponse IR.Q.Many =
|
|
|
|
pure . J.Array . V.fromList . fmap J.Object
|
2022-04-08 09:48:37 +03:00
|
|
|
|
2022-04-28 04:51:58 +03:00
|
|
|
-- | Validate if a 'IR.Q' contains any relationships.
|
|
|
|
queryHasRelations :: IR.Q.Query -> Bool
|
|
|
|
queryHasRelations IR.Q.Query {fields} = getAny $ flip foldMap fields \case
|
|
|
|
IR.Q.Relationship _ -> Any True
|
2022-04-08 09:48:37 +03:00
|
|
|
_ -> Any False
|