{-# 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.BackendType
import Hasura.RQL.Types.Common qualified as RQL
import Hasura.RemoteSchema.SchemaCache.Types qualified as RQL
import Hasura.SQL.AnyBackend qualified as AB

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 ((forall x. MutationDB b r v -> Rep (MutationDB b r v) x)
-> (forall x. Rep (MutationDB b r v) x -> MutationDB b r v)
-> Generic (MutationDB b r v)
forall x. Rep (MutationDB b r v) x -> MutationDB b r v
forall x. MutationDB b r v -> Rep (MutationDB b r v) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (b :: BackendType) r v x.
Rep (MutationDB b r v) x -> MutationDB b r v
forall (b :: BackendType) r v x.
MutationDB b r v -> Rep (MutationDB b r v) x
$cfrom :: forall (b :: BackendType) r v x.
MutationDB b r v -> Rep (MutationDB b r v) x
from :: forall x. MutationDB b r v -> Rep (MutationDB b r v) x
$cto :: forall (b :: BackendType) r v x.
Rep (MutationDB b r v) x -> MutationDB b r v
to :: forall x. Rep (MutationDB b r v) x -> MutationDB b r v
Generic, (forall a b. (a -> b) -> MutationDB b r a -> MutationDB b r b)
-> (forall a b. a -> MutationDB b r b -> MutationDB b r a)
-> Functor (MutationDB b r)
forall a b. a -> MutationDB b r b -> MutationDB b r a
forall a b. (a -> b) -> MutationDB b r a -> MutationDB b r b
forall (b :: BackendType) r a b.
Backend b =>
a -> MutationDB b r b -> MutationDB b r a
forall (b :: BackendType) r a b.
Backend b =>
(a -> b) -> MutationDB b r a -> MutationDB b r b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall (b :: BackendType) r a b.
Backend b =>
(a -> b) -> MutationDB b r a -> MutationDB b r b
fmap :: forall a b. (a -> b) -> MutationDB b r a -> MutationDB b r b
$c<$ :: forall (b :: BackendType) r a b.
Backend b =>
a -> MutationDB b r b -> MutationDB b r a
<$ :: forall a b. a -> MutationDB b r b -> MutationDB b r a
Functor, (forall m. Monoid m => MutationDB b r m -> m)
-> (forall m a. Monoid m => (a -> m) -> MutationDB b r a -> m)
-> (forall m a. Monoid m => (a -> m) -> MutationDB b r a -> m)
-> (forall a b. (a -> b -> b) -> b -> MutationDB b r a -> b)
-> (forall a b. (a -> b -> b) -> b -> MutationDB b r a -> b)
-> (forall b a. (b -> a -> b) -> b -> MutationDB b r a -> b)
-> (forall b a. (b -> a -> b) -> b -> MutationDB b r a -> b)
-> (forall a. (a -> a -> a) -> MutationDB b r a -> a)
-> (forall a. (a -> a -> a) -> MutationDB b r a -> a)
-> (forall a. MutationDB b r a -> [a])
-> (forall a. MutationDB b r a -> Bool)
-> (forall a. MutationDB b r a -> Int)
-> (forall a. Eq a => a -> MutationDB b r a -> Bool)
-> (forall a. Ord a => MutationDB b r a -> a)
-> (forall a. Ord a => MutationDB b r a -> a)
-> (forall a. Num a => MutationDB b r a -> a)
-> (forall a. Num a => MutationDB b r a -> a)
-> Foldable (MutationDB b r)
forall a. Eq a => a -> MutationDB b r a -> Bool
forall a. Num a => MutationDB b r a -> a
forall a. Ord a => MutationDB b r a -> a
forall m. Monoid m => MutationDB b r m -> m
forall a. MutationDB b r a -> Bool
forall a. MutationDB b r a -> Int
forall a. MutationDB b r a -> [a]
forall a. (a -> a -> a) -> MutationDB b r a -> a
forall m a. Monoid m => (a -> m) -> MutationDB b r a -> m
forall b a. (b -> a -> b) -> b -> MutationDB b r a -> b
forall a b. (a -> b -> b) -> b -> MutationDB b r a -> b
forall (b :: BackendType) r a.
(Backend b, Eq a) =>
a -> MutationDB b r a -> Bool
forall (b :: BackendType) r a.
(Backend b, Num a) =>
MutationDB b r a -> a
forall (b :: BackendType) r a.
(Backend b, Ord a) =>
MutationDB b r a -> a
forall (b :: BackendType) r m.
(Backend b, Monoid m) =>
MutationDB b r m -> m
forall (b :: BackendType) r a.
Backend b =>
MutationDB b r a -> Bool
forall (b :: BackendType) r a. Backend b => MutationDB b r a -> Int
forall (b :: BackendType) r a. Backend b => MutationDB b r a -> [a]
forall (b :: BackendType) r a.
Backend b =>
(a -> a -> a) -> MutationDB b r a -> a
forall (b :: BackendType) r m a.
(Backend b, Monoid m) =>
(a -> m) -> MutationDB b r a -> m
forall (b :: BackendType) r b a.
Backend b =>
(b -> a -> b) -> b -> MutationDB b r a -> b
forall (b :: BackendType) r a b.
Backend b =>
(a -> b -> b) -> b -> MutationDB b r a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
$cfold :: forall (b :: BackendType) r m.
(Backend b, Monoid m) =>
MutationDB b r m -> m
fold :: forall m. Monoid m => MutationDB b r m -> m
$cfoldMap :: forall (b :: BackendType) r m a.
(Backend b, Monoid m) =>
(a -> m) -> MutationDB b r a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> MutationDB b r a -> m
$cfoldMap' :: forall (b :: BackendType) r m a.
(Backend b, Monoid m) =>
(a -> m) -> MutationDB b r a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> MutationDB b r a -> m
$cfoldr :: forall (b :: BackendType) r a b.
Backend b =>
(a -> b -> b) -> b -> MutationDB b r a -> b
foldr :: forall a b. (a -> b -> b) -> b -> MutationDB b r a -> b
$cfoldr' :: forall (b :: BackendType) r a b.
Backend b =>
(a -> b -> b) -> b -> MutationDB b r a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> MutationDB b r a -> b
$cfoldl :: forall (b :: BackendType) r b a.
Backend b =>
(b -> a -> b) -> b -> MutationDB b r a -> b
foldl :: forall b a. (b -> a -> b) -> b -> MutationDB b r a -> b
$cfoldl' :: forall (b :: BackendType) r b a.
Backend b =>
(b -> a -> b) -> b -> MutationDB b r a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> MutationDB b r a -> b
$cfoldr1 :: forall (b :: BackendType) r a.
Backend b =>
(a -> a -> a) -> MutationDB b r a -> a
foldr1 :: forall a. (a -> a -> a) -> MutationDB b r a -> a
$cfoldl1 :: forall (b :: BackendType) r a.
Backend b =>
(a -> a -> a) -> MutationDB b r a -> a
foldl1 :: forall a. (a -> a -> a) -> MutationDB b r a -> a
$ctoList :: forall (b :: BackendType) r a. Backend b => MutationDB b r a -> [a]
toList :: forall a. MutationDB b r a -> [a]
$cnull :: forall (b :: BackendType) r a.
Backend b =>
MutationDB b r a -> Bool
null :: forall a. MutationDB b r a -> Bool
$clength :: forall (b :: BackendType) r a. Backend b => MutationDB b r a -> Int
length :: forall a. MutationDB b r a -> Int
$celem :: forall (b :: BackendType) r a.
(Backend b, Eq a) =>
a -> MutationDB b r a -> Bool
elem :: forall a. Eq a => a -> MutationDB b r a -> Bool
$cmaximum :: forall (b :: BackendType) r a.
(Backend b, Ord a) =>
MutationDB b r a -> a
maximum :: forall a. Ord a => MutationDB b r a -> a
$cminimum :: forall (b :: BackendType) r a.
(Backend b, Ord a) =>
MutationDB b r a -> a
minimum :: forall a. Ord a => MutationDB b r a -> a
$csum :: forall (b :: BackendType) r a.
(Backend b, Num a) =>
MutationDB b r a -> a
sum :: forall a. Num a => MutationDB b r a -> a
$cproduct :: forall (b :: BackendType) r a.
(Backend b, Num a) =>
MutationDB b r a -> a
product :: forall a. Num a => MutationDB b r a -> a
Foldable, Functor (MutationDB b r)
Foldable (MutationDB b r)
Functor (MutationDB b r)
-> Foldable (MutationDB b r)
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> MutationDB b r a -> f (MutationDB b r b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    MutationDB b r (f a) -> f (MutationDB b r a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> MutationDB b r a -> m (MutationDB b r b))
-> (forall (m :: * -> *) a.
    Monad m =>
    MutationDB b r (m a) -> m (MutationDB b r a))
-> Traversable (MutationDB b r)
forall (b :: BackendType) r. Backend b => Functor (MutationDB b r)
forall (b :: BackendType) r. Backend b => Foldable (MutationDB b r)
forall (b :: BackendType) r (m :: * -> *) a.
(Backend b, Monad m) =>
MutationDB b r (m a) -> m (MutationDB b r a)
forall (b :: BackendType) r (f :: * -> *) a.
(Backend b, Applicative f) =>
MutationDB b r (f a) -> f (MutationDB b r a)
forall (b :: BackendType) r (m :: * -> *) a b.
(Backend b, Monad m) =>
(a -> m b) -> MutationDB b r a -> m (MutationDB b r b)
forall (b :: BackendType) r (f :: * -> *) a b.
(Backend b, Applicative f) =>
(a -> f b) -> MutationDB b r a -> f (MutationDB b r b)
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a.
Monad m =>
MutationDB b r (m a) -> m (MutationDB b r a)
forall (f :: * -> *) a.
Applicative f =>
MutationDB b r (f a) -> f (MutationDB b r a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> MutationDB b r a -> m (MutationDB b r b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> MutationDB b r a -> f (MutationDB b r b)
$ctraverse :: forall (b :: BackendType) r (f :: * -> *) a b.
(Backend b, Applicative f) =>
(a -> f b) -> MutationDB b r a -> f (MutationDB b r b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> MutationDB b r a -> f (MutationDB b r b)
$csequenceA :: forall (b :: BackendType) r (f :: * -> *) a.
(Backend b, Applicative f) =>
MutationDB b r (f a) -> f (MutationDB b r a)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
MutationDB b r (f a) -> f (MutationDB b r a)
$cmapM :: forall (b :: BackendType) r (m :: * -> *) a b.
(Backend b, Monad m) =>
(a -> m b) -> MutationDB b r a -> m (MutationDB b r b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> MutationDB b r a -> m (MutationDB b r b)
$csequence :: forall (b :: BackendType) r (m :: * -> *) a.
(Backend b, Monad m) =>
MutationDB b r (m a) -> m (MutationDB b r a)
sequence :: forall (m :: * -> *) a.
Monad m =>
MutationDB b r (m a) -> m (MutationDB b r a)
Traversable)

data ActionQuery (r :: Type)
  = AQQuery (AnnActionExecution r)
  | AQAsync (AnnActionAsyncQuery ('Postgres 'Vanilla) r)
  deriving stock ((forall a b. (a -> b) -> ActionQuery a -> ActionQuery b)
-> (forall a b. a -> ActionQuery b -> ActionQuery a)
-> Functor ActionQuery
forall a b. a -> ActionQuery b -> ActionQuery a
forall a b. (a -> b) -> ActionQuery a -> ActionQuery b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> ActionQuery a -> ActionQuery b
fmap :: forall a b. (a -> b) -> ActionQuery a -> ActionQuery b
$c<$ :: forall a b. a -> ActionQuery b -> ActionQuery a
<$ :: forall a b. a -> ActionQuery b -> ActionQuery a
Functor, (forall m. Monoid m => ActionQuery m -> m)
-> (forall m a. Monoid m => (a -> m) -> ActionQuery a -> m)
-> (forall m a. Monoid m => (a -> m) -> ActionQuery a -> m)
-> (forall a b. (a -> b -> b) -> b -> ActionQuery a -> b)
-> (forall a b. (a -> b -> b) -> b -> ActionQuery a -> b)
-> (forall b a. (b -> a -> b) -> b -> ActionQuery a -> b)
-> (forall b a. (b -> a -> b) -> b -> ActionQuery a -> b)
-> (forall a. (a -> a -> a) -> ActionQuery a -> a)
-> (forall a. (a -> a -> a) -> ActionQuery a -> a)
-> (forall a. ActionQuery a -> [a])
-> (forall a. ActionQuery a -> Bool)
-> (forall a. ActionQuery a -> Int)
-> (forall a. Eq a => a -> ActionQuery a -> Bool)
-> (forall a. Ord a => ActionQuery a -> a)
-> (forall a. Ord a => ActionQuery a -> a)
-> (forall a. Num a => ActionQuery a -> a)
-> (forall a. Num a => ActionQuery a -> a)
-> Foldable ActionQuery
forall a. Eq a => a -> ActionQuery a -> Bool
forall a. Num a => ActionQuery a -> a
forall a. Ord a => ActionQuery a -> a
forall m. Monoid m => ActionQuery m -> m
forall a. ActionQuery a -> Bool
forall a. ActionQuery a -> Int
forall a. ActionQuery a -> [a]
forall a. (a -> a -> a) -> ActionQuery a -> a
forall m a. Monoid m => (a -> m) -> ActionQuery a -> m
forall b a. (b -> a -> b) -> b -> ActionQuery a -> b
forall a b. (a -> b -> b) -> b -> ActionQuery a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
$cfold :: forall m. Monoid m => ActionQuery m -> m
fold :: forall m. Monoid m => ActionQuery m -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> ActionQuery a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> ActionQuery a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> ActionQuery a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> ActionQuery a -> m
$cfoldr :: forall a b. (a -> b -> b) -> b -> ActionQuery a -> b
foldr :: forall a b. (a -> b -> b) -> b -> ActionQuery a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> ActionQuery a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> ActionQuery a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> ActionQuery a -> b
foldl :: forall b a. (b -> a -> b) -> b -> ActionQuery a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> ActionQuery a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> ActionQuery a -> b
$cfoldr1 :: forall a. (a -> a -> a) -> ActionQuery a -> a
foldr1 :: forall a. (a -> a -> a) -> ActionQuery a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> ActionQuery a -> a
foldl1 :: forall a. (a -> a -> a) -> ActionQuery a -> a
$ctoList :: forall a. ActionQuery a -> [a]
toList :: forall a. ActionQuery a -> [a]
$cnull :: forall a. ActionQuery a -> Bool
null :: forall a. ActionQuery a -> Bool
$clength :: forall a. ActionQuery a -> Int
length :: forall a. ActionQuery a -> Int
$celem :: forall a. Eq a => a -> ActionQuery a -> Bool
elem :: forall a. Eq a => a -> ActionQuery a -> Bool
$cmaximum :: forall a. Ord a => ActionQuery a -> a
maximum :: forall a. Ord a => ActionQuery a -> a
$cminimum :: forall a. Ord a => ActionQuery a -> a
minimum :: forall a. Ord a => ActionQuery a -> a
$csum :: forall a. Num a => ActionQuery a -> a
sum :: forall a. Num a => ActionQuery a -> a
$cproduct :: forall a. Num a => ActionQuery a -> a
product :: forall a. Num a => ActionQuery a -> a
Foldable, Functor ActionQuery
Foldable ActionQuery
Functor ActionQuery
-> Foldable ActionQuery
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> ActionQuery a -> f (ActionQuery b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    ActionQuery (f a) -> f (ActionQuery a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> ActionQuery a -> m (ActionQuery b))
-> (forall (m :: * -> *) a.
    Monad m =>
    ActionQuery (m a) -> m (ActionQuery a))
-> Traversable ActionQuery
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a.
Monad m =>
ActionQuery (m a) -> m (ActionQuery a)
forall (f :: * -> *) a.
Applicative f =>
ActionQuery (f a) -> f (ActionQuery a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> ActionQuery a -> m (ActionQuery b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> ActionQuery a -> f (ActionQuery b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> ActionQuery a -> f (ActionQuery b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> ActionQuery a -> f (ActionQuery b)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
ActionQuery (f a) -> f (ActionQuery a)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
ActionQuery (f a) -> f (ActionQuery a)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> ActionQuery a -> m (ActionQuery b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> ActionQuery a -> m (ActionQuery b)
$csequence :: forall (m :: * -> *) a.
Monad m =>
ActionQuery (m a) -> m (ActionQuery a)
sequence :: forall (m :: * -> *) a.
Monad m =>
ActionQuery (m a) -> m (ActionQuery a)
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