{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE NoImplicitPrelude #-}

module Data.Morpheus.Server.Deriving.Channels
  ( channelResolver,
    ChannelsConstraint,
  )
where

import Control.Monad.Except (throwError)
import qualified Data.HashMap.Lazy as HM
import Data.Morpheus.App.Internal.Resolving
  ( Channel,
    Resolver,
    ResolverState,
    SubscriptionField (..),
  )
import Data.Morpheus.Internal.Utils
  ( selectBy,
  )
import Data.Morpheus.Server.Deriving.Decode
  ( DecodeConstraint,
    decodeArguments,
  )
import Data.Morpheus.Server.Deriving.Utils
  ( ConsRep (..),
    DataType (..),
    FieldRep (..),
    TypeConstraint (..),
    TypeRep (..),
    toValue,
  )
import Data.Morpheus.Server.Types.GQLType (GQLType)
import Data.Morpheus.Types.Internal.AST
  ( FieldName,
    OUT,
    SUBSCRIPTION,
    Selection (..),
    SelectionContent (..),
    VALID,
    internal,
  )
import GHC.Generics
import Relude

newtype DerivedChannel e = DerivedChannel
  { DerivedChannel e -> Channel e
_unpackChannel :: Channel e
  }

type ChannelRes (e :: Type) = Selection VALID -> ResolverState (DerivedChannel e)

type ChannelsConstraint e m (subs :: (Type -> Type) -> Type) =
  ExploreConstraint e (subs (Resolver SUBSCRIPTION e m))

channelResolver ::
  forall e m subs.
  ChannelsConstraint e m subs =>
  subs (Resolver SUBSCRIPTION e m) ->
  Selection VALID ->
  ResolverState (Channel e)
channelResolver :: subs (Resolver SUBSCRIPTION e m)
-> Selection VALID -> ResolverState (Channel e)
channelResolver subs (Resolver SUBSCRIPTION e m)
value = (DerivedChannel e -> Channel e)
-> ResolverStateT () Identity (DerivedChannel e)
-> ResolverState (Channel e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap DerivedChannel e -> Channel e
forall e. DerivedChannel e -> Channel e
_unpackChannel (ResolverStateT () Identity (DerivedChannel e)
 -> ResolverState (Channel e))
-> (Selection VALID
    -> ResolverStateT () Identity (DerivedChannel e))
-> Selection VALID
-> ResolverState (Channel e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Selection VALID -> ResolverStateT () Identity (DerivedChannel e)
channelSelector
  where
    channelSelector ::
      Selection VALID ->
      ResolverState (DerivedChannel e)
    channelSelector :: Selection VALID -> ResolverStateT () Identity (DerivedChannel e)
channelSelector = HashMap
  FieldName
  (Selection VALID -> ResolverStateT () Identity (DerivedChannel e))
-> Selection VALID -> ResolverStateT () Identity (DerivedChannel e)
forall e. HashMap FieldName (ChannelRes e) -> ChannelRes e
selectBySelection (subs (Resolver SUBSCRIPTION e m)
-> HashMap
     FieldName
     (Selection VALID -> ResolverStateT () Identity (DerivedChannel e))
forall e a.
ExploreConstraint e a =>
a -> HashMap FieldName (ChannelRes e)
exploreChannels subs (Resolver SUBSCRIPTION e m)
value)

selectBySelection ::
  HashMap FieldName (ChannelRes e) ->
  Selection VALID ->
  ResolverState (DerivedChannel e)
selectBySelection :: HashMap FieldName (ChannelRes e) -> ChannelRes e
selectBySelection HashMap FieldName (ChannelRes e)
channels = Selection VALID -> ResolverState (Selection VALID)
withSubscriptionSelection (Selection VALID -> ResolverState (Selection VALID))
-> ChannelRes e -> ChannelRes e
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> HashMap FieldName (ChannelRes e) -> ChannelRes e
forall e. HashMap FieldName (ChannelRes e) -> ChannelRes e
selectSubscription HashMap FieldName (ChannelRes e)
channels

selectSubscription ::
  HashMap FieldName (ChannelRes e) ->
  Selection VALID ->
  ResolverState (DerivedChannel e)
selectSubscription :: HashMap FieldName (ChannelRes e) -> ChannelRes e
selectSubscription HashMap FieldName (ChannelRes e)
channels sel :: Selection VALID
sel@Selection {FieldName
selectionName :: forall (s :: Stage). Selection s -> FieldName
selectionName :: FieldName
selectionName} =
  GQLError
-> FieldName
-> HashMap FieldName (ChannelRes e)
-> ResolverStateT () Identity (ChannelRes e)
forall e (m :: * -> *) k (c :: * -> *) a.
(MonadError e m, IsMap k c, Monad m) =>
e -> k -> c a -> m a
selectBy
    (GQLError -> GQLError
internal GQLError
"invalid subscription: no channel is selected.")
    FieldName
selectionName
    HashMap FieldName (ChannelRes e)
channels
    ResolverStateT () Identity (ChannelRes e)
-> (ChannelRes e -> ResolverStateT () Identity (DerivedChannel e))
-> ResolverStateT () Identity (DerivedChannel e)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Selection VALID
sel Selection VALID
-> ChannelRes e -> ResolverStateT () Identity (DerivedChannel e)
forall a b. a -> (a -> b) -> b
&)

withSubscriptionSelection :: Selection VALID -> ResolverState (Selection VALID)
withSubscriptionSelection :: Selection VALID -> ResolverState (Selection VALID)
withSubscriptionSelection Selection {selectionContent :: forall (s :: Stage). Selection s -> SelectionContent s
selectionContent = SelectionSet SelectionSet VALID
selSet} =
  case MergeMap 'False FieldName (Selection VALID) -> [Selection VALID]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList MergeMap 'False FieldName (Selection VALID)
SelectionSet VALID
selSet of
    [Selection VALID
sel] -> Selection VALID -> ResolverState (Selection VALID)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Selection VALID
sel
    [Selection VALID]
_ -> GQLError -> ResolverState (Selection VALID)
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (GQLError -> GQLError
internal GQLError
"invalid subscription: there can be only one top level selection")
withSubscriptionSelection Selection VALID
_ = GQLError -> ResolverState (Selection VALID)
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (GQLError -> GQLError
internal GQLError
"invalid subscription: expected selectionSet")

class GetChannel e a | a -> e where
  getChannel :: a -> ChannelRes e

instance GetChannel e (SubscriptionField (Resolver SUBSCRIPTION e m a)) where
  getChannel :: SubscriptionField (Resolver SUBSCRIPTION e m a) -> ChannelRes e
getChannel SubscriptionField (Resolver SUBSCRIPTION e m a)
x = ResolverStateT () Identity (DerivedChannel e) -> ChannelRes e
forall a b. a -> b -> a
const (ResolverStateT () Identity (DerivedChannel e) -> ChannelRes e)
-> ResolverStateT () Identity (DerivedChannel e) -> ChannelRes e
forall a b. (a -> b) -> a -> b
$ DerivedChannel e -> ResolverStateT () Identity (DerivedChannel e)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DerivedChannel e -> ResolverStateT () Identity (DerivedChannel e))
-> DerivedChannel e
-> ResolverStateT () Identity (DerivedChannel e)
forall a b. (a -> b) -> a -> b
$ Channel e -> DerivedChannel e
forall e. Channel e -> DerivedChannel e
DerivedChannel (Channel e -> DerivedChannel e) -> Channel e -> DerivedChannel e
forall a b. (a -> b) -> a -> b
$ SubscriptionField (Resolver SUBSCRIPTION e m a)
-> forall e (m :: * -> *) v.
   (Resolver SUBSCRIPTION e m a ~ Resolver SUBSCRIPTION e m v) =>
   Channel e
forall a.
SubscriptionField a
-> forall e (m :: * -> *) v.
   (a ~ Resolver SUBSCRIPTION e m v) =>
   Channel e
channel SubscriptionField (Resolver SUBSCRIPTION e m a)
x

instance
  DecodeConstraint arg =>
  GetChannel e (arg -> SubscriptionField (Resolver SUBSCRIPTION e m a))
  where
  getChannel :: (arg -> SubscriptionField (Resolver SUBSCRIPTION e m a))
-> ChannelRes e
getChannel arg -> SubscriptionField (Resolver SUBSCRIPTION e m a)
f sel :: Selection VALID
sel@Selection {Arguments VALID
selectionArguments :: forall (s :: Stage). Selection s -> Arguments s
selectionArguments :: Arguments VALID
selectionArguments} =
    Arguments VALID -> ResolverState arg
forall a. DecodeConstraint a => Arguments VALID -> ResolverState a
decodeArguments Arguments VALID
selectionArguments
      ResolverState arg
-> (arg -> ResolverStateT () Identity (DerivedChannel e))
-> ResolverStateT () Identity (DerivedChannel e)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (SubscriptionField (Resolver SUBSCRIPTION e m a) -> ChannelRes e
forall e a. GetChannel e a => a -> ChannelRes e
`getChannel` Selection VALID
sel)
        (SubscriptionField (Resolver SUBSCRIPTION e m a)
 -> ResolverStateT () Identity (DerivedChannel e))
-> (arg -> SubscriptionField (Resolver SUBSCRIPTION e m a))
-> arg
-> ResolverStateT () Identity (DerivedChannel e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. arg -> SubscriptionField (Resolver SUBSCRIPTION e m a)
f

------------------------------------------------------

type ExploreConstraint e a =
  ( GQLType a,
    Generic a,
    TypeRep (GetChannel e) (ChannelRes e) (Rep a)
  )

exploreChannels :: forall e a. ExploreConstraint e a => a -> HashMap FieldName (ChannelRes e)
exploreChannels :: a -> HashMap FieldName (ChannelRes e)
exploreChannels =
  [(FieldName, ChannelRes e)] -> HashMap FieldName (ChannelRes e)
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HM.fromList
    ([(FieldName, ChannelRes e)] -> HashMap FieldName (ChannelRes e))
-> (a -> [(FieldName, ChannelRes e)])
-> a
-> HashMap FieldName (ChannelRes e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DataType (ChannelRes e) -> [(FieldName, ChannelRes e)]
forall e. DataType (ChannelRes e) -> [(FieldName, ChannelRes e)]
convertNode
    (DataType (ChannelRes e) -> [(FieldName, ChannelRes e)])
-> (a -> DataType (ChannelRes e))
-> a
-> [(FieldName, ChannelRes e)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TypeConstraint (GetChannel e) (ChannelRes e) Identity
-> Proxy OUT -> a -> DataType (ChannelRes e)
forall (proxy :: TypeCategory -> *) (kind :: TypeCategory)
       (constraint :: * -> Constraint) value a.
(GQLType a, CategoryValue kind, Generic a,
 TypeRep constraint value (Rep a)) =>
TypeConstraint constraint value Identity
-> proxy kind -> a -> DataType value
toValue
      ( (forall a. GetChannel e a => Identity a -> ChannelRes e)
-> TypeConstraint (GetChannel e) (ChannelRes e) Identity
forall (c :: * -> Constraint) v (f :: * -> *).
(forall a. c a => f a -> v) -> TypeConstraint c v f
TypeConstraint (a -> ChannelRes e
forall e a. GetChannel e a => a -> ChannelRes e
getChannel (a -> ChannelRes e)
-> (Identity a -> a) -> Identity a -> ChannelRes e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Identity a -> a
forall a. Identity a -> a
runIdentity) :: TypeConstraint (GetChannel e) (ChannelRes e) Identity
      )
      (Proxy OUT
forall k (t :: k). Proxy t
Proxy @OUT)

convertNode :: DataType (ChannelRes e) -> [(FieldName, ChannelRes e)]
convertNode :: DataType (ChannelRes e) -> [(FieldName, ChannelRes e)]
convertNode DataType {tyCons :: forall v. DataType v -> ConsRep v
tyCons = ConsRep {[FieldRep (ChannelRes e)]
consFields :: forall v. ConsRep v -> [FieldRep v]
consFields :: [FieldRep (ChannelRes e)]
consFields}} = (FieldRep (ChannelRes e) -> (FieldName, ChannelRes e))
-> [FieldRep (ChannelRes e)] -> [(FieldName, ChannelRes e)]
forall a b. (a -> b) -> [a] -> [b]
map FieldRep (ChannelRes e) -> (FieldName, ChannelRes e)
forall b. FieldRep b -> (FieldName, b)
toChannels [FieldRep (ChannelRes e)]
consFields
  where
    toChannels :: FieldRep b -> (FieldName, b)
toChannels FieldRep {FieldName
fieldSelector :: forall a. FieldRep a -> FieldName
fieldSelector :: FieldName
fieldSelector, b
fieldValue :: forall a. FieldRep a -> a
fieldValue :: b
fieldValue} = (FieldName
fieldSelector, b
fieldValue)