2022-03-16 03:39:21 +03:00
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
|
|
|
|
2022-04-07 17:41:43 +03:00
|
|
|
-- | Postgres Execute subscription
|
2022-02-08 12:24:34 +03:00
|
|
|
--
|
|
|
|
-- Multiplex is an optimization which allows us to group similar queries into a
|
|
|
|
-- single query, and routing the response rows afterwards. See
|
|
|
|
-- https://hasura.io/docs/latest/graphql/core/databases/postgres/subscriptions/execution-and-performance.html
|
|
|
|
-- for more details
|
|
|
|
--
|
|
|
|
-- See 'Hasura.Backends.Postgres.Instances.Execute'.
|
2022-04-07 17:41:43 +03:00
|
|
|
module Hasura.Backends.Postgres.Execute.Subscription
|
2021-02-20 16:45:49 +03:00
|
|
|
( MultiplexedQuery (..),
|
|
|
|
QueryParametersInfo (..),
|
|
|
|
mkMultiplexedQuery,
|
2022-04-07 17:41:43 +03:00
|
|
|
mkStreamingMultiplexedQuery,
|
2021-02-20 16:45:49 +03:00
|
|
|
resolveMultiplexedValue,
|
2022-12-22 20:08:04 +03:00
|
|
|
validateVariablesTx,
|
2021-02-20 16:45:49 +03:00
|
|
|
executeMultiplexedQuery,
|
2022-04-07 17:41:43 +03:00
|
|
|
executeStreamingMultiplexedQuery,
|
2021-02-20 16:45:49 +03:00
|
|
|
executeQuery,
|
2022-04-22 22:53:12 +03:00
|
|
|
SubscriptionType (..),
|
2021-02-20 16:45:49 +03:00
|
|
|
)
|
|
|
|
where
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2021-02-20 16:45:49 +03:00
|
|
|
import Control.Lens
|
|
|
|
import Data.ByteString qualified as B
|
|
|
|
import Data.HashMap.Strict qualified as Map
|
|
|
|
import Data.HashMap.Strict.InsOrd qualified as OMap
|
|
|
|
import Data.HashSet qualified as Set
|
|
|
|
import Data.Semigroup.Generic
|
|
|
|
import Data.Text.Extended
|
Import `pg-client-hs` as `PG`
Result of executing the following commands:
```shell
# replace "as Q" imports with "as PG" (in retrospect this didn't need a regex)
git grep -lE 'as Q($|[^a-zA-Z])' -- '*.hs' | xargs sed -i -E 's/as Q($|[^a-zA-Z])/as PG\1/'
# replace " Q." with " PG."
git grep -lE ' Q\.' -- '*.hs' | xargs sed -i 's/ Q\./ PG./g'
# replace "(Q." with "(PG."
git grep -lE '\(Q\.' -- '*.hs' | xargs sed -i 's/(Q\./(PG./g'
# ditto, but for [, |, { and !
git grep -lE '\[Q\.' -- '*.hs' | xargs sed -i 's/\[Q\./\[PG./g'
git grep -l '|Q\.' -- '*.hs' | xargs sed -i 's/|Q\./|PG./g'
git grep -l '{Q\.' -- '*.hs' | xargs sed -i 's/{Q\./{PG./g'
git grep -l '!Q\.' -- '*.hs' | xargs sed -i 's/!Q\./!PG./g'
```
(Doing the `grep -l` before the `sed`, instead of `sed` on the entire codebase, reduces the number of `mtime` updates, and so reduces how many times a file gets recompiled while checking intermediate results.)
Finally, I manually removed a broken and unused `Arbitrary` instance in `Hasura.RQL.Network`. (It used an `import Test.QuickCheck.Arbitrary as Q` statement, which was erroneously caught by the first find-replace command.)
After this PR, `Q` is no longer used as an import qualifier. That was not the goal of this PR, but perhaps it's a useful fact for future efforts.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5933
GitOrigin-RevId: 8c84c59d57789111d40f5d3322c5a885dcfbf40e
2022-09-20 22:54:43 +03:00
|
|
|
import Database.PG.Query qualified as PG
|
2021-02-20 16:45:49 +03:00
|
|
|
import Hasura.Backends.Postgres.Connection
|
|
|
|
import Hasura.Backends.Postgres.SQL.DML qualified as S
|
|
|
|
import Hasura.Backends.Postgres.SQL.Error
|
|
|
|
import Hasura.Backends.Postgres.SQL.Types
|
|
|
|
import Hasura.Backends.Postgres.SQL.Value
|
|
|
|
import Hasura.Backends.Postgres.Translate.Column (toTxtValue)
|
|
|
|
import Hasura.Backends.Postgres.Translate.Select qualified as DS
|
2021-03-25 20:50:08 +03:00
|
|
|
import Hasura.Backends.Postgres.Types.Column
|
2021-05-11 18:18:31 +03:00
|
|
|
import Hasura.Base.Error
|
2022-03-21 13:39:49 +03:00
|
|
|
import Hasura.GraphQL.Execute.Subscription.Plan
|
server: Metadata origin for definitions (type parameter version v2)
The code that builds the GraphQL schema, and `buildGQLContext` in particular, is partial: not every value of `(ServerConfigCtx, GraphQLQueryType, SourceCache, HashMap RemoteSchemaName (RemoteSchemaCtx, MetadataObject), ActionCache, AnnotatedCustomTypes)` results in a valid GraphQL schema. When it fails, we want to be able to return better error messages than we currently do.
The key thing that is missing is a way to trace back GraphQL type information to their origin from the Hasura metadata. Currently, we have a number of correctness checks of our GraphQL schema. But these correctness checks only have access to pure GraphQL type information, and hence can only report errors in terms of that. Possibly the worst is the "conflicting definitions" error, which, in practice, can only be debugged by Hasura engineers. This is terrible DX for customers.
This PR allows us to print better error messages, by adding a field to the `Definition` type that traces the GraphQL type to its origin in the metadata. So the idea is simple: just add `MetadataObjId`, or `Maybe` that, or some other sum type of that, to `Definition`.
However, we want to avoid having to import a `Hasura.RQL` module from `Hasura.GraphQL.Parser`. So we instead define this additional field of `Definition` through a new type parameter, which is threaded through in `Hasura.GraphQL.Parser`. We then define type synonyms in `Hasura.GraphQL.Schema.Parser` that fill in this type parameter, so that it is not visible for the majority of the codebase.
The idea of associating metadata information to `Definition`s really comes to fruition when combined with hasura/graphql-engine-mono#4517. Their combination would allow us to use the API of fatal errors (just like the current `MonadError QErr`) to report _inconsistencies_ in the metadata. Such inconsistencies are then _automatically_ ignored. So no ad-hoc decisions need to be made on how to cut out inconsistent metadata from the GraphQL schema. This will allow us to report much better errors, as well as improve the likelihood of a successful HGE startup.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/4770
Co-authored-by: Samir Talwar <47582+SamirTalwar@users.noreply.github.com>
GitOrigin-RevId: 728402b0cae83ae8e83463a826ceeb609001acae
2022-06-28 18:52:26 +03:00
|
|
|
import Hasura.GraphQL.Parser.Names
|
2021-02-20 16:45:49 +03:00
|
|
|
import Hasura.Prelude
|
2021-06-11 06:26:50 +03:00
|
|
|
import Hasura.RQL.IR
|
2022-04-27 16:57:28 +03:00
|
|
|
import Hasura.RQL.Types.Backend
|
|
|
|
import Hasura.RQL.Types.Column
|
|
|
|
import Hasura.RQL.Types.Common
|
|
|
|
import Hasura.RQL.Types.Subscription
|
|
|
|
import Hasura.SQL.Backend
|
2021-02-20 16:45:49 +03:00
|
|
|
import Hasura.SQL.Types
|
|
|
|
import Hasura.Session
|
|
|
|
import Language.GraphQL.Draft.Syntax qualified as G
|
|
|
|
|
|
|
|
----------------------------------------------------------------------------------------------------
|
|
|
|
-- Variables
|
|
|
|
|
2022-10-11 13:42:15 +03:00
|
|
|
subsAlias :: S.TableAlias
|
|
|
|
subsAlias = S.mkTableAlias "_subs"
|
|
|
|
|
|
|
|
subsIdentifier :: TableIdentifier
|
|
|
|
subsIdentifier = S.tableAliasToIdentifier subsAlias
|
|
|
|
|
|
|
|
resultIdAlias, resultVarsAlias :: S.ColumnAlias
|
|
|
|
resultIdAlias = S.mkColumnAlias "result_id"
|
|
|
|
resultVarsAlias = S.mkColumnAlias "result_vars"
|
|
|
|
|
|
|
|
fldRespAlias :: S.TableAlias
|
|
|
|
fldRespAlias = S.mkTableAlias "_fld_resp"
|
|
|
|
|
|
|
|
fldRespIdentifier :: TableIdentifier
|
|
|
|
fldRespIdentifier = S.tableAliasToIdentifier fldRespAlias
|
|
|
|
|
2021-02-20 16:45:49 +03:00
|
|
|
-- | Internal: Used to collect information about various parameters
|
|
|
|
-- of a subscription field's AST as we resolve them to SQL expressions.
|
2021-04-22 00:44:37 +03:00
|
|
|
data QueryParametersInfo (b :: BackendType) = QueryParametersInfo
|
2022-07-29 17:05:03 +03:00
|
|
|
{ _qpiReusableVariableValues :: HashMap G.Name (ColumnValue b),
|
|
|
|
_qpiSyntheticVariableValues :: Seq (ColumnValue b),
|
2021-02-20 16:45:49 +03:00
|
|
|
-- | The session variables that are referenced in the query root fld's AST.
|
|
|
|
-- This information is used to determine a cohort's required session
|
|
|
|
-- variables
|
2022-07-29 17:05:03 +03:00
|
|
|
_qpiReferencedSessionVariables :: Set.HashSet SessionVariable
|
2021-02-20 16:45:49 +03:00
|
|
|
}
|
|
|
|
deriving (Generic)
|
2021-04-22 00:44:37 +03:00
|
|
|
deriving (Semigroup, Monoid) via (GenericSemigroupMonoid (QueryParametersInfo b))
|
2021-02-20 16:45:49 +03:00
|
|
|
|
|
|
|
makeLenses ''QueryParametersInfo
|
|
|
|
|
|
|
|
-- | Checks if the provided arguments are valid values for their corresponding types.
|
|
|
|
-- | Generates SQL of the format "select 'v1'::t1, 'v2'::t2 ..."
|
2022-12-22 20:08:04 +03:00
|
|
|
validateVariablesTx ::
|
2021-04-22 00:44:37 +03:00
|
|
|
forall pgKind f m.
|
2022-12-22 20:08:04 +03:00
|
|
|
(Traversable f, MonadTx m, MonadIO m) =>
|
2021-04-22 00:44:37 +03:00
|
|
|
f (ColumnValue ('Postgres pgKind)) ->
|
2021-02-20 16:45:49 +03:00
|
|
|
m (ValidatedVariables f)
|
2022-12-22 20:08:04 +03:00
|
|
|
validateVariablesTx variableValues = do
|
2022-11-02 14:39:49 +03:00
|
|
|
-- no need to test the types when there are no variables to test.
|
|
|
|
unless (null variableValues) do
|
|
|
|
let valSel = mkValidationSel $ toList variableValues
|
2022-12-22 20:08:04 +03:00
|
|
|
PG.Discard () <- liftTx $ PG.rawQE dataExnErrHandler (PG.fromBuilder $ toSQL valSel) [] False
|
2022-11-02 14:39:49 +03:00
|
|
|
pure ()
|
2021-04-20 19:57:14 +03:00
|
|
|
pure . ValidatedVariables $ fmap (txtEncodedVal . cvValue) variableValues
|
2021-02-20 16:45:49 +03:00
|
|
|
where
|
|
|
|
mkExtr = flip S.Extractor Nothing . toTxtValue
|
|
|
|
mkValidationSel vars =
|
|
|
|
S.mkSelect {S.selExtr = map mkExtr vars}
|
|
|
|
-- Explicitly look for the class of errors raised when the format of a value
|
|
|
|
-- provided for a type is incorrect.
|
|
|
|
dataExnErrHandler = mkTxErrorHandler (has _PGDataException)
|
|
|
|
|
|
|
|
----------------------------------------------------------------------------------------------------
|
|
|
|
-- Multiplexed queries
|
|
|
|
|
Import `pg-client-hs` as `PG`
Result of executing the following commands:
```shell
# replace "as Q" imports with "as PG" (in retrospect this didn't need a regex)
git grep -lE 'as Q($|[^a-zA-Z])' -- '*.hs' | xargs sed -i -E 's/as Q($|[^a-zA-Z])/as PG\1/'
# replace " Q." with " PG."
git grep -lE ' Q\.' -- '*.hs' | xargs sed -i 's/ Q\./ PG./g'
# replace "(Q." with "(PG."
git grep -lE '\(Q\.' -- '*.hs' | xargs sed -i 's/(Q\./(PG./g'
# ditto, but for [, |, { and !
git grep -lE '\[Q\.' -- '*.hs' | xargs sed -i 's/\[Q\./\[PG./g'
git grep -l '|Q\.' -- '*.hs' | xargs sed -i 's/|Q\./|PG./g'
git grep -l '{Q\.' -- '*.hs' | xargs sed -i 's/{Q\./{PG./g'
git grep -l '!Q\.' -- '*.hs' | xargs sed -i 's/!Q\./!PG./g'
```
(Doing the `grep -l` before the `sed`, instead of `sed` on the entire codebase, reduces the number of `mtime` updates, and so reduces how many times a file gets recompiled while checking intermediate results.)
Finally, I manually removed a broken and unused `Arbitrary` instance in `Hasura.RQL.Network`. (It used an `import Test.QuickCheck.Arbitrary as Q` statement, which was erroneously caught by the first find-replace command.)
After this PR, `Q` is no longer used as an import qualifier. That was not the goal of this PR, but perhaps it's a useful fact for future efforts.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5933
GitOrigin-RevId: 8c84c59d57789111d40f5d3322c5a885dcfbf40e
2022-09-20 22:54:43 +03:00
|
|
|
newtype MultiplexedQuery = MultiplexedQuery {unMultiplexedQuery :: PG.Query}
|
2021-02-20 16:45:49 +03:00
|
|
|
deriving (Eq, Hashable)
|
|
|
|
|
|
|
|
instance ToTxt MultiplexedQuery where
|
Import `pg-client-hs` as `PG`
Result of executing the following commands:
```shell
# replace "as Q" imports with "as PG" (in retrospect this didn't need a regex)
git grep -lE 'as Q($|[^a-zA-Z])' -- '*.hs' | xargs sed -i -E 's/as Q($|[^a-zA-Z])/as PG\1/'
# replace " Q." with " PG."
git grep -lE ' Q\.' -- '*.hs' | xargs sed -i 's/ Q\./ PG./g'
# replace "(Q." with "(PG."
git grep -lE '\(Q\.' -- '*.hs' | xargs sed -i 's/(Q\./(PG./g'
# ditto, but for [, |, { and !
git grep -lE '\[Q\.' -- '*.hs' | xargs sed -i 's/\[Q\./\[PG./g'
git grep -l '|Q\.' -- '*.hs' | xargs sed -i 's/|Q\./|PG./g'
git grep -l '{Q\.' -- '*.hs' | xargs sed -i 's/{Q\./{PG./g'
git grep -l '!Q\.' -- '*.hs' | xargs sed -i 's/!Q\./!PG./g'
```
(Doing the `grep -l` before the `sed`, instead of `sed` on the entire codebase, reduces the number of `mtime` updates, and so reduces how many times a file gets recompiled while checking intermediate results.)
Finally, I manually removed a broken and unused `Arbitrary` instance in `Hasura.RQL.Network`. (It used an `import Test.QuickCheck.Arbitrary as Q` statement, which was erroneously caught by the first find-replace command.)
After this PR, `Q` is no longer used as an import qualifier. That was not the goal of this PR, but perhaps it's a useful fact for future efforts.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5933
GitOrigin-RevId: 8c84c59d57789111d40f5d3322c5a885dcfbf40e
2022-09-20 22:54:43 +03:00
|
|
|
toTxt = PG.getQueryText . unMultiplexedQuery
|
2021-02-20 16:45:49 +03:00
|
|
|
|
2021-04-22 00:44:37 +03:00
|
|
|
toSQLFromItem ::
|
2021-05-21 05:46:58 +03:00
|
|
|
( Backend ('Postgres pgKind),
|
2023-01-27 13:03:47 +03:00
|
|
|
DS.PostgresAnnotatedFieldJSON pgKind
|
2021-05-21 05:46:58 +03:00
|
|
|
) =>
|
2022-07-18 12:44:17 +03:00
|
|
|
S.TableAlias ->
|
2021-12-07 16:12:02 +03:00
|
|
|
QueryDB ('Postgres pgKind) Void S.SQLExp ->
|
2023-01-27 13:03:47 +03:00
|
|
|
S.FromItem
|
|
|
|
toSQLFromItem = flip \case
|
|
|
|
QDBSingleRow s -> S.mkSelFromItem $ DS.mkSQLSelect JASSingleObject s
|
|
|
|
QDBMultipleRows s -> S.mkSelFromItem $ DS.mkSQLSelect JASMultipleRows s
|
|
|
|
QDBAggregation s -> S.mkSelFromItem $ DS.mkAggregateSelect s
|
|
|
|
QDBConnection s -> S.mkSelectWithFromItem $ DS.mkConnectionSelect s
|
|
|
|
QDBStreamMultipleRows s -> S.mkSelFromItem $ DS.mkStreamSQLSelect s
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2021-04-22 00:44:37 +03:00
|
|
|
mkMultiplexedQuery ::
|
2021-05-21 05:46:58 +03:00
|
|
|
( Backend ('Postgres pgKind),
|
|
|
|
DS.PostgresAnnotatedFieldJSON pgKind
|
|
|
|
) =>
|
2021-12-07 16:12:02 +03:00
|
|
|
OMap.InsOrdHashMap G.Name (QueryDB ('Postgres pgKind) Void S.SQLExp) ->
|
2021-04-22 00:44:37 +03:00
|
|
|
MultiplexedQuery
|
2021-02-20 16:45:49 +03:00
|
|
|
mkMultiplexedQuery rootFields =
|
2023-01-27 13:03:47 +03:00
|
|
|
MultiplexedQuery . PG.fromBuilder . toSQL $
|
|
|
|
S.mkSelect
|
|
|
|
{ S.selExtr =
|
|
|
|
-- SELECT _subs.result_id, _fld_resp.root AS result
|
|
|
|
[ S.Extractor (mkQualifiedIdentifier subsIdentifier (Identifier "result_id")) Nothing,
|
|
|
|
S.Extractor (mkQualifiedIdentifier fldRespIdentifier (Identifier "root")) (Just $ S.toColumnAlias $ Identifier "result")
|
|
|
|
],
|
|
|
|
S.selFrom =
|
|
|
|
Just $
|
|
|
|
S.FromExp
|
|
|
|
[ S.FIJoin $
|
|
|
|
S.JoinExpr subsInputFromItem S.LeftOuter responseLateralFromItem (S.JoinOn $ S.BELit True)
|
|
|
|
]
|
|
|
|
}
|
2021-02-20 16:45:49 +03:00
|
|
|
where
|
|
|
|
-- FROM unnest($1::uuid[], $2::json[]) _subs (result_id, result_vars)
|
|
|
|
subsInputFromItem =
|
|
|
|
S.FIUnnest
|
|
|
|
[S.SEPrep 1 `S.SETyAnn` S.TypeAnn "uuid[]", S.SEPrep 2 `S.SETyAnn` S.TypeAnn "json[]"]
|
2023-01-27 13:03:47 +03:00
|
|
|
(S.toTableAlias $ Identifier "_subs")
|
2022-07-18 12:44:17 +03:00
|
|
|
[S.toColumnAlias $ Identifier "result_id", S.toColumnAlias $ Identifier "result_vars"]
|
2021-02-20 16:45:49 +03:00
|
|
|
|
|
|
|
-- LEFT OUTER JOIN LATERAL ( ... ) _fld_resp
|
2022-10-11 13:42:15 +03:00
|
|
|
responseLateralFromItem = S.mkLateralFromItem selectRootFields fldRespAlias
|
2021-02-20 16:45:49 +03:00
|
|
|
selectRootFields =
|
|
|
|
S.mkSelect
|
2022-07-18 12:44:17 +03:00
|
|
|
{ S.selExtr = [S.Extractor rootFieldsJsonAggregate (Just $ S.toColumnAlias $ Identifier "root")],
|
2021-02-20 16:45:49 +03:00
|
|
|
S.selFrom =
|
2023-01-27 13:03:47 +03:00
|
|
|
Just . S.FromExp $
|
|
|
|
OMap.toList rootFields <&> \(fieldAlias, resolvedAST) ->
|
|
|
|
toSQLFromItem (S.mkTableAlias $ G.unName fieldAlias) resolvedAST
|
2021-02-20 16:45:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
-- json_build_object('field1', field1.root, 'field2', field2.root, ...)
|
|
|
|
rootFieldsJsonAggregate = S.SEFnApp "json_build_object" rootFieldsJsonPairs Nothing
|
|
|
|
rootFieldsJsonPairs = flip concatMap (OMap.keys rootFields) $ \fieldAlias ->
|
|
|
|
[ S.SELit (G.unName fieldAlias),
|
|
|
|
mkQualifiedIdentifier (aliasToIdentifier fieldAlias) (Identifier "root")
|
|
|
|
]
|
|
|
|
|
2022-04-22 22:53:12 +03:00
|
|
|
mkQualifiedIdentifier prefix = S.SEQIdentifier . S.QIdentifier (S.QualifiedIdentifier prefix Nothing)
|
2022-10-11 13:42:15 +03:00
|
|
|
aliasToIdentifier = TableIdentifier . G.unName
|
2021-02-20 16:45:49 +03:00
|
|
|
|
2022-04-07 17:41:43 +03:00
|
|
|
mkStreamingMultiplexedQuery ::
|
|
|
|
( Backend ('Postgres pgKind),
|
|
|
|
DS.PostgresAnnotatedFieldJSON pgKind
|
|
|
|
) =>
|
|
|
|
(G.Name, (QueryDB ('Postgres pgKind) Void S.SQLExp)) ->
|
|
|
|
MultiplexedQuery
|
|
|
|
mkStreamingMultiplexedQuery (fieldAlias, resolvedAST) =
|
2023-01-27 13:03:47 +03:00
|
|
|
MultiplexedQuery . PG.fromBuilder . toSQL $
|
|
|
|
S.mkSelect
|
|
|
|
{ S.selExtr =
|
|
|
|
-- SELECT _subs.result_id, _fld_resp.root, _fld_resp.cursor AS result
|
|
|
|
[ S.Extractor (mkQualifiedIdentifier subsIdentifier (Identifier "result_id")) Nothing,
|
|
|
|
S.Extractor (mkQualifiedIdentifier fldRespIdentifier (Identifier "root")) (Just $ S.toColumnAlias $ Identifier "result"),
|
|
|
|
S.Extractor (mkQualifiedIdentifier fldRespIdentifier (Identifier "cursor")) (Just $ S.toColumnAlias $ Identifier "cursor")
|
|
|
|
],
|
|
|
|
S.selFrom =
|
|
|
|
Just $
|
|
|
|
S.FromExp
|
|
|
|
[ S.FIJoin $
|
|
|
|
S.JoinExpr subsInputFromItem S.LeftOuter responseLateralFromItem (S.JoinOn $ S.BELit True)
|
|
|
|
]
|
|
|
|
}
|
2022-04-07 17:41:43 +03:00
|
|
|
where
|
|
|
|
-- FROM unnest($1::uuid[], $2::json[]) _subs (result_id, result_vars)
|
|
|
|
subsInputFromItem =
|
|
|
|
S.FIUnnest
|
|
|
|
[S.SEPrep 1 `S.SETyAnn` S.TypeAnn "uuid[]", S.SEPrep 2 `S.SETyAnn` S.TypeAnn "json[]"]
|
2022-10-11 13:42:15 +03:00
|
|
|
subsAlias
|
|
|
|
[resultIdAlias, resultVarsAlias]
|
2022-04-07 17:41:43 +03:00
|
|
|
|
|
|
|
-- LEFT OUTER JOIN LATERAL ( ... ) _fld_resp
|
2023-01-27 13:03:47 +03:00
|
|
|
responseLateralFromItem = S.mkLateralFromItem selectRootFields (S.toTableAlias $ Identifier "_fld_resp")
|
2022-04-07 17:41:43 +03:00
|
|
|
selectRootFields =
|
|
|
|
S.mkSelect
|
2022-07-18 12:44:17 +03:00
|
|
|
{ S.selExtr = [(S.Extractor rootFieldJsonAggregate (Just $ S.toColumnAlias $ Identifier "root")), cursorExtractor],
|
2022-04-07 17:41:43 +03:00
|
|
|
S.selFrom =
|
2023-01-27 13:03:47 +03:00
|
|
|
Just . S.FromExp $
|
|
|
|
pure $
|
|
|
|
toSQLFromItem (S.mkTableAlias $ G.unName fieldAlias) resolvedAST
|
2022-04-07 17:41:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
-- json_build_object('field1', field1.root, 'field2', field2.root, ...)
|
|
|
|
rootFieldJsonAggregate = S.SEFnApp "json_build_object" rootFieldJsonPair Nothing
|
|
|
|
rootFieldJsonPair =
|
|
|
|
[ S.SELit (G.unName fieldAlias),
|
|
|
|
mkQualifiedIdentifier (aliasToIdentifier fieldAlias) (Identifier "root")
|
|
|
|
]
|
|
|
|
|
|
|
|
-- to_json("root"."cursor") AS "cursor"
|
|
|
|
cursorSQLExp = S.SEFnApp "to_json" [mkQualifiedIdentifier (aliasToIdentifier fieldAlias) (Identifier "cursor")] Nothing
|
2022-07-18 12:44:17 +03:00
|
|
|
cursorExtractor = S.Extractor cursorSQLExp (Just $ S.toColumnAlias $ Identifier "cursor")
|
2022-04-07 17:41:43 +03:00
|
|
|
mkQualifiedIdentifier prefix = S.SEQIdentifier . S.QIdentifier (S.QualifiedIdentifier prefix Nothing)
|
2022-10-11 13:42:15 +03:00
|
|
|
aliasToIdentifier = TableIdentifier . G.unName
|
2022-04-07 17:41:43 +03:00
|
|
|
|
2021-02-20 16:45:49 +03:00
|
|
|
-- | Resolves an 'GR.UnresolvedVal' by converting 'GR.UVPG' values to SQL
|
|
|
|
-- expressions that refer to the @result_vars@ input object, collecting information
|
|
|
|
-- about various parameters of the query along the way.
|
|
|
|
resolveMultiplexedValue ::
|
2021-07-31 00:41:52 +03:00
|
|
|
( MonadState (QueryParametersInfo ('Postgres pgKind)) m,
|
|
|
|
MonadError QErr m
|
|
|
|
) =>
|
|
|
|
SessionVariables ->
|
|
|
|
UnpreparedValue ('Postgres pgKind) ->
|
|
|
|
m S.SQLExp
|
2021-05-24 10:33:33 +03:00
|
|
|
resolveMultiplexedValue allSessionVars = \case
|
2021-02-20 16:45:49 +03:00
|
|
|
UVParameter varM colVal -> do
|
server: Metadata origin for definitions (type parameter version v2)
The code that builds the GraphQL schema, and `buildGQLContext` in particular, is partial: not every value of `(ServerConfigCtx, GraphQLQueryType, SourceCache, HashMap RemoteSchemaName (RemoteSchemaCtx, MetadataObject), ActionCache, AnnotatedCustomTypes)` results in a valid GraphQL schema. When it fails, we want to be able to return better error messages than we currently do.
The key thing that is missing is a way to trace back GraphQL type information to their origin from the Hasura metadata. Currently, we have a number of correctness checks of our GraphQL schema. But these correctness checks only have access to pure GraphQL type information, and hence can only report errors in terms of that. Possibly the worst is the "conflicting definitions" error, which, in practice, can only be debugged by Hasura engineers. This is terrible DX for customers.
This PR allows us to print better error messages, by adding a field to the `Definition` type that traces the GraphQL type to its origin in the metadata. So the idea is simple: just add `MetadataObjId`, or `Maybe` that, or some other sum type of that, to `Definition`.
However, we want to avoid having to import a `Hasura.RQL` module from `Hasura.GraphQL.Parser`. So we instead define this additional field of `Definition` through a new type parameter, which is threaded through in `Hasura.GraphQL.Parser`. We then define type synonyms in `Hasura.GraphQL.Schema.Parser` that fill in this type parameter, so that it is not visible for the majority of the codebase.
The idea of associating metadata information to `Definition`s really comes to fruition when combined with hasura/graphql-engine-mono#4517. Their combination would allow us to use the API of fatal errors (just like the current `MonadError QErr`) to report _inconsistencies_ in the metadata. Such inconsistencies are then _automatically_ ignored. So no ad-hoc decisions need to be made on how to cut out inconsistent metadata from the GraphQL schema. This will allow us to report much better errors, as well as improve the likelihood of a successful HGE startup.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/4770
Co-authored-by: Samir Talwar <47582+SamirTalwar@users.noreply.github.com>
GitOrigin-RevId: 728402b0cae83ae8e83463a826ceeb609001acae
2022-06-28 18:52:26 +03:00
|
|
|
varJsonPath <- case fmap getName varM of
|
2021-02-20 16:45:49 +03:00
|
|
|
Just varName -> do
|
|
|
|
modifying qpiReusableVariableValues $ Map.insert varName colVal
|
|
|
|
pure ["query", G.unName varName]
|
|
|
|
Nothing -> do
|
|
|
|
syntheticVarIndex <- use (qpiSyntheticVariableValues . to length)
|
|
|
|
modifying qpiSyntheticVariableValues (|> colVal)
|
|
|
|
pure ["synthetic", tshow syntheticVarIndex]
|
|
|
|
pure $ fromResVars (CollectableTypeScalar $ unsafePGColumnToBackend $ cvType colVal) varJsonPath
|
|
|
|
UVSessionVar ty sessVar -> do
|
2021-07-31 00:41:52 +03:00
|
|
|
_ <-
|
|
|
|
getSessionVariableValue sessVar allSessionVars
|
|
|
|
`onNothing` throw400
|
|
|
|
NotFound
|
|
|
|
("missing session variable: " <>> sessionVariableToText sessVar)
|
2021-02-20 16:45:49 +03:00
|
|
|
modifying qpiReferencedSessionVariables (Set.insert sessVar)
|
|
|
|
pure $ fromResVars ty ["session", sessionVariableToText sessVar]
|
|
|
|
UVLiteral sqlExp -> pure sqlExp
|
2021-05-24 10:33:33 +03:00
|
|
|
UVSession -> do
|
|
|
|
-- if the entire session is referenced, then add all session vars in referenced vars
|
|
|
|
modifying qpiReferencedSessionVariables (const $ getSessionVariablesSet allSessionVars)
|
|
|
|
pure $ fromResVars (CollectableTypeScalar PGJSON) ["session"]
|
2021-02-20 16:45:49 +03:00
|
|
|
where
|
|
|
|
fromResVars pgType jPath =
|
|
|
|
addTypeAnnotation pgType $
|
|
|
|
S.SEOpApp
|
|
|
|
(S.SQLOp "#>>")
|
2022-10-11 13:42:15 +03:00
|
|
|
[ S.SEQIdentifier $ S.QIdentifier (S.QualifiedIdentifier subsIdentifier Nothing) (Identifier "result_vars"),
|
2021-02-20 16:45:49 +03:00
|
|
|
S.SEArray $ map S.SELit jPath
|
|
|
|
]
|
|
|
|
addTypeAnnotation pgType =
|
|
|
|
flip S.SETyAnn (S.mkTypeAnn pgType) . case pgType of
|
|
|
|
CollectableTypeScalar scalarType -> withConstructorFn scalarType
|
|
|
|
CollectableTypeArray _ -> id
|
|
|
|
|
|
|
|
----------------------------------------------------------------------------------------------------
|
|
|
|
-- Execution
|
|
|
|
|
|
|
|
executeMultiplexedQuery ::
|
|
|
|
(MonadTx m) =>
|
|
|
|
MultiplexedQuery ->
|
|
|
|
[(CohortId, CohortVariables)] ->
|
|
|
|
m [(CohortId, B.ByteString)]
|
2022-04-22 22:53:12 +03:00
|
|
|
executeMultiplexedQuery (MultiplexedQuery query) cohorts =
|
|
|
|
executeQuery query cohorts
|
2021-02-20 16:45:49 +03:00
|
|
|
|
2022-04-07 17:41:43 +03:00
|
|
|
executeStreamingMultiplexedQuery ::
|
|
|
|
(MonadTx m) =>
|
|
|
|
MultiplexedQuery ->
|
|
|
|
[(CohortId, CohortVariables)] ->
|
2022-09-21 21:40:41 +03:00
|
|
|
m [(CohortId, B.ByteString, PG.ViaJSON CursorVariableValues)]
|
2022-04-07 17:41:43 +03:00
|
|
|
executeStreamingMultiplexedQuery (MultiplexedQuery query) cohorts = do
|
|
|
|
executeQuery query cohorts
|
|
|
|
|
|
|
|
-- | Internal; used by both 'executeMultiplexedQuery', 'executeStreamingMultiplexedQuery'
|
|
|
|
-- and 'pgDBSubscriptionExplain'.
|
2021-02-20 16:45:49 +03:00
|
|
|
executeQuery ::
|
2022-12-22 20:08:04 +03:00
|
|
|
(MonadTx m, PG.FromRes a) =>
|
Import `pg-client-hs` as `PG`
Result of executing the following commands:
```shell
# replace "as Q" imports with "as PG" (in retrospect this didn't need a regex)
git grep -lE 'as Q($|[^a-zA-Z])' -- '*.hs' | xargs sed -i -E 's/as Q($|[^a-zA-Z])/as PG\1/'
# replace " Q." with " PG."
git grep -lE ' Q\.' -- '*.hs' | xargs sed -i 's/ Q\./ PG./g'
# replace "(Q." with "(PG."
git grep -lE '\(Q\.' -- '*.hs' | xargs sed -i 's/(Q\./(PG./g'
# ditto, but for [, |, { and !
git grep -lE '\[Q\.' -- '*.hs' | xargs sed -i 's/\[Q\./\[PG./g'
git grep -l '|Q\.' -- '*.hs' | xargs sed -i 's/|Q\./|PG./g'
git grep -l '{Q\.' -- '*.hs' | xargs sed -i 's/{Q\./{PG./g'
git grep -l '!Q\.' -- '*.hs' | xargs sed -i 's/!Q\./!PG./g'
```
(Doing the `grep -l` before the `sed`, instead of `sed` on the entire codebase, reduces the number of `mtime` updates, and so reduces how many times a file gets recompiled while checking intermediate results.)
Finally, I manually removed a broken and unused `Arbitrary` instance in `Hasura.RQL.Network`. (It used an `import Test.QuickCheck.Arbitrary as Q` statement, which was erroneously caught by the first find-replace command.)
After this PR, `Q` is no longer used as an import qualifier. That was not the goal of this PR, but perhaps it's a useful fact for future efforts.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5933
GitOrigin-RevId: 8c84c59d57789111d40f5d3322c5a885dcfbf40e
2022-09-20 22:54:43 +03:00
|
|
|
PG.Query ->
|
2021-02-20 16:45:49 +03:00
|
|
|
[(CohortId, CohortVariables)] ->
|
2022-12-22 20:08:04 +03:00
|
|
|
m a
|
2021-02-20 16:45:49 +03:00
|
|
|
executeQuery query cohorts =
|
|
|
|
let (cohortIds, cohortVars) = unzip cohorts
|
|
|
|
preparedArgs = (CohortIdArray cohortIds, CohortVariablesArray cohortVars)
|
2022-10-07 14:55:42 +03:00
|
|
|
in liftTx $ PG.withQE defaultTxErrorHandler query preparedArgs True
|