summaryrefslogtreecommitdiff
path: root/src/Language/GraphQL/Execute/Internal.hs
blob: 792a7580d8e40bb201706e3c9473f8c2881099be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
{- This Source Code Form is subject to the terms of the Mozilla Public License,
   v. 2.0. If a copy of the MPL was not distributed with this file, You can
   obtain one at https://mozilla.org/MPL/2.0/. -}

{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE ExplicitForAll #-}
{-# LANGUAGE NamedFieldPuns #-}

module Language.GraphQL.Execute.Internal
    ( addError
    , singleError
    ) where

import Control.Monad.Trans.State (modify)
import Control.Monad.Catch (MonadCatch)
import Data.Sequence ((|>))
import Data.Text (Text)
import Language.GraphQL.Execute.Coerce
import Language.GraphQL.Error
    ( CollectErrsT
    , Error(..)
    , Resolution(..)
    , Response(..)
    )
import Prelude hiding (null)

addError :: MonadCatch m => forall a. a -> Error -> CollectErrsT m a
addError returnValue error' = modify appender >> pure returnValue
  where
   appender :: Resolution m -> Resolution m
   appender resolution@Resolution{ errors } = resolution
       { errors = errors |> error'
       }

singleError :: Serialize b => forall a. Text -> Either a (Response b)
singleError message = Right $ Response null $ pure $ Error message [] []