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

import Data.Aeson qualified as J
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
    forall a. RoleContext a -> a
_rctxDefault :: !a,
    -- | The context for sessions with backend privilege.
    forall a. 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
$cshowsPrec :: forall a. Show a => Int -> RoleContext a -> ShowS
showsPrec :: Int -> RoleContext a -> ShowS
$cshow :: forall a. Show a => RoleContext a -> String
show :: RoleContext a -> String
$cshowList :: forall a. Show a => [RoleContext a] -> ShowS
showList :: [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
$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
/= :: RoleContext a -> RoleContext a -> Bool
Eq, (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
$cfmap :: forall a b. (a -> b) -> RoleContext a -> RoleContext b
fmap :: forall a b. (a -> b) -> RoleContext a -> RoleContext b
$c<$ :: forall a b. a -> RoleContext b -> RoleContext a
<$ :: forall a b. a -> RoleContext b -> RoleContext a
Functor, (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
$cfold :: forall m. Monoid m => RoleContext m -> m
fold :: forall m. Monoid m => RoleContext m -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> RoleContext a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> RoleContext a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> RoleContext a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> RoleContext a -> m
$cfoldr :: forall a b. (a -> b -> b) -> b -> RoleContext a -> b
foldr :: forall a b. (a -> b -> b) -> b -> RoleContext a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> RoleContext a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> RoleContext a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> RoleContext a -> b
foldl :: forall b a. (b -> a -> b) -> b -> RoleContext a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> RoleContext a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> RoleContext a -> b
$cfoldr1 :: forall a. (a -> a -> a) -> RoleContext a -> a
foldr1 :: forall a. (a -> a -> a) -> RoleContext a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> RoleContext a -> a
foldl1 :: forall a. (a -> a -> a) -> RoleContext a -> a
$ctoList :: forall a. RoleContext a -> [a]
toList :: forall a. RoleContext a -> [a]
$cnull :: forall a. RoleContext a -> Bool
null :: forall a. RoleContext a -> Bool
$clength :: forall a. RoleContext a -> Int
length :: forall a. RoleContext a -> Int
$celem :: forall a. Eq a => a -> RoleContext a -> Bool
elem :: forall a. Eq a => a -> RoleContext a -> Bool
$cmaximum :: forall a. Ord a => RoleContext a -> a
maximum :: forall a. Ord a => RoleContext a -> a
$cminimum :: forall a. Ord a => RoleContext a -> a
minimum :: forall a. Ord a => RoleContext a -> a
$csum :: forall a. Num a => RoleContext a -> a
sum :: forall a. Num a => RoleContext a -> a
$cproduct :: forall a. Num a => RoleContext a -> a
product :: forall a. Num a => RoleContext a -> a
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
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)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> RoleContext a -> f (RoleContext b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> RoleContext a -> f (RoleContext b)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
RoleContext (f a) -> f (RoleContext a)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
RoleContext (f a) -> f (RoleContext a)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> RoleContext a -> m (RoleContext b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> RoleContext a -> m (RoleContext b)
$csequence :: forall (m :: * -> *) a.
Monad m =>
RoleContext (m a) -> m (RoleContext a)
sequence :: forall (m :: * -> *) a.
Monad m =>
RoleContext (m a) -> m (RoleContext a)
Traversable, (forall x. RoleContext a -> Rep (RoleContext a) x)
-> (forall x. Rep (RoleContext a) x -> RoleContext a)
-> Generic (RoleContext a)
forall x. Rep (RoleContext a) x -> RoleContext a
forall x. RoleContext a -> Rep (RoleContext a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (RoleContext a) x -> RoleContext a
forall a x. RoleContext a -> Rep (RoleContext a) x
$cfrom :: forall a x. RoleContext a -> Rep (RoleContext a) x
from :: forall x. RoleContext a -> Rep (RoleContext a) x
$cto :: forall a x. Rep (RoleContext a) x -> RoleContext a
to :: forall x. Rep (RoleContext a) x -> RoleContext a
Generic)

instance (J.ToJSON a) => J.ToJSON (RoleContext a) where
  toJSON :: RoleContext a -> Value
toJSON = Options -> RoleContext a -> Value
forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
J.genericToJSON Options
hasuraJSON
  toEncoding :: RoleContext a -> Encoding
toEncoding = Options -> RoleContext a -> Encoding
forall a.
(Generic a, GToJSON' Encoding Zero (Rep a)) =>
Options -> a -> Encoding
J.genericToEncoding Options
hasuraJSON

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