2023-01-31 15:52:26 +03:00
|
|
|
{-# LANGUAGE DeriveAnyClass #-}
|
2023-03-03 18:27:51 +03:00
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
2023-01-30 19:04:56 +03:00
|
|
|
{-# LANGUAGE UndecidableInstances #-}
|
|
|
|
|
2023-04-13 19:10:38 +03:00
|
|
|
-- | Metadata representation of a native query in the metadata,
|
2023-02-22 16:45:27 +03:00
|
|
|
-- as well as a parser and prettyprinter for the query code.
|
2023-04-13 19:10:38 +03:00
|
|
|
module Hasura.NativeQuery.Metadata
|
|
|
|
( NativeQueryName (..),
|
|
|
|
NativeQueryMetadata (..),
|
|
|
|
nqmArguments,
|
|
|
|
nqmCode,
|
|
|
|
nqmDescription,
|
|
|
|
nqmReturns,
|
2023-04-17 14:30:10 +03:00
|
|
|
nqmArrayRelationships,
|
2023-04-13 19:10:38 +03:00
|
|
|
nqmRootFieldName,
|
2023-05-02 15:54:30 +03:00
|
|
|
ArgumentName (..),
|
2023-01-31 15:52:26 +03:00
|
|
|
InterpolatedItem (..),
|
|
|
|
InterpolatedQuery (..),
|
|
|
|
parseInterpolatedQuery,
|
2023-04-13 19:10:38 +03:00
|
|
|
module Hasura.NativeQuery.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
|
2023-03-03 18:27:51 +03:00
|
|
|
import Control.Lens (makeLenses)
|
2023-04-28 22:23:32 +03:00
|
|
|
import Data.Aeson (FromJSON, ToJSON)
|
2023-04-17 14:30:10 +03:00
|
|
|
import Data.HashMap.Strict.InsOrd.Autodocodec (sortedElemsCodec)
|
|
|
|
import Data.Text.Extended qualified as T
|
2023-04-19 12:03:36 +03:00
|
|
|
import Hasura.LogicalModel.Types
|
2023-04-28 22:23:32 +03:00
|
|
|
import Hasura.NativeQuery.InterpolatedQuery
|
2023-04-17 14:30:10 +03:00
|
|
|
import Hasura.NativeQuery.Types (NativeQueryName (..), NullableScalarType (..), nullableScalarTypeMapCodec)
|
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-04-25 11:59:34 +03:00
|
|
|
import Hasura.RQL.Types.BackendTag (backendPrefix)
|
2023-04-24 21:35:48 +03:00
|
|
|
import Hasura.RQL.Types.BackendType
|
2023-04-17 14:30:10 +03:00
|
|
|
import Hasura.RQL.Types.Common (RelName)
|
|
|
|
import Hasura.RQL.Types.Relationships.Local (RelDef (..), RelManualConfig (..))
|
2023-01-19 14:25:52 +03:00
|
|
|
|
2023-04-17 14:30:10 +03:00
|
|
|
-- | copy pasta'd from Hasura.RQL.Types.Metadata.Common, forgive me Padre i did
|
|
|
|
-- not have the heart for the Real Fix.
|
|
|
|
type Relationships = InsOrdHashMap RelName
|
|
|
|
|
2023-02-03 14:15:08 +03:00
|
|
|
---------------------------------------
|
|
|
|
|
2023-04-13 19:10:38 +03:00
|
|
|
-- | The representation of native queries within the metadata structure.
|
|
|
|
data NativeQueryMetadata (b :: BackendType) = NativeQueryMetadata
|
|
|
|
{ _nqmRootFieldName :: NativeQueryName,
|
2023-05-02 15:54:30 +03:00
|
|
|
_nqmCode :: InterpolatedQuery ArgumentName,
|
2023-04-19 12:03:36 +03:00
|
|
|
_nqmReturns :: LogicalModelName,
|
2023-05-02 15:54:30 +03:00
|
|
|
_nqmArguments :: HashMap ArgumentName (NullableScalarType b),
|
2023-04-17 14:30:10 +03:00
|
|
|
_nqmArrayRelationships :: Relationships (RelDef (RelManualConfig b)),
|
2023-04-13 19:10:38 +03:00
|
|
|
_nqmDescription :: Maybe Text
|
2023-01-30 19:04:56 +03:00
|
|
|
}
|
|
|
|
deriving (Generic)
|
|
|
|
|
2023-04-13 19:10:38 +03:00
|
|
|
deriving instance Backend b => Eq (NativeQueryMetadata b)
|
2023-01-30 19:04:56 +03:00
|
|
|
|
2023-04-13 19:10:38 +03:00
|
|
|
deriving instance Backend b => Show (NativeQueryMetadata b)
|
2023-01-30 19:04:56 +03:00
|
|
|
|
2023-04-13 19:10:38 +03:00
|
|
|
instance (Backend b) => HasCodec (NativeQueryMetadata b) where
|
2023-01-30 19:04:56 +03:00
|
|
|
codec =
|
|
|
|
CommentCodec
|
2023-04-13 19:10:38 +03:00
|
|
|
("A native query as represented in metadata.")
|
2023-04-25 11:59:34 +03:00
|
|
|
$ AC.object (backendPrefix @b <> "NativeQueryMetadata")
|
2023-04-13 19:10:38 +03:00
|
|
|
$ NativeQueryMetadata
|
2023-01-30 19:04:56 +03:00
|
|
|
<$> requiredField "root_field_name" fieldNameDoc
|
2023-04-13 19:10:38 +03:00
|
|
|
AC..= _nqmRootFieldName
|
2023-01-30 19:04:56 +03:00
|
|
|
<*> requiredField "code" sqlDoc
|
2023-04-13 19:10:38 +03:00
|
|
|
AC..= _nqmCode
|
2023-01-30 19:04:56 +03:00
|
|
|
<*> requiredField "returns" returnsDoc
|
2023-04-13 19:10:38 +03:00
|
|
|
AC..= _nqmReturns
|
2023-01-31 13:53:29 +03:00
|
|
|
<*> optionalFieldWithDefault "arguments" mempty argumentDoc
|
2023-04-13 19:10:38 +03:00
|
|
|
AC..= _nqmArguments
|
2023-04-17 14:30:10 +03:00
|
|
|
<*> optSortedList "array_relationships" _rdName
|
|
|
|
AC..= _nqmArrayRelationships
|
2023-01-30 19:04:56 +03:00
|
|
|
<*> optionalField "description" descriptionDoc
|
2023-04-13 19:10:38 +03:00
|
|
|
AC..= _nqmDescription
|
2023-01-30 19:04:56 +03:00
|
|
|
where
|
2023-04-13 19:10:38 +03:00
|
|
|
fieldNameDoc = "Root field name for the native query"
|
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-04-13 19:10:38 +03:00
|
|
|
descriptionDoc = "A description of the native query which appears in the graphql schema"
|
2023-01-30 19:04:56 +03:00
|
|
|
|
2023-04-17 14:30:10 +03:00
|
|
|
optSortedList ::
|
|
|
|
(HasCodec a, Eq a, Hashable k, Ord k, T.ToTxt k) =>
|
|
|
|
Text ->
|
|
|
|
(a -> k) ->
|
|
|
|
ObjectCodec (InsOrdHashMap k a) (InsOrdHashMap k a)
|
|
|
|
optSortedList name keyForElem =
|
|
|
|
AC.optionalFieldWithOmittedDefaultWith' name (sortedElemsCodec keyForElem) mempty
|
|
|
|
|
2023-01-30 19:04:56 +03:00
|
|
|
deriving via
|
2023-04-13 19:10:38 +03:00
|
|
|
(Autodocodec (NativeQueryMetadata b))
|
2023-01-30 19:04:56 +03:00
|
|
|
instance
|
2023-04-13 19:10:38 +03:00
|
|
|
(Backend b) => (FromJSON (NativeQueryMetadata b))
|
2023-01-30 19:04:56 +03:00
|
|
|
|
|
|
|
deriving via
|
2023-04-13 19:10:38 +03:00
|
|
|
(Autodocodec (NativeQueryMetadata b))
|
2023-01-30 19:04:56 +03:00
|
|
|
instance
|
2023-04-13 19:10:38 +03:00
|
|
|
(Backend b) => (ToJSON (NativeQueryMetadata b))
|
2023-01-31 15:52:26 +03:00
|
|
|
|
2023-04-13 19:10:38 +03:00
|
|
|
makeLenses ''NativeQueryMetadata
|