graphql-engine/server/src-lib/Hasura/Backends/DataWrapper/API/V0/Expression.hs
David Overton 44577dab1b Add ToSchema instances to GDW API types
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/4006
Co-authored-by: Daniel Chambers <1214352+daniel-chambers@users.noreply.github.com>
GitOrigin-RevId: 756ca0ed60865d0eb675562e8959f0d1839f9abe
2022-03-31 04:46:08 +00:00

88 lines
3.2 KiB
Haskell

{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TemplateHaskell #-}
--
module Hasura.Backends.DataWrapper.API.V0.Expression
( Expression (..),
Operator (..),
)
where
import Autodocodec.Extended
import Autodocodec.OpenAPI ()
import Control.Lens.TH (makePrisms)
import Data.Aeson (FromJSON, ToJSON)
import Data.OpenApi (ToSchema)
import Hasura.Backends.DataWrapper.API.V0.Column qualified as API.V0
import Hasura.Backends.DataWrapper.API.V0.Scalar.Value qualified as API.V0.Scalar
import Hasura.Incremental (Cacheable)
import Hasura.Prelude
--------------------------------------------------------------------------------
-- | A serializable representation of query operators.
data Operator
= LessThan
| LessThanOrEqual
| GreaterThan
| GreaterThanOrEqual
deriving stock (Data, Eq, Generic, Ord, Show, Enum, Bounded)
deriving anyclass (Cacheable, Hashable, NFData)
deriving (FromJSON, ToJSON, ToSchema) via Autodocodec Operator
instance HasCodec Operator where
codec =
named "Operator" $
disjointStringConstCodec
[ (LessThan, "less_than"),
(LessThanOrEqual, "less_than_or_equal"),
(GreaterThan, "greater_than"),
(GreaterThanOrEqual, "greater_than_or_equal")
]
--------------------------------------------------------------------------------
-- | A serializable representation of query expressions.
data Expression
= -- | A constant 'Scalar.Value'.
Literal (ValueWrapper "value" API.V0.Scalar.Value)
| In (ValueWrapper2 "expression" Expression "values" [API.V0.Scalar.Value])
| And (ValueWrapper "expressions" [Expression])
| Or (ValueWrapper "expressions" [Expression])
| Not (ValueWrapper "expression" Expression)
| IsNull (ValueWrapper "expression" Expression)
| IsNotNull (ValueWrapper "expression" Expression)
| Column (ValueWrapper "column" API.V0.ColumnName)
| Equal (ValueWrapper2 "left" Expression "right" Expression)
| NotEqual (ValueWrapper2 "left" Expression "right" Expression)
| ApplyOperator (ValueWrapper3 "operator" Operator "left" Expression "right" Expression)
deriving stock (Data, Eq, Generic, Ord, Show)
deriving anyclass (Cacheable, Hashable, NFData)
$(makePrisms ''Expression)
instance HasCodec Expression where
codec =
named "Expression" $
sumTypeCodec
[ TypeAlternative "LiteralExpression" "literal" _Literal,
TypeAlternative "InExpression" "in" _In,
TypeAlternative "AndExpression" "and" _And,
TypeAlternative "OrExpression" "or" _Or,
TypeAlternative "NotExpression" "not" _Not,
TypeAlternative "IsNullExpression" "is_null" _IsNull,
TypeAlternative "IsNotNullExpression" "is_not_null" _IsNotNull,
TypeAlternative "ColumnExpression" "column" _Column,
TypeAlternative "EqualExpression" "equal" _Equal,
TypeAlternative "NotEqualExpression" "not_equal" _NotEqual,
TypeAlternative "ApplyOperatorExpression" "op" _ApplyOperator
]
deriving via Autodocodec Expression instance FromJSON Expression
deriving via Autodocodec Expression instance ToJSON Expression
deriving via Autodocodec Expression instance ToSchema Expression