2022-04-22 16:38:35 +03:00
|
|
|
-- | This module defines the top-level translation functions pertaining to
|
|
|
|
-- translating Connection (i.e. Relay) queries into Postgres AST.
|
|
|
|
module Hasura.Backends.Postgres.Translate.Select.Connection
|
|
|
|
( connectionSelectQuerySQL,
|
|
|
|
mkConnectionSelect,
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Control.Monad.Writer (runWriter)
|
2023-01-27 13:03:47 +03:00
|
|
|
import Database.PG.Query (Query, fromBuilder)
|
2022-04-22 16:38:35 +03:00
|
|
|
import Hasura.Backends.Postgres.SQL.DML qualified as S
|
2023-01-27 13:03:47 +03:00
|
|
|
import Hasura.Backends.Postgres.SQL.RenameIdentifiers (renameIdentifiersSelectWith)
|
2022-04-22 16:38:35 +03:00
|
|
|
import Hasura.Backends.Postgres.SQL.Types
|
|
|
|
import Hasura.Backends.Postgres.Translate.Select.AnnotatedFieldJSON
|
2022-04-22 20:18:20 +03:00
|
|
|
import Hasura.Backends.Postgres.Translate.Select.Internal.GenerateSelect (connectionToSelectWith)
|
|
|
|
import Hasura.Backends.Postgres.Translate.Select.Internal.Process (processConnectionSelect)
|
2022-04-22 16:38:35 +03:00
|
|
|
import Hasura.Backends.Postgres.Translate.Types
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.IR.Select
|
|
|
|
( AnnSelectG (_asnStrfyNum),
|
|
|
|
ConnectionSelect (_csSelect),
|
|
|
|
)
|
|
|
|
import Hasura.RQL.Types.Backend (Backend)
|
|
|
|
import Hasura.RQL.Types.Common (FieldName (FieldName))
|
|
|
|
import Hasura.SQL.Backend (BackendType (Postgres))
|
2023-01-27 13:03:47 +03:00
|
|
|
import Hasura.SQL.Types (ToSQL (toSQL))
|
2022-04-22 16:38:35 +03:00
|
|
|
|
|
|
|
-- | Translates IR to Postgres queries for "connection" queries (used for Relay).
|
|
|
|
--
|
|
|
|
-- See 'mkConnectionSelect' for the Postgres AST.
|
|
|
|
connectionSelectQuerySQL ::
|
|
|
|
forall pgKind.
|
|
|
|
( Backend ('Postgres pgKind),
|
|
|
|
PostgresAnnotatedFieldJSON pgKind
|
|
|
|
) =>
|
|
|
|
ConnectionSelect ('Postgres pgKind) Void S.SQLExp ->
|
|
|
|
Query
|
|
|
|
connectionSelectQuerySQL =
|
2023-01-27 13:03:47 +03:00
|
|
|
fromBuilder . toSQL . mkConnectionSelect
|
2022-04-22 16:38:35 +03:00
|
|
|
|
|
|
|
mkConnectionSelect ::
|
2023-01-27 13:03:47 +03:00
|
|
|
forall pgKind.
|
2022-04-22 16:38:35 +03:00
|
|
|
( Backend ('Postgres pgKind),
|
2023-01-27 13:03:47 +03:00
|
|
|
PostgresAnnotatedFieldJSON pgKind
|
2022-04-22 16:38:35 +03:00
|
|
|
) =>
|
|
|
|
ConnectionSelect ('Postgres pgKind) Void S.SQLExp ->
|
2023-01-27 13:03:47 +03:00
|
|
|
S.SelectWithG S.Select
|
|
|
|
mkConnectionSelect connectionSelect =
|
|
|
|
let ((connectionSource, topExtractor, nodeExtractors), joinTree) =
|
|
|
|
runWriter $
|
|
|
|
flip runReaderT strfyNum $
|
|
|
|
processConnectionSelect
|
|
|
|
sourcePrefixes
|
|
|
|
rootFieldName
|
|
|
|
(S.toTableAlias rootIdentifier)
|
|
|
|
mempty
|
|
|
|
connectionSelect
|
2022-04-22 16:38:35 +03:00
|
|
|
selectNode =
|
|
|
|
MultiRowSelectNode [topExtractor] $
|
|
|
|
SelectNode nodeExtractors joinTree
|
2023-01-27 13:03:47 +03:00
|
|
|
in renameIdentifiersSelectWith $
|
2022-07-18 12:44:17 +03:00
|
|
|
connectionToSelectWith (S.toTableAlias rootIdentifier) connectionSource selectNode
|
2022-04-22 16:38:35 +03:00
|
|
|
where
|
|
|
|
strfyNum = _asnStrfyNum $ _csSelect connectionSelect
|
|
|
|
rootFieldName = FieldName "root"
|
|
|
|
rootIdentifier = toIdentifier rootFieldName
|
|
|
|
sourcePrefixes = SourcePrefixes rootIdentifier rootIdentifier
|