2023-01-31 15:52:26 +03:00
|
|
|
{-# LANGUAGE DeriveAnyClass #-}
|
2023-01-30 19:04:56 +03:00
|
|
|
{-# LANGUAGE UndecidableInstances #-}
|
|
|
|
|
2023-02-22 16:45:27 +03:00
|
|
|
-- | Metadata representation of a logical model in the metadata,
|
|
|
|
-- as well as a parser and prettyprinter for the query code.
|
2023-02-22 12:22:22 +03:00
|
|
|
module Hasura.LogicalModel.Metadata
|
2023-02-21 16:45:12 +03:00
|
|
|
( LogicalModelName (..),
|
2023-02-22 16:45:27 +03:00
|
|
|
LogicalModelInfo (..),
|
|
|
|
LogicalModelArgumentName (..),
|
2023-01-31 15:52:26 +03:00
|
|
|
InterpolatedItem (..),
|
|
|
|
InterpolatedQuery (..),
|
|
|
|
parseInterpolatedQuery,
|
2023-02-22 12:22:22 +03:00
|
|
|
module Hasura.LogicalModel.Types,
|
2023-01-19 14:25:52 +03:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
2023-01-30 19:04:56 +03:00
|
|
|
import Autodocodec
|
|
|
|
import Autodocodec qualified as AC
|
|
|
|
import Data.Aeson
|
2023-02-01 11:44:50 +03:00
|
|
|
import Data.Bifunctor (first)
|
2023-01-31 15:52:26 +03:00
|
|
|
import Data.Text qualified as T
|
2023-02-15 20:55:06 +03:00
|
|
|
import Hasura.CustomReturnType (CustomReturnType)
|
2023-02-22 12:22:22 +03:00
|
|
|
import Hasura.LogicalModel.Types
|
2023-01-30 19:04:56 +03:00
|
|
|
import Hasura.Metadata.DTO.Utils (codecNamePrefix)
|
2023-02-01 11:44:50 +03:00
|
|
|
import Hasura.Prelude hiding (first)
|
2023-01-19 14:25:52 +03:00
|
|
|
import Hasura.RQL.Types.Backend
|
2023-01-30 19:04:56 +03:00
|
|
|
import Hasura.SQL.Backend
|
2023-01-19 14:25:52 +03:00
|
|
|
|
2023-01-31 15:52:26 +03:00
|
|
|
newtype RawQuery = RawQuery {getRawQuery :: Text}
|
|
|
|
deriving newtype (Eq, Ord, Show, FromJSON, ToJSON)
|
|
|
|
|
|
|
|
instance HasCodec RawQuery where
|
2023-02-02 13:53:25 +03:00
|
|
|
codec = AC.dimapCodec RawQuery getRawQuery codec
|
2023-01-31 15:52:26 +03:00
|
|
|
|
|
|
|
---------------------------------------
|
|
|
|
|
2023-02-01 11:44:50 +03:00
|
|
|
-- | A component of an interpolated query
|
2023-01-31 15:52:26 +03:00
|
|
|
data InterpolatedItem variable
|
2023-02-01 11:44:50 +03:00
|
|
|
= -- | normal text
|
|
|
|
IIText Text
|
|
|
|
| -- | a captured variable
|
|
|
|
IIVariable variable
|
2023-02-28 14:17:08 +03:00
|
|
|
deriving stock (Eq, Ord, Show, Functor, Foldable, Data, Generic, Traversable)
|
2023-01-31 15:52:26 +03:00
|
|
|
|
2023-02-01 11:44:50 +03:00
|
|
|
-- | Converting an interpolated query back to text.
|
|
|
|
-- Should roundtrip with the 'parseInterpolatedQuery'.
|
2023-02-22 16:45:27 +03:00
|
|
|
ppInterpolatedItem :: InterpolatedItem LogicalModelArgumentName -> Text
|
2023-02-01 11:44:50 +03:00
|
|
|
ppInterpolatedItem (IIText t) = t
|
2023-02-22 16:45:27 +03:00
|
|
|
ppInterpolatedItem (IIVariable v) = "{{" <> getLogicalModelArgumentName v <> "}}"
|
2023-02-01 11:44:50 +03:00
|
|
|
|
2023-01-31 15:52:26 +03:00
|
|
|
deriving instance (Hashable variable) => Hashable (InterpolatedItem variable)
|
|
|
|
|
|
|
|
deriving instance (NFData variable) => NFData (InterpolatedItem variable)
|
|
|
|
|
|
|
|
---------------------------------------
|
|
|
|
|
2023-02-22 16:45:27 +03:00
|
|
|
-- | A list of logical model components representing a single logical model,
|
2023-02-01 11:44:50 +03:00
|
|
|
-- separating the variables from the text.
|
|
|
|
newtype InterpolatedQuery variable = InterpolatedQuery
|
|
|
|
{ getInterpolatedQuery :: [InterpolatedItem variable]
|
|
|
|
}
|
2023-01-31 15:52:26 +03:00
|
|
|
deriving newtype (Eq, Ord, Show, Generic)
|
2023-02-28 14:17:08 +03:00
|
|
|
deriving stock (Data, Functor, Foldable, Traversable)
|
2023-01-31 15:52:26 +03:00
|
|
|
|
|
|
|
deriving newtype instance (Hashable variable) => Hashable (InterpolatedQuery variable)
|
|
|
|
|
|
|
|
deriving newtype instance (NFData variable) => NFData (InterpolatedQuery variable)
|
|
|
|
|
2023-02-22 16:45:27 +03:00
|
|
|
ppInterpolatedQuery :: InterpolatedQuery LogicalModelArgumentName -> Text
|
2023-02-01 11:44:50 +03:00
|
|
|
ppInterpolatedQuery (InterpolatedQuery parts) = foldMap ppInterpolatedItem parts
|
2023-01-31 15:52:26 +03:00
|
|
|
|
2023-02-01 11:44:50 +03:00
|
|
|
-- | We store the interpolated query as the user text and parse it back
|
|
|
|
-- when converting back to Haskell code.
|
2023-02-22 16:45:27 +03:00
|
|
|
instance HasCodec (InterpolatedQuery LogicalModelArgumentName) where
|
2023-02-01 11:44:50 +03:00
|
|
|
codec =
|
|
|
|
CommentCodec
|
|
|
|
("An interpolated query expressed in native code (SQL)")
|
|
|
|
$ bimapCodec
|
|
|
|
(first T.unpack . parseInterpolatedQuery)
|
|
|
|
ppInterpolatedQuery
|
|
|
|
textCodec
|
2023-01-31 15:52:26 +03:00
|
|
|
|
|
|
|
---------------------------------------
|
|
|
|
|
2023-02-22 16:45:27 +03:00
|
|
|
newtype LogicalModelArgumentName = LogicalModelArgumentName
|
|
|
|
{ getLogicalModelArgumentName :: Text
|
2023-02-03 14:15:08 +03:00
|
|
|
}
|
|
|
|
deriving newtype (Eq, Ord, Show, Hashable)
|
|
|
|
deriving stock (Generic)
|
|
|
|
|
2023-02-22 16:45:27 +03:00
|
|
|
instance HasCodec LogicalModelArgumentName where
|
|
|
|
codec = dimapCodec LogicalModelArgumentName getLogicalModelArgumentName codec
|
2023-02-03 14:15:08 +03:00
|
|
|
|
2023-02-22 16:45:27 +03:00
|
|
|
deriving newtype instance ToJSON LogicalModelArgumentName
|
2023-02-03 14:15:08 +03:00
|
|
|
|
2023-02-22 16:45:27 +03:00
|
|
|
deriving newtype instance FromJSON LogicalModelArgumentName
|
2023-02-03 14:15:08 +03:00
|
|
|
|
2023-02-22 16:45:27 +03:00
|
|
|
deriving newtype instance ToJSONKey LogicalModelArgumentName
|
2023-02-03 14:15:08 +03:00
|
|
|
|
2023-02-22 16:45:27 +03:00
|
|
|
deriving newtype instance FromJSONKey LogicalModelArgumentName
|
2023-02-03 14:15:08 +03:00
|
|
|
|
2023-02-22 16:45:27 +03:00
|
|
|
instance NFData LogicalModelArgumentName
|
2023-02-03 14:15:08 +03:00
|
|
|
|
|
|
|
---------------------------------------
|
|
|
|
|
2023-02-22 16:45:27 +03:00
|
|
|
-- | A representation of a logical model metadata info object.
|
|
|
|
data LogicalModelInfo (b :: BackendType) = LogicalModelInfo
|
|
|
|
{ lmiRootFieldName :: LogicalModelName,
|
|
|
|
lmiCode :: InterpolatedQuery LogicalModelArgumentName,
|
|
|
|
lmiReturns :: CustomReturnType b,
|
|
|
|
lmiArguments :: HashMap LogicalModelArgumentName (ScalarType b),
|
|
|
|
lmiDescription :: Maybe Text
|
2023-01-30 19:04:56 +03:00
|
|
|
}
|
|
|
|
deriving (Generic)
|
|
|
|
|
2023-02-22 16:45:27 +03:00
|
|
|
deriving instance Backend b => Eq (LogicalModelInfo b)
|
2023-01-30 19:04:56 +03:00
|
|
|
|
2023-02-22 16:45:27 +03:00
|
|
|
deriving instance Backend b => Show (LogicalModelInfo b)
|
2023-01-30 19:04:56 +03:00
|
|
|
|
2023-02-22 16:45:27 +03:00
|
|
|
instance Backend b => Hashable (LogicalModelInfo b)
|
2023-01-30 19:04:56 +03:00
|
|
|
|
2023-02-22 16:45:27 +03:00
|
|
|
instance Backend b => NFData (LogicalModelInfo b)
|
2023-01-30 19:04:56 +03:00
|
|
|
|
2023-02-22 16:45:27 +03:00
|
|
|
instance (Backend b) => HasCodec (LogicalModelInfo b) where
|
2023-01-30 19:04:56 +03:00
|
|
|
codec =
|
|
|
|
CommentCodec
|
|
|
|
("A query in expressed in native code (SQL) to add to the GraphQL schema with configuration.")
|
2023-02-22 16:45:27 +03:00
|
|
|
$ AC.object (codecNamePrefix @b <> "LogicalModelInfo")
|
|
|
|
$ LogicalModelInfo
|
2023-01-30 19:04:56 +03:00
|
|
|
<$> requiredField "root_field_name" fieldNameDoc
|
2023-02-22 16:45:27 +03:00
|
|
|
AC..= lmiRootFieldName
|
2023-01-30 19:04:56 +03:00
|
|
|
<*> requiredField "code" sqlDoc
|
2023-02-22 16:45:27 +03:00
|
|
|
AC..= lmiCode
|
2023-01-30 19:04:56 +03:00
|
|
|
<*> requiredField "returns" returnsDoc
|
2023-02-22 16:45:27 +03:00
|
|
|
AC..= lmiReturns
|
2023-01-31 13:53:29 +03:00
|
|
|
<*> optionalFieldWithDefault "arguments" mempty argumentDoc
|
2023-02-22 16:45:27 +03:00
|
|
|
AC..= lmiArguments
|
2023-01-30 19:04:56 +03:00
|
|
|
<*> optionalField "description" descriptionDoc
|
2023-02-22 16:45:27 +03:00
|
|
|
AC..= lmiDescription
|
2023-01-30 19:04:56 +03:00
|
|
|
where
|
2023-02-22 16:45:27 +03:00
|
|
|
fieldNameDoc = "Root field name for the logical model"
|
2023-01-30 19:04:56 +03:00
|
|
|
sqlDoc = "Native code expression (SQL) to run"
|
|
|
|
argumentDoc = "Free variables in the expression and their types"
|
|
|
|
returnsDoc = "Return type (table) of the expression"
|
2023-02-22 16:45:27 +03:00
|
|
|
descriptionDoc = "A description of the logical model which appears in the graphql schema"
|
2023-01-30 19:04:56 +03:00
|
|
|
|
|
|
|
deriving via
|
2023-02-22 16:45:27 +03:00
|
|
|
(Autodocodec (LogicalModelInfo b))
|
2023-01-30 19:04:56 +03:00
|
|
|
instance
|
2023-02-22 16:45:27 +03:00
|
|
|
(Backend b) => (FromJSON (LogicalModelInfo b))
|
2023-01-30 19:04:56 +03:00
|
|
|
|
|
|
|
deriving via
|
2023-02-22 16:45:27 +03:00
|
|
|
(Autodocodec (LogicalModelInfo b))
|
2023-01-30 19:04:56 +03:00
|
|
|
instance
|
2023-02-22 16:45:27 +03:00
|
|
|
(Backend b) => (ToJSON (LogicalModelInfo b))
|
2023-01-31 15:52:26 +03:00
|
|
|
|
|
|
|
-- | extract all of the `{{ variable }}` inside our query string
|
|
|
|
parseInterpolatedQuery ::
|
|
|
|
Text ->
|
2023-02-22 16:45:27 +03:00
|
|
|
Either Text (InterpolatedQuery LogicalModelArgumentName)
|
2023-01-31 15:52:26 +03:00
|
|
|
parseInterpolatedQuery =
|
|
|
|
fmap
|
|
|
|
( InterpolatedQuery
|
|
|
|
. mergeAdjacent
|
|
|
|
. trashEmpties
|
|
|
|
)
|
|
|
|
. consumeString
|
|
|
|
. T.unpack
|
|
|
|
where
|
|
|
|
trashEmpties = filter (/= IIText "")
|
|
|
|
|
|
|
|
mergeAdjacent = \case
|
|
|
|
(IIText a : IIText b : rest) ->
|
|
|
|
mergeAdjacent (IIText (a <> b) : rest)
|
|
|
|
(a : rest) -> a : mergeAdjacent rest
|
|
|
|
[] -> []
|
|
|
|
|
2023-02-22 16:45:27 +03:00
|
|
|
consumeString :: String -> Either Text [InterpolatedItem LogicalModelArgumentName]
|
2023-01-31 15:52:26 +03:00
|
|
|
consumeString str =
|
|
|
|
let (beforeCurly, fromCurly) = break (== '{') str
|
|
|
|
in case fromCurly of
|
|
|
|
('{' : '{' : rest) ->
|
|
|
|
(IIText (T.pack beforeCurly) :) <$> consumeVar rest
|
|
|
|
('{' : other) ->
|
|
|
|
(IIText (T.pack (beforeCurly <> "{")) :) <$> consumeString other
|
|
|
|
_other -> pure [IIText (T.pack beforeCurly)]
|
|
|
|
|
2023-02-22 16:45:27 +03:00
|
|
|
consumeVar :: String -> Either Text [InterpolatedItem LogicalModelArgumentName]
|
2023-01-31 15:52:26 +03:00
|
|
|
consumeVar str =
|
|
|
|
let (beforeCloseCurly, fromClosedCurly) = break (== '}') str
|
|
|
|
in case fromClosedCurly of
|
|
|
|
('}' : '}' : rest) ->
|
2023-02-22 16:45:27 +03:00
|
|
|
(IIVariable (LogicalModelArgumentName $ T.pack beforeCloseCurly) :) <$> consumeString rest
|
2023-01-31 15:52:26 +03:00
|
|
|
_ -> Left "Found '{{' without a matching closing '}}'"
|