-- | Postgres-specific schema combinators. Those should be moved to
-- the corresponding instance of `BackendSchema`, when actions are
-- generalized.
module Hasura.GraphQL.Schema.Postgres
  ( buildActionQueryFields,
    buildActionSubscriptionFields,
    buildActionMutationFields,
  )
where

import Hasura.GraphQL.Schema.Action
import Hasura.GraphQL.Schema.Backend (MonadBuildSchema)
import Hasura.GraphQL.Schema.Parser (FieldParser)
import Hasura.Prelude
import Hasura.RQL.IR
import Hasura.RQL.Types.Action
import Hasura.RQL.Types.CustomTypes
import Hasura.SQL.Backend

buildActionQueryFields ::
  MonadBuildSchema ('Postgres 'Vanilla) r m n =>
  AnnotatedCustomTypes ->
  ActionInfo ->
  m [FieldParser n (QueryRootField UnpreparedValue)]
buildActionQueryFields :: AnnotatedCustomTypes
-> ActionInfo -> m [FieldParser n (QueryRootField UnpreparedValue)]
buildActionQueryFields AnnotatedCustomTypes
customTypes ActionInfo
actionInfo =
  Maybe (FieldParser n (QueryRootField UnpreparedValue))
-> [FieldParser n (QueryRootField UnpreparedValue)]
forall a. Maybe a -> [a]
maybeToList (Maybe (FieldParser n (QueryRootField UnpreparedValue))
 -> [FieldParser n (QueryRootField UnpreparedValue)])
-> m (Maybe (FieldParser n (QueryRootField UnpreparedValue)))
-> m [FieldParser n (QueryRootField UnpreparedValue)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> case ActionDefinition
  (GType, AnnotatedInputType) (EnvRecord ResolvedWebhook)
-> ActionType
forall arg webhook. ActionDefinition arg webhook -> ActionType
_adType (ActionInfo
-> ActionDefinition
     (GType, AnnotatedInputType) (EnvRecord ResolvedWebhook)
_aiDefinition ActionInfo
actionInfo) of
    ActionType
ActionQuery ->
      (FieldParser
   MetadataObjId
   n
   (AnnActionExecution (RemoteRelationshipField UnpreparedValue))
 -> FieldParser n (QueryRootField UnpreparedValue))
-> Maybe
     (FieldParser
        MetadataObjId
        n
        (AnnActionExecution (RemoteRelationshipField UnpreparedValue)))
-> Maybe (FieldParser n (QueryRootField UnpreparedValue))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((AnnActionExecution (RemoteRelationshipField UnpreparedValue)
 -> QueryRootField UnpreparedValue)
-> FieldParser
     MetadataObjId
     n
     (AnnActionExecution (RemoteRelationshipField UnpreparedValue))
-> FieldParser n (QueryRootField UnpreparedValue)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (ActionQuery (RemoteRelationshipField UnpreparedValue)
-> QueryRootField UnpreparedValue
forall action (db :: BackendType -> *) remote raw.
action -> RootField db remote action raw
RFAction (ActionQuery (RemoteRelationshipField UnpreparedValue)
 -> QueryRootField UnpreparedValue)
-> (AnnActionExecution (RemoteRelationshipField UnpreparedValue)
    -> ActionQuery (RemoteRelationshipField UnpreparedValue))
-> AnnActionExecution (RemoteRelationshipField UnpreparedValue)
-> QueryRootField UnpreparedValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnnActionExecution (RemoteRelationshipField UnpreparedValue)
-> ActionQuery (RemoteRelationshipField UnpreparedValue)
forall r. AnnActionExecution r -> ActionQuery r
AQQuery)) (Maybe
   (FieldParser
      MetadataObjId
      n
      (AnnActionExecution (RemoteRelationshipField UnpreparedValue)))
 -> Maybe (FieldParser n (QueryRootField UnpreparedValue)))
-> m (Maybe
        (FieldParser
           MetadataObjId
           n
           (AnnActionExecution (RemoteRelationshipField UnpreparedValue))))
-> m (Maybe (FieldParser n (QueryRootField UnpreparedValue)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> AnnotatedCustomTypes
-> ActionInfo
-> m (Maybe
        (FieldParser
           MetadataObjId
           n
           (AnnActionExecution (RemoteRelationshipField UnpreparedValue))))
forall r (m :: * -> *) (n :: * -> *).
MonadBuildSchemaBase r m n =>
AnnotatedCustomTypes
-> ActionInfo
-> m (Maybe
        (FieldParser
           n (AnnActionExecution (RemoteRelationshipField UnpreparedValue))))
actionExecute AnnotatedCustomTypes
customTypes ActionInfo
actionInfo
    ActionMutation ActionMutationKind
ActionSynchronous -> Maybe (FieldParser n (QueryRootField UnpreparedValue))
-> m (Maybe (FieldParser n (QueryRootField UnpreparedValue)))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (FieldParser n (QueryRootField UnpreparedValue))
forall a. Maybe a
Nothing
    ActionMutation ActionMutationKind
ActionAsynchronous ->
      (FieldParser
   MetadataObjId
   n
   (AnnActionAsyncQuery
      ('Postgres 'Vanilla) (RemoteRelationshipField UnpreparedValue))
 -> FieldParser n (QueryRootField UnpreparedValue))
-> Maybe
     (FieldParser
        MetadataObjId
        n
        (AnnActionAsyncQuery
           ('Postgres 'Vanilla) (RemoteRelationshipField UnpreparedValue)))
-> Maybe (FieldParser n (QueryRootField UnpreparedValue))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((AnnActionAsyncQuery
   ('Postgres 'Vanilla) (RemoteRelationshipField UnpreparedValue)
 -> QueryRootField UnpreparedValue)
-> FieldParser
     MetadataObjId
     n
     (AnnActionAsyncQuery
        ('Postgres 'Vanilla) (RemoteRelationshipField UnpreparedValue))
-> FieldParser n (QueryRootField UnpreparedValue)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (ActionQuery (RemoteRelationshipField UnpreparedValue)
-> QueryRootField UnpreparedValue
forall action (db :: BackendType -> *) remote raw.
action -> RootField db remote action raw
RFAction (ActionQuery (RemoteRelationshipField UnpreparedValue)
 -> QueryRootField UnpreparedValue)
-> (AnnActionAsyncQuery
      ('Postgres 'Vanilla) (RemoteRelationshipField UnpreparedValue)
    -> ActionQuery (RemoteRelationshipField UnpreparedValue))
-> AnnActionAsyncQuery
     ('Postgres 'Vanilla) (RemoteRelationshipField UnpreparedValue)
-> QueryRootField UnpreparedValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnnActionAsyncQuery
  ('Postgres 'Vanilla) (RemoteRelationshipField UnpreparedValue)
-> ActionQuery (RemoteRelationshipField UnpreparedValue)
forall r.
AnnActionAsyncQuery ('Postgres 'Vanilla) r -> ActionQuery r
AQAsync)) (Maybe
   (FieldParser
      MetadataObjId
      n
      (AnnActionAsyncQuery
         ('Postgres 'Vanilla) (RemoteRelationshipField UnpreparedValue)))
 -> Maybe (FieldParser n (QueryRootField UnpreparedValue)))
-> m (Maybe
        (FieldParser
           MetadataObjId
           n
           (AnnActionAsyncQuery
              ('Postgres 'Vanilla) (RemoteRelationshipField UnpreparedValue))))
-> m (Maybe (FieldParser n (QueryRootField UnpreparedValue)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HashMap Name AnnotatedObjectType
-> ActionInfo
-> m (Maybe
        (FieldParser
           MetadataObjId
           n
           (AnnActionAsyncQuery
              ('Postgres 'Vanilla) (RemoteRelationshipField UnpreparedValue))))
forall r (m :: * -> *) (n :: * -> *).
MonadBuildSchema ('Postgres 'Vanilla) r m n =>
HashMap Name AnnotatedObjectType
-> ActionInfo
-> m (Maybe
        (FieldParser
           n
           (AnnActionAsyncQuery
              ('Postgres 'Vanilla) (RemoteRelationshipField UnpreparedValue))))
actionAsyncQuery (AnnotatedCustomTypes -> HashMap Name AnnotatedObjectType
_actObjectTypes AnnotatedCustomTypes
customTypes) ActionInfo
actionInfo

buildActionMutationFields ::
  MonadBuildSchema ('Postgres 'Vanilla) r m n =>
  AnnotatedCustomTypes ->
  ActionInfo ->
  m [FieldParser n (MutationRootField UnpreparedValue)]
buildActionMutationFields :: AnnotatedCustomTypes
-> ActionInfo
-> m [FieldParser n (MutationRootField UnpreparedValue)]
buildActionMutationFields AnnotatedCustomTypes
customTypes ActionInfo
actionInfo =
  Maybe (FieldParser n (MutationRootField UnpreparedValue))
-> [FieldParser n (MutationRootField UnpreparedValue)]
forall a. Maybe a -> [a]
maybeToList (Maybe (FieldParser n (MutationRootField UnpreparedValue))
 -> [FieldParser n (MutationRootField UnpreparedValue)])
-> m (Maybe (FieldParser n (MutationRootField UnpreparedValue)))
-> m [FieldParser n (MutationRootField UnpreparedValue)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> case ActionDefinition
  (GType, AnnotatedInputType) (EnvRecord ResolvedWebhook)
-> ActionType
forall arg webhook. ActionDefinition arg webhook -> ActionType
_adType (ActionInfo
-> ActionDefinition
     (GType, AnnotatedInputType) (EnvRecord ResolvedWebhook)
_aiDefinition ActionInfo
actionInfo) of
    ActionType
ActionQuery -> Maybe (FieldParser n (MutationRootField UnpreparedValue))
-> m (Maybe (FieldParser n (MutationRootField UnpreparedValue)))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (FieldParser n (MutationRootField UnpreparedValue))
forall a. Maybe a
Nothing
    ActionMutation ActionMutationKind
ActionSynchronous ->
      (FieldParser
   MetadataObjId
   n
   (AnnActionExecution (RemoteRelationshipField UnpreparedValue))
 -> FieldParser n (MutationRootField UnpreparedValue))
-> Maybe
     (FieldParser
        MetadataObjId
        n
        (AnnActionExecution (RemoteRelationshipField UnpreparedValue)))
-> Maybe (FieldParser n (MutationRootField UnpreparedValue))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((AnnActionExecution (RemoteRelationshipField UnpreparedValue)
 -> MutationRootField UnpreparedValue)
-> FieldParser
     MetadataObjId
     n
     (AnnActionExecution (RemoteRelationshipField UnpreparedValue))
-> FieldParser n (MutationRootField UnpreparedValue)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (ActionMutation (RemoteRelationshipField UnpreparedValue)
-> MutationRootField UnpreparedValue
forall action (db :: BackendType -> *) remote raw.
action -> RootField db remote action raw
RFAction (ActionMutation (RemoteRelationshipField UnpreparedValue)
 -> MutationRootField UnpreparedValue)
-> (AnnActionExecution (RemoteRelationshipField UnpreparedValue)
    -> ActionMutation (RemoteRelationshipField UnpreparedValue))
-> AnnActionExecution (RemoteRelationshipField UnpreparedValue)
-> MutationRootField UnpreparedValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnnActionExecution (RemoteRelationshipField UnpreparedValue)
-> ActionMutation (RemoteRelationshipField UnpreparedValue)
forall r. AnnActionExecution r -> ActionMutation r
AMSync)) (Maybe
   (FieldParser
      MetadataObjId
      n
      (AnnActionExecution (RemoteRelationshipField UnpreparedValue)))
 -> Maybe (FieldParser n (MutationRootField UnpreparedValue)))
-> m (Maybe
        (FieldParser
           MetadataObjId
           n
           (AnnActionExecution (RemoteRelationshipField UnpreparedValue))))
-> m (Maybe (FieldParser n (MutationRootField UnpreparedValue)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> AnnotatedCustomTypes
-> ActionInfo
-> m (Maybe
        (FieldParser
           MetadataObjId
           n
           (AnnActionExecution (RemoteRelationshipField UnpreparedValue))))
forall r (m :: * -> *) (n :: * -> *).
MonadBuildSchemaBase r m n =>
AnnotatedCustomTypes
-> ActionInfo
-> m (Maybe
        (FieldParser
           n (AnnActionExecution (RemoteRelationshipField UnpreparedValue))))
actionExecute AnnotatedCustomTypes
customTypes ActionInfo
actionInfo
    ActionMutation ActionMutationKind
ActionAsynchronous ->
      (FieldParser MetadataObjId n AnnActionMutationAsync
 -> FieldParser n (MutationRootField UnpreparedValue))
-> Maybe (FieldParser MetadataObjId n AnnActionMutationAsync)
-> Maybe (FieldParser n (MutationRootField UnpreparedValue))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((AnnActionMutationAsync -> MutationRootField UnpreparedValue)
-> FieldParser MetadataObjId n AnnActionMutationAsync
-> FieldParser n (MutationRootField UnpreparedValue)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (ActionMutation (RemoteRelationshipField UnpreparedValue)
-> MutationRootField UnpreparedValue
forall action (db :: BackendType -> *) remote raw.
action -> RootField db remote action raw
RFAction (ActionMutation (RemoteRelationshipField UnpreparedValue)
 -> MutationRootField UnpreparedValue)
-> (AnnActionMutationAsync
    -> ActionMutation (RemoteRelationshipField UnpreparedValue))
-> AnnActionMutationAsync
-> MutationRootField UnpreparedValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnnActionMutationAsync
-> ActionMutation (RemoteRelationshipField UnpreparedValue)
forall r. AnnActionMutationAsync -> ActionMutation r
AMAsync)) (Maybe (FieldParser MetadataObjId n AnnActionMutationAsync)
 -> Maybe (FieldParser n (MutationRootField UnpreparedValue)))
-> m (Maybe (FieldParser MetadataObjId n AnnActionMutationAsync))
-> m (Maybe (FieldParser n (MutationRootField UnpreparedValue)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HashMap Name AnnotatedInputType
-> ActionInfo
-> m (Maybe (FieldParser MetadataObjId n AnnActionMutationAsync))
forall r (m :: * -> *) (n :: * -> *).
MonadBuildSchemaBase r m n =>
HashMap Name AnnotatedInputType
-> ActionInfo -> m (Maybe (FieldParser n AnnActionMutationAsync))
actionAsyncMutation (AnnotatedCustomTypes -> HashMap Name AnnotatedInputType
_actInputTypes AnnotatedCustomTypes
customTypes) ActionInfo
actionInfo

buildActionSubscriptionFields ::
  MonadBuildSchema ('Postgres 'Vanilla) r m n =>
  AnnotatedCustomTypes ->
  ActionInfo ->
  m [FieldParser n (QueryRootField UnpreparedValue)]
buildActionSubscriptionFields :: AnnotatedCustomTypes
-> ActionInfo -> m [FieldParser n (QueryRootField UnpreparedValue)]
buildActionSubscriptionFields AnnotatedCustomTypes
customTypes ActionInfo
actionInfo =
  Maybe (FieldParser n (QueryRootField UnpreparedValue))
-> [FieldParser n (QueryRootField UnpreparedValue)]
forall a. Maybe a -> [a]
maybeToList (Maybe (FieldParser n (QueryRootField UnpreparedValue))
 -> [FieldParser n (QueryRootField UnpreparedValue)])
-> m (Maybe (FieldParser n (QueryRootField UnpreparedValue)))
-> m [FieldParser n (QueryRootField UnpreparedValue)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> case ActionDefinition
  (GType, AnnotatedInputType) (EnvRecord ResolvedWebhook)
-> ActionType
forall arg webhook. ActionDefinition arg webhook -> ActionType
_adType (ActionInfo
-> ActionDefinition
     (GType, AnnotatedInputType) (EnvRecord ResolvedWebhook)
_aiDefinition ActionInfo
actionInfo) of
    ActionType
ActionQuery -> Maybe (FieldParser n (QueryRootField UnpreparedValue))
-> m (Maybe (FieldParser n (QueryRootField UnpreparedValue)))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (FieldParser n (QueryRootField UnpreparedValue))
forall a. Maybe a
Nothing
    ActionMutation ActionMutationKind
ActionSynchronous -> Maybe (FieldParser n (QueryRootField UnpreparedValue))
-> m (Maybe (FieldParser n (QueryRootField UnpreparedValue)))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (FieldParser n (QueryRootField UnpreparedValue))
forall a. Maybe a
Nothing
    ActionMutation ActionMutationKind
ActionAsynchronous ->
      (FieldParser
   MetadataObjId
   n
   (AnnActionAsyncQuery
      ('Postgres 'Vanilla) (RemoteRelationshipField UnpreparedValue))
 -> FieldParser n (QueryRootField UnpreparedValue))
-> Maybe
     (FieldParser
        MetadataObjId
        n
        (AnnActionAsyncQuery
           ('Postgres 'Vanilla) (RemoteRelationshipField UnpreparedValue)))
-> Maybe (FieldParser n (QueryRootField UnpreparedValue))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((AnnActionAsyncQuery
   ('Postgres 'Vanilla) (RemoteRelationshipField UnpreparedValue)
 -> QueryRootField UnpreparedValue)
-> FieldParser
     MetadataObjId
     n
     (AnnActionAsyncQuery
        ('Postgres 'Vanilla) (RemoteRelationshipField UnpreparedValue))
-> FieldParser n (QueryRootField UnpreparedValue)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (ActionQuery (RemoteRelationshipField UnpreparedValue)
-> QueryRootField UnpreparedValue
forall action (db :: BackendType -> *) remote raw.
action -> RootField db remote action raw
RFAction (ActionQuery (RemoteRelationshipField UnpreparedValue)
 -> QueryRootField UnpreparedValue)
-> (AnnActionAsyncQuery
      ('Postgres 'Vanilla) (RemoteRelationshipField UnpreparedValue)
    -> ActionQuery (RemoteRelationshipField UnpreparedValue))
-> AnnActionAsyncQuery
     ('Postgres 'Vanilla) (RemoteRelationshipField UnpreparedValue)
-> QueryRootField UnpreparedValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnnActionAsyncQuery
  ('Postgres 'Vanilla) (RemoteRelationshipField UnpreparedValue)
-> ActionQuery (RemoteRelationshipField UnpreparedValue)
forall r.
AnnActionAsyncQuery ('Postgres 'Vanilla) r -> ActionQuery r
AQAsync)) (Maybe
   (FieldParser
      MetadataObjId
      n
      (AnnActionAsyncQuery
         ('Postgres 'Vanilla) (RemoteRelationshipField UnpreparedValue)))
 -> Maybe (FieldParser n (QueryRootField UnpreparedValue)))
-> m (Maybe
        (FieldParser
           MetadataObjId
           n
           (AnnActionAsyncQuery
              ('Postgres 'Vanilla) (RemoteRelationshipField UnpreparedValue))))
-> m (Maybe (FieldParser n (QueryRootField UnpreparedValue)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HashMap Name AnnotatedObjectType
-> ActionInfo
-> m (Maybe
        (FieldParser
           MetadataObjId
           n
           (AnnActionAsyncQuery
              ('Postgres 'Vanilla) (RemoteRelationshipField UnpreparedValue))))
forall r (m :: * -> *) (n :: * -> *).
MonadBuildSchema ('Postgres 'Vanilla) r m n =>
HashMap Name AnnotatedObjectType
-> ActionInfo
-> m (Maybe
        (FieldParser
           n
           (AnnActionAsyncQuery
              ('Postgres 'Vanilla) (RemoteRelationshipField UnpreparedValue))))
actionAsyncQuery (AnnotatedCustomTypes -> HashMap Name AnnotatedObjectType
_actObjectTypes AnnotatedCustomTypes
customTypes) ActionInfo
actionInfo