{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NoImplicitPrelude #-}

module Data.Morpheus.Error.Warning
  ( renderGQLErrors,
    deprecatedEnum,
    deprecatedField,
    gqlWarnings,
  )
where

import Data.Aeson (encode)
import Data.ByteString.Lazy.Char8 (unpack)
import Data.Morpheus.Types.Internal.AST.Base
  ( Description,
    Ref (..),
  )
import Data.Morpheus.Types.Internal.AST.Error
  ( GQLError,
    GQLErrors,
    at,
    msg,
  )
import Data.Morpheus.Types.Internal.AST.Name
  ( FieldName,
  )
import Language.Haskell.TH
import Relude

renderGQLErrors :: GQLErrors -> String
renderGQLErrors :: GQLErrors -> String
renderGQLErrors = ByteString -> String
unpack forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. ToJSON a => a -> ByteString
encode forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> [a]
toList

-- TODO: implement warnings, is not used
deprecatedEnum :: FieldName -> Ref FieldName -> Maybe Description -> GQLError
deprecatedEnum :: FieldName -> Ref FieldName -> Maybe Description -> GQLError
deprecatedEnum FieldName
typeName Ref {Position
refPosition :: forall name. Ref name -> Position
refPosition :: Position
refPosition, FieldName
refName :: forall name. Ref name -> name
refName :: FieldName
refName} Maybe Description
reason =
  GQLError
"the enum value "
    forall a. Semigroup a => a -> a -> a
<> forall a. Msg a => a -> GQLError
msg FieldName
typeName
    forall a. Semigroup a => a -> a -> a
<> GQLError
"."
    forall a. Semigroup a => a -> a -> a
<> forall a. Msg a => a -> GQLError
msg FieldName
refName
    forall a. Semigroup a => a -> a -> a
<> GQLError
" is deprecated."
    forall a. Semigroup a => a -> a -> a
<> forall a. Msg a => a -> GQLError
msg (forall b a. b -> (a -> b) -> Maybe a -> b
maybe Description
"" (Description
" " forall a. Semigroup a => a -> a -> a
<>) Maybe Description
reason)
    GQLError -> Position -> GQLError
`at` Position
refPosition

deprecatedField :: FieldName -> Ref FieldName -> Maybe Description -> GQLError
deprecatedField :: FieldName -> Ref FieldName -> Maybe Description -> GQLError
deprecatedField FieldName
typeName Ref {Position
refPosition :: Position
refPosition :: forall name. Ref name -> Position
refPosition, FieldName
refName :: FieldName
refName :: forall name. Ref name -> name
refName} Maybe Description
reason =
  GQLError
"the field "
    forall a. Semigroup a => a -> a -> a
<> forall a. Msg a => a -> GQLError
msg FieldName
typeName
    forall a. Semigroup a => a -> a -> a
<> GQLError
"."
    forall a. Semigroup a => a -> a -> a
<> forall a. Msg a => a -> GQLError
msg FieldName
refName
    forall a. Semigroup a => a -> a -> a
<> GQLError
" is deprecated."
    forall a. Semigroup a => a -> a -> a
<> forall a. Msg a => a -> GQLError
msg (forall b a. b -> (a -> b) -> Maybe a -> b
maybe Description
"" (Description
" " forall a. Semigroup a => a -> a -> a
<>) Maybe Description
reason)
    GQLError -> Position -> GQLError
`at` Position
refPosition

gqlWarnings :: [GQLError] -> Q ()
gqlWarnings :: [GQLError] -> Q ()
gqlWarnings [] = forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
gqlWarnings [GQLError]
warnings = forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ forall {a}. ToJSON a => a -> Q ()
handleWarning [GQLError]
warnings
  where
    handleWarning :: a -> Q ()
handleWarning a
warning =
      String -> Q ()
reportWarning (String
"Morpheus GraphQL Warning: " forall a. Semigroup a => a -> a -> a
<> (ByteString -> String
unpack forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. ToJSON a => a -> ByteString
encode) a
warning)