graphql-engine/server/src-lib/Hasura/RQL/IR/Root.hs
Auke Booij 79b8a6a07b chore(server): move some query tags code to a sensible place
Also add a `default` implementation for `MonadQueryTags`.

This avoids a bunch of imports on `Hasura.GraphQL.Execute.Backend` which is a big module with lots of (transitive) dependencies.

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/8571
GitOrigin-RevId: 8ecca452721b77953e6d088c79d8d6f003f2996f
2023-03-30 21:19:38 +00:00

113 lines
4.1 KiB
Haskell

{-# LANGUAGE UndecidableInstances #-}
module Hasura.RQL.IR.Root
( SourceConfigWith (..),
RootField (..),
MutationDB (..),
ActionQuery (..),
ActionMutation (..),
QueryRootField,
MutationRootField,
SubscriptionRootField,
QueryDBRoot (..),
MutationDBRoot (..),
RemoteRelationshipField (..),
)
where
import Data.Aeson.Ordered qualified as JO
import Data.Kind (Type)
import Hasura.Prelude
import Hasura.QueryTags.Types qualified as RQL
import Hasura.RQL.IR.Action
import Hasura.RQL.IR.Delete
import Hasura.RQL.IR.Insert
import Hasura.RQL.IR.RemoteSchema
import Hasura.RQL.IR.Select
import Hasura.RQL.IR.Update
import Hasura.RQL.Types.Backend qualified as RQL
import Hasura.RQL.Types.Common qualified as RQL
import Hasura.RemoteSchema.SchemaCache.Types qualified as RQL
import Hasura.SQL.AnyBackend qualified as AB
import Hasura.SQL.Backend
data SourceConfigWith (db :: BackendType -> Type) (b :: BackendType)
= SourceConfigWith (RQL.SourceConfig b) (Maybe RQL.QueryTagsConfig) (db b)
data RootField (db :: BackendType -> Type) remote action raw where
RFDB ::
RQL.SourceName ->
AB.AnyBackend (SourceConfigWith db) ->
RootField db remote action raw
RFRemote :: remote -> RootField db remote action raw
RFAction :: action -> RootField db remote action raw
RFRaw :: raw -> RootField db remote action raw
RFMulti :: [RootField db remote action raw] -> RootField db remote action raw
data MutationDB (b :: BackendType) (r :: Type) v
= MDBInsert (AnnotatedInsert b r v)
| MDBUpdate (AnnotatedUpdateG b r v)
| MDBDelete (AnnDelG b r v)
| -- | This represents a VOLATILE function, and is AnnSimpleSelG for easy
-- re-use of non-VOLATILE function tracking code.
MDBFunction RQL.JsonAggSelect (AnnSimpleSelectG b r v)
deriving stock (Generic, Functor, Foldable, Traversable)
data ActionQuery (r :: Type)
= AQQuery (AnnActionExecution r)
| AQAsync (AnnActionAsyncQuery ('Postgres 'Vanilla) r)
deriving stock (Functor, Foldable, Traversable)
data ActionMutation (r :: Type)
= AMSync (AnnActionExecution r)
| AMAsync AnnActionMutationAsync
-- The `db` type argument of @RootField@ expects only one type argument, the backend `b`, as not all
-- types stored in a RootField will have a second parameter like @QueryDB@ does: they all only have
-- in common the fact that they're parametric over the backend. To define @QueryRootField@ in terms
-- of @QueryDB@ (and likewise for mutations), we need a type-level function `b -> QueryDB b (v
-- b)`. Sadly, neither type synonyms nor type families may be partially applied. Hence the need for
-- @QueryDBRoot@ and @MutationDBRoot@.
newtype QueryDBRoot r v b = QDBR (QueryDB b r (v b))
newtype MutationDBRoot r v b = MDBR (MutationDB b r (v b))
-- | IR of a remote relationship. A remote relationship currently can be to
-- either a remote schema or a database's table. See RemoteSourceSelect for
-- explanation on 'vf'.
data RemoteRelationshipField vf
= RemoteSchemaField (RemoteSchemaSelect (RemoteRelationshipField vf))
| -- | AnyBackend is used here to capture a relationship to an arbitrary target
RemoteSourceField (AB.AnyBackend (RemoteSourceSelect (RemoteRelationshipField vf) vf))
deriving instance AB.SatisfiesForAllBackends vf Show => Show (RemoteRelationshipField vf)
-- | Represents a query root field to an action
type QueryActionRoot v =
ActionQuery (RemoteRelationshipField v)
-- | Represents a mutation root field to an action
type MutationActionRoot v =
ActionMutation (RemoteRelationshipField v)
type QueryRootField v =
RootField
(QueryDBRoot (RemoteRelationshipField v) v)
(RemoteSchemaRootField (RemoteRelationshipField v) RQL.RemoteSchemaVariable)
(QueryActionRoot v)
JO.Value
type MutationRootField v =
RootField
(MutationDBRoot (RemoteRelationshipField v) v)
(RemoteSchemaRootField (RemoteRelationshipField v) RQL.RemoteSchemaVariable)
(MutationActionRoot v)
JO.Value
type SubscriptionRootField v =
RootField
(QueryDBRoot (RemoteRelationshipField v) v)
(RemoteSchemaRootField (RemoteRelationshipField v) RQL.RemoteSchemaVariable)
(QueryActionRoot v)
JO.Value