{-# LANGUAGE ConstrainedClassMethods #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE NoImplicitPrelude #-}
module Data.Morpheus.Client.Fetch
( Fetch (..),
)
where
import Data.Aeson
( FromJSON,
ToJSON (..),
eitherDecode,
encode,
)
import qualified Data.Aeson as A
import qualified Data.Aeson.Types as A
import Data.ByteString.Lazy (ByteString)
import Data.Morpheus.Client.Internal.Types
( FetchError (..),
)
import Data.Morpheus.Client.Schema.JSON.Types
( JSONResponse (..),
)
import Data.Morpheus.Types.IO
( GQLRequest (..),
)
import Data.Morpheus.Types.Internal.AST
( FieldName,
)
import Data.Text
( pack,
)
import Relude hiding (ByteString)
fixVars :: A.Value -> Maybe A.Value
fixVars :: Value -> Maybe Value
fixVars Value
x
| Value
x forall a. Eq a => a -> a -> Bool
== Value
A.emptyArray = forall a. Maybe a
Nothing
| Bool
otherwise = forall a. a -> Maybe a
Just Value
x
class Fetch a where
type Args a :: Type
__fetch ::
(Monad m, Show a, ToJSON (Args a), FromJSON a) =>
String ->
FieldName ->
(ByteString -> m ByteString) ->
Args a ->
m (Either (FetchError a) a)
__fetch String
strQuery FieldName
opName ByteString -> m ByteString
trans Args a
vars = ((forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first forall a. String -> FetchError a
FetchErrorParseFailure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. FromJSON a => ByteString -> Either String a
eitherDecode) forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall {a}. JSONResponse a -> Either (FetchError a) a
processResponse) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> m ByteString
trans (forall a. ToJSON a => a -> ByteString
encode GQLRequest
gqlReq)
where
gqlReq :: GQLRequest
gqlReq = GQLRequest {operationName :: Maybe FieldName
operationName = forall a. a -> Maybe a
Just FieldName
opName, query :: Text
query = String -> Text
pack String
strQuery, variables :: Maybe Value
variables = Value -> Maybe Value
fixVars (forall a. ToJSON a => a -> Value
toJSON Args a
vars)}
processResponse :: JSONResponse a -> Either (FetchError a) a
processResponse JSONResponse {$sel:responseData:JSONResponse :: forall a. JSONResponse a -> Maybe a
responseData = Just a
x, $sel:responseErrors:JSONResponse :: forall a. JSONResponse a -> [GQLError]
responseErrors = []} = forall a b. b -> Either a b
Right a
x
processResponse JSONResponse {$sel:responseData:JSONResponse :: forall a. JSONResponse a -> Maybe a
responseData = Maybe a
Nothing, $sel:responseErrors:JSONResponse :: forall a. JSONResponse a -> [GQLError]
responseErrors = []} = forall a b. a -> Either a b
Left forall a. FetchError a
FetchErrorNoResult
processResponse JSONResponse {$sel:responseData:JSONResponse :: forall a. JSONResponse a -> Maybe a
responseData = Maybe a
result, $sel:responseErrors:JSONResponse :: forall a. JSONResponse a -> [GQLError]
responseErrors = (GQLError
x : [GQLError]
xs)} = forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ forall a. GQLErrors -> Maybe a -> FetchError a
FetchErrorProducedErrors (GQLError
x forall a. a -> [a] -> NonEmpty a
:| [GQLError]
xs) Maybe a
result
fetch :: (Monad m, FromJSON a) => (ByteString -> m ByteString) -> Args a -> m (Either (FetchError a) a)