2023-04-13 19:10:38 +03:00
|
|
|
-- | Schema parsers for native queries.
|
2023-05-10 18:13:56 +03:00
|
|
|
module Hasura.NativeQuery.Schema
|
|
|
|
( defaultSelectNativeQuery,
|
|
|
|
defaultSelectNativeQueryObject,
|
|
|
|
defaultBuildNativeQueryRootFields,
|
|
|
|
)
|
|
|
|
where
|
2023-01-19 14:25:52 +03:00
|
|
|
|
|
|
|
import Data.Has (Has (getter))
|
2023-04-26 18:42:13 +03:00
|
|
|
import Data.HashMap.Strict qualified as HashMap
|
2023-01-19 14:25:52 +03:00
|
|
|
import Hasura.GraphQL.Schema.Backend
|
2023-04-19 12:03:36 +03:00
|
|
|
( BackendLogicalModelSelectSchema (..),
|
2023-01-19 14:25:52 +03:00
|
|
|
MonadBuildSchema,
|
|
|
|
)
|
|
|
|
import Hasura.GraphQL.Schema.Common
|
2023-02-15 20:55:06 +03:00
|
|
|
( SchemaT,
|
2023-01-19 14:25:52 +03:00
|
|
|
retrieve,
|
|
|
|
)
|
|
|
|
import Hasura.GraphQL.Schema.Parser qualified as P
|
2023-05-10 18:13:56 +03:00
|
|
|
import Hasura.LogicalModel.Schema (buildLogicalModelFields, buildLogicalModelIR, buildLogicalModelPermissions)
|
2023-05-03 12:59:21 +03:00
|
|
|
import Hasura.LogicalModelResolver.Schema (argumentsSchema)
|
2023-04-13 19:10:38 +03:00
|
|
|
import Hasura.NativeQuery.Cache (NativeQueryInfo (..))
|
|
|
|
import Hasura.NativeQuery.IR (NativeQuery (..))
|
2023-05-02 15:54:30 +03:00
|
|
|
import Hasura.NativeQuery.Metadata (ArgumentName (..), InterpolatedQuery (..))
|
2023-04-13 19:10:38 +03:00
|
|
|
import Hasura.NativeQuery.Types (NullableScalarType (..), getNativeQueryName)
|
2023-01-19 14:25:52 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.IR.Root (RemoteRelationshipField)
|
2023-01-27 17:36:35 +03:00
|
|
|
import Hasura.RQL.IR.Select (QueryDB (QDBMultipleRows))
|
2023-01-19 14:25:52 +03:00
|
|
|
import Hasura.RQL.IR.Select qualified as IR
|
2023-05-03 12:59:21 +03:00
|
|
|
import Hasura.RQL.IR.Value (Provenance (FromInternal), UnpreparedValue (UVParameter))
|
2023-01-19 14:25:52 +03:00
|
|
|
import Hasura.RQL.Types.Column qualified as Column
|
|
|
|
import Hasura.RQL.Types.Metadata.Object qualified as MO
|
2023-05-31 03:14:16 +03:00
|
|
|
import Hasura.RQL.Types.Relationships.Local (Nullable (..))
|
2023-04-24 18:17:15 +03:00
|
|
|
import Hasura.RQL.Types.Schema.Options qualified as Options
|
2023-01-19 14:25:52 +03:00
|
|
|
import Hasura.RQL.Types.Source
|
|
|
|
( SourceInfo (_siCustomization, _siName),
|
|
|
|
)
|
|
|
|
import Hasura.RQL.Types.SourceCustomization
|
|
|
|
( ResolvedSourceCustomization (_rscNamingConvention),
|
|
|
|
)
|
|
|
|
import Hasura.SQL.AnyBackend (mkAnyBackend)
|
|
|
|
import Language.GraphQL.Draft.Syntax qualified as G
|
|
|
|
|
2023-05-10 18:13:56 +03:00
|
|
|
defaultSelectNativeQueryObject ::
|
2023-01-19 14:25:52 +03:00
|
|
|
forall b r m n.
|
|
|
|
( MonadBuildSchema b r m n,
|
2023-04-19 12:03:36 +03:00
|
|
|
BackendLogicalModelSelectSchema b
|
2023-01-19 14:25:52 +03:00
|
|
|
) =>
|
2023-05-10 18:13:56 +03:00
|
|
|
-- native query info
|
2023-04-13 19:10:38 +03:00
|
|
|
NativeQueryInfo b ->
|
2023-05-10 18:13:56 +03:00
|
|
|
-- field name
|
|
|
|
G.Name ->
|
|
|
|
-- field description, if any
|
|
|
|
Maybe G.Description ->
|
2023-01-19 14:25:52 +03:00
|
|
|
SchemaT
|
|
|
|
r
|
|
|
|
m
|
2023-05-10 18:13:56 +03:00
|
|
|
(Maybe (P.FieldParser n (IR.AnnObjectSelectG b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b))))
|
|
|
|
defaultSelectNativeQueryObject NativeQueryInfo {..} fieldName description = runMaybeT $ do
|
|
|
|
nativeQueryArgsParser <-
|
|
|
|
nativeQueryArgumentsSchema @b @r @m @n fieldName _nqiArguments
|
|
|
|
|
|
|
|
sourceInfo :: SourceInfo b <- asks getter
|
|
|
|
|
|
|
|
let sourceName = _siName sourceInfo
|
|
|
|
|
|
|
|
logicalModelPermissions <-
|
2023-05-24 16:51:56 +03:00
|
|
|
MaybeT
|
|
|
|
. fmap Just
|
|
|
|
$ buildLogicalModelPermissions @b @r @m @n _nqiReturns
|
2023-05-10 18:13:56 +03:00
|
|
|
|
2023-05-15 11:10:59 +03:00
|
|
|
selectionSetParser <-
|
|
|
|
MaybeT $ logicalModelSelectionSet _nqiRelationships _nqiReturns
|
2023-05-10 18:13:56 +03:00
|
|
|
|
|
|
|
let sourceObj =
|
|
|
|
MO.MOSourceObjId
|
|
|
|
sourceName
|
|
|
|
(mkAnyBackend $ MO.SMONativeQuery @b _nqiRootFieldName)
|
|
|
|
|
2023-05-24 16:51:56 +03:00
|
|
|
pure
|
|
|
|
$ P.setFieldParserOrigin sourceObj
|
|
|
|
$ P.subselection
|
|
|
|
fieldName
|
|
|
|
description
|
|
|
|
nativeQueryArgsParser
|
|
|
|
selectionSetParser
|
|
|
|
<&> \(nqArgs, fields) ->
|
|
|
|
IR.AnnObjectSelectG
|
|
|
|
fields
|
|
|
|
( IR.FromNativeQuery
|
|
|
|
NativeQuery
|
|
|
|
{ nqRootFieldName = _nqiRootFieldName,
|
|
|
|
nqInterpolatedQuery = interpolatedQuery _nqiCode nqArgs,
|
|
|
|
nqLogicalModel = buildLogicalModelIR _nqiReturns
|
|
|
|
}
|
|
|
|
)
|
|
|
|
(IR._tpFilter logicalModelPermissions)
|
2023-04-04 15:45:20 +03:00
|
|
|
|
2023-05-10 18:13:56 +03:00
|
|
|
-- | select a native query - implementation is the same for root fields and
|
|
|
|
-- array relationships
|
|
|
|
defaultSelectNativeQuery ::
|
|
|
|
forall b r m n.
|
|
|
|
( MonadBuildSchema b r m n,
|
|
|
|
BackendLogicalModelSelectSchema b
|
|
|
|
) =>
|
|
|
|
-- native query info
|
|
|
|
NativeQueryInfo b ->
|
|
|
|
-- field name
|
|
|
|
G.Name ->
|
2023-05-31 03:14:16 +03:00
|
|
|
-- are fields nullable?
|
|
|
|
Nullable ->
|
2023-05-10 18:13:56 +03:00
|
|
|
-- field description, if any
|
|
|
|
Maybe G.Description ->
|
|
|
|
SchemaT
|
|
|
|
r
|
|
|
|
m
|
|
|
|
(Maybe (P.FieldParser n (IR.AnnSimpleSelectG b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b))))
|
2023-05-31 03:14:16 +03:00
|
|
|
defaultSelectNativeQuery NativeQueryInfo {..} fieldName nullability description = runMaybeT $ do
|
2023-04-13 19:10:38 +03:00
|
|
|
nativeQueryArgsParser <-
|
|
|
|
nativeQueryArgumentsSchema @b @r @m @n fieldName _nqiArguments
|
2023-02-15 20:55:06 +03:00
|
|
|
|
2023-01-19 14:25:52 +03:00
|
|
|
sourceInfo :: SourceInfo b <- asks getter
|
2023-02-15 20:55:06 +03:00
|
|
|
|
2023-01-19 14:25:52 +03:00
|
|
|
let sourceName = _siName sourceInfo
|
|
|
|
tCase = _rscNamingConvention $ _siCustomization sourceInfo
|
2023-02-15 20:55:06 +03:00
|
|
|
|
2023-01-19 14:25:52 +03:00
|
|
|
stringifyNumbers <- retrieve Options.soStringifyNumbers
|
|
|
|
|
2023-04-19 12:03:36 +03:00
|
|
|
logicalModelPermissions <-
|
2023-05-24 16:51:56 +03:00
|
|
|
MaybeT
|
|
|
|
. fmap Just
|
|
|
|
$ buildLogicalModelPermissions @b @r @m @n _nqiReturns
|
2023-04-04 15:45:20 +03:00
|
|
|
|
2023-05-15 11:10:59 +03:00
|
|
|
(selectionListParser, logicalModelsArgsParser) <-
|
2023-05-31 03:14:16 +03:00
|
|
|
MaybeT $ buildLogicalModelFields _nqiRelationships nullability _nqiReturns
|
2023-02-03 14:15:08 +03:00
|
|
|
|
2023-04-04 15:45:20 +03:00
|
|
|
let sourceObj =
|
|
|
|
MO.MOSourceObjId
|
|
|
|
sourceName
|
2023-04-13 19:10:38 +03:00
|
|
|
(mkAnyBackend $ MO.SMONativeQuery @b _nqiRootFieldName)
|
2023-03-31 18:33:32 +03:00
|
|
|
|
2023-05-24 16:51:56 +03:00
|
|
|
pure
|
|
|
|
$ P.setFieldParserOrigin sourceObj
|
|
|
|
$ P.subselection
|
|
|
|
fieldName
|
|
|
|
description
|
|
|
|
( (,)
|
|
|
|
<$> logicalModelsArgsParser
|
|
|
|
<*> nativeQueryArgsParser
|
|
|
|
)
|
|
|
|
selectionListParser
|
|
|
|
<&> \((lmArgs, nqArgs), fields) ->
|
|
|
|
IR.AnnSelectG
|
|
|
|
{ IR._asnFields = fields,
|
|
|
|
IR._asnFrom =
|
|
|
|
IR.FromNativeQuery
|
|
|
|
NativeQuery
|
|
|
|
{ nqRootFieldName = _nqiRootFieldName,
|
|
|
|
nqInterpolatedQuery = interpolatedQuery _nqiCode nqArgs,
|
|
|
|
nqLogicalModel = buildLogicalModelIR _nqiReturns
|
|
|
|
},
|
|
|
|
IR._asnPerm = logicalModelPermissions,
|
|
|
|
IR._asnArgs = lmArgs,
|
|
|
|
IR._asnStrfyNum = stringifyNumbers,
|
|
|
|
IR._asnNamingConvention = Just tCase
|
|
|
|
}
|
2023-05-10 18:13:56 +03:00
|
|
|
|
|
|
|
defaultBuildNativeQueryRootFields ::
|
|
|
|
forall b r m n.
|
|
|
|
( MonadBuildSchema b r m n,
|
|
|
|
BackendLogicalModelSelectSchema b
|
|
|
|
) =>
|
|
|
|
NativeQueryInfo b ->
|
|
|
|
SchemaT
|
|
|
|
r
|
|
|
|
m
|
|
|
|
(Maybe (P.FieldParser n (QueryDB b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b))))
|
|
|
|
defaultBuildNativeQueryRootFields nqi@NativeQueryInfo {..} = do
|
|
|
|
let fieldName = getNativeQueryName _nqiRootFieldName
|
|
|
|
description = G.Description <$> _nqiDescription
|
2023-05-31 03:14:16 +03:00
|
|
|
(fmap . fmap . fmap) QDBMultipleRows (defaultSelectNativeQuery nqi fieldName NotNullable description)
|
2023-01-19 14:25:52 +03:00
|
|
|
|
2023-04-13 19:10:38 +03:00
|
|
|
nativeQueryArgumentsSchema ::
|
2023-01-19 14:25:52 +03:00
|
|
|
forall b r m n.
|
2023-05-24 16:51:56 +03:00
|
|
|
(MonadBuildSchema b r m n) =>
|
2023-01-19 14:25:52 +03:00
|
|
|
G.Name ->
|
2023-05-02 15:54:30 +03:00
|
|
|
HashMap ArgumentName (NullableScalarType b) ->
|
|
|
|
MaybeT (SchemaT r m) (P.InputFieldsParser n (HashMap ArgumentName (Column.ColumnValue b)))
|
2023-05-03 12:59:21 +03:00
|
|
|
nativeQueryArgumentsSchema = argumentsSchema "Native Query"
|
2023-05-10 18:13:56 +03:00
|
|
|
|
|
|
|
-- | swap the template names in our query for unprepared values
|
|
|
|
interpolatedQuery ::
|
|
|
|
InterpolatedQuery ArgumentName ->
|
|
|
|
HashMap ArgumentName (Column.ColumnValue b) ->
|
|
|
|
InterpolatedQuery (UnpreparedValue b)
|
|
|
|
interpolatedQuery nqiCode nqArgs =
|
2023-05-24 16:51:56 +03:00
|
|
|
InterpolatedQuery
|
|
|
|
$ (fmap . fmap)
|
2023-05-10 18:13:56 +03:00
|
|
|
( \var@(ArgumentName name) -> case HashMap.lookup var nqArgs of
|
|
|
|
Just arg -> UVParameter (FromInternal name) arg
|
|
|
|
Nothing ->
|
|
|
|
-- the `nativeQueryArgsParser` will already have checked
|
|
|
|
-- we have all the args the query needs so this _should
|
|
|
|
-- not_ happen
|
|
|
|
error $ "No native query arg passed for " <> show var
|
|
|
|
)
|
|
|
|
(getInterpolatedQuery nqiCode)
|