{-# LANGUAGE TemplateHaskell #-}

module Hasura.GraphQL.Context
  ( RoleContext (..),
    GQLContext (..),
    ParserFn,
  )
where

import Data.Aeson qualified as J
import Data.Aeson.TH
import Hasura.Base.Error
import Hasura.GraphQL.Namespace
import Hasura.GraphQL.Parser
import Hasura.Prelude
import Hasura.RQL.IR qualified as IR
import Language.GraphQL.Draft.Syntax qualified as G

-- | For storing both a normal GQLContext and one for the backend variant.
-- Currently, this is to enable the backend variant to have certain insert/update/delete
-- permissions which the frontend variant does not.
data RoleContext a = RoleContext
  { -- | The default context for normal sessions
    RoleContext a -> a
_rctxDefault :: !a,
    -- | The context for sessions with backend privilege.
    RoleContext a -> Maybe a
_rctxBackend :: !(Maybe a)
  }
  deriving (Int -> RoleContext a -> ShowS
[RoleContext a] -> ShowS
RoleContext a -> String
(Int -> RoleContext a -> ShowS)
-> (RoleContext a -> String)
-> ([RoleContext a] -> ShowS)
-> Show (RoleContext a)
forall a. Show a => Int -> RoleContext a -> ShowS
forall a. Show a => [RoleContext a] -> ShowS
forall a. Show a => RoleContext a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [RoleContext a] -> ShowS
$cshowList :: forall a. Show a => [RoleContext a] -> ShowS
show :: RoleContext a -> String
$cshow :: forall a. Show a => RoleContext a -> String
showsPrec :: Int -> RoleContext a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> RoleContext a -> ShowS
Show, RoleContext a -> RoleContext a -> Bool
(RoleContext a -> RoleContext a -> Bool)
-> (RoleContext a -> RoleContext a -> Bool) -> Eq (RoleContext a)
forall a. Eq a => RoleContext a -> RoleContext a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: RoleContext a -> RoleContext a -> Bool
$c/= :: forall a. Eq a => RoleContext a -> RoleContext a -> Bool
== :: RoleContext a -> RoleContext a -> Bool
$c== :: forall a. Eq a => RoleContext a -> RoleContext a -> Bool
Eq, a -> RoleContext b -> RoleContext a
(a -> b) -> RoleContext a -> RoleContext b
(forall a b. (a -> b) -> RoleContext a -> RoleContext b)
-> (forall a b. a -> RoleContext b -> RoleContext a)
-> Functor RoleContext
forall a b. a -> RoleContext b -> RoleContext a
forall a b. (a -> b) -> RoleContext a -> RoleContext b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> RoleContext b -> RoleContext a
$c<$ :: forall a b. a -> RoleContext b -> RoleContext a
fmap :: (a -> b) -> RoleContext a -> RoleContext b
$cfmap :: forall a b. (a -> b) -> RoleContext a -> RoleContext b
Functor, RoleContext a -> Bool
(a -> m) -> RoleContext a -> m
(a -> b -> b) -> b -> RoleContext a -> b
(forall m. Monoid m => RoleContext m -> m)
-> (forall m a. Monoid m => (a -> m) -> RoleContext a -> m)
-> (forall m a. Monoid m => (a -> m) -> RoleContext a -> m)
-> (forall a b. (a -> b -> b) -> b -> RoleContext a -> b)
-> (forall a b. (a -> b -> b) -> b -> RoleContext a -> b)
-> (forall b a. (b -> a -> b) -> b -> RoleContext a -> b)
-> (forall b a. (b -> a -> b) -> b -> RoleContext a -> b)
-> (forall a. (a -> a -> a) -> RoleContext a -> a)
-> (forall a. (a -> a -> a) -> RoleContext a -> a)
-> (forall a. RoleContext a -> [a])
-> (forall a. RoleContext a -> Bool)
-> (forall a. RoleContext a -> Int)
-> (forall a. Eq a => a -> RoleContext a -> Bool)
-> (forall a. Ord a => RoleContext a -> a)
-> (forall a. Ord a => RoleContext a -> a)
-> (forall a. Num a => RoleContext a -> a)
-> (forall a. Num a => RoleContext a -> a)
-> Foldable RoleContext
forall a. Eq a => a -> RoleContext a -> Bool
forall a. Num a => RoleContext a -> a
forall a. Ord a => RoleContext a -> a
forall m. Monoid m => RoleContext m -> m
forall a. RoleContext a -> Bool
forall a. RoleContext a -> Int
forall a. RoleContext a -> [a]
forall a. (a -> a -> a) -> RoleContext a -> a
forall m a. Monoid m => (a -> m) -> RoleContext a -> m
forall b a. (b -> a -> b) -> b -> RoleContext a -> b
forall a b. (a -> b -> b) -> b -> RoleContext 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
product :: RoleContext a -> a
$cproduct :: forall a. Num a => RoleContext a -> a
sum :: RoleContext a -> a
$csum :: forall a. Num a => RoleContext a -> a
minimum :: RoleContext a -> a
$cminimum :: forall a. Ord a => RoleContext a -> a
maximum :: RoleContext a -> a
$cmaximum :: forall a. Ord a => RoleContext a -> a
elem :: a -> RoleContext a -> Bool
$celem :: forall a. Eq a => a -> RoleContext a -> Bool
length :: RoleContext a -> Int
$clength :: forall a. RoleContext a -> Int
null :: RoleContext a -> Bool
$cnull :: forall a. RoleContext a -> Bool
toList :: RoleContext a -> [a]
$ctoList :: forall a. RoleContext a -> [a]
foldl1 :: (a -> a -> a) -> RoleContext a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> RoleContext a -> a
foldr1 :: (a -> a -> a) -> RoleContext a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> RoleContext a -> a
foldl' :: (b -> a -> b) -> b -> RoleContext a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> RoleContext a -> b
foldl :: (b -> a -> b) -> b -> RoleContext a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> RoleContext a -> b
foldr' :: (a -> b -> b) -> b -> RoleContext a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> RoleContext a -> b
foldr :: (a -> b -> b) -> b -> RoleContext a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> RoleContext a -> b
foldMap' :: (a -> m) -> RoleContext a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> RoleContext a -> m
foldMap :: (a -> m) -> RoleContext a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> RoleContext a -> m
fold :: RoleContext m -> m
$cfold :: forall m. Monoid m => RoleContext m -> m
Foldable, Functor RoleContext
Foldable RoleContext
Functor RoleContext
-> Foldable RoleContext
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> RoleContext a -> f (RoleContext b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    RoleContext (f a) -> f (RoleContext a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> RoleContext a -> m (RoleContext b))
-> (forall (m :: * -> *) a.
    Monad m =>
    RoleContext (m a) -> m (RoleContext a))
-> Traversable RoleContext
(a -> f b) -> RoleContext a -> f (RoleContext 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 =>
RoleContext (m a) -> m (RoleContext a)
forall (f :: * -> *) a.
Applicative f =>
RoleContext (f a) -> f (RoleContext a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> RoleContext a -> m (RoleContext b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> RoleContext a -> f (RoleContext b)
sequence :: RoleContext (m a) -> m (RoleContext a)
$csequence :: forall (m :: * -> *) a.
Monad m =>
RoleContext (m a) -> m (RoleContext a)
mapM :: (a -> m b) -> RoleContext a -> m (RoleContext b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> RoleContext a -> m (RoleContext b)
sequenceA :: RoleContext (f a) -> f (RoleContext a)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
RoleContext (f a) -> f (RoleContext a)
traverse :: (a -> f b) -> RoleContext a -> f (RoleContext b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> RoleContext a -> f (RoleContext b)
$cp2Traversable :: Foldable RoleContext
$cp1Traversable :: Functor RoleContext
Traversable)

$(deriveToJSON hasuraJSON ''RoleContext)

data GQLContext = GQLContext
  { GQLContext
-> ParserFn (RootFieldMap (QueryRootField UnpreparedValue))
gqlQueryParser :: ParserFn (RootFieldMap (IR.QueryRootField IR.UnpreparedValue)),
    GQLContext
-> Maybe
     (ParserFn (RootFieldMap (MutationRootField UnpreparedValue)))
gqlMutationParser :: Maybe (ParserFn (RootFieldMap (IR.MutationRootField IR.UnpreparedValue))),
    GQLContext
-> Maybe (ParserFn (RootFieldMap (QueryRootField UnpreparedValue)))
gqlSubscriptionParser :: Maybe (ParserFn (RootFieldMap (IR.QueryRootField IR.UnpreparedValue)))
  }

instance J.ToJSON GQLContext where
  toJSON :: GQLContext -> Value
toJSON GQLContext {} = Text -> Value
J.String Text
"The GraphQL schema parsers"

type ParserFn a =
  G.SelectionSet G.NoFragments Variable ->
  Either QErr a