{-# LANGUAGE ConstrainedClassMethods #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE NoImplicitPrelude #-}

module Data.Morpheus.Client.Fetch
  ( Fetch (..),
    decodeResponse,
  )
where

import Data.Aeson
  ( FromJSON,
    ToJSON (..),
    eitherDecode,
    encode,
  )
import Data.ByteString.Lazy (ByteString)
import Data.Morpheus.Client.Fetch.RequestType (Request (Request), RequestType (RequestArgs), processResponse, toRequest)
import Data.Morpheus.Client.Fetch.Types
  ( FetchError (..),
  )
import Relude hiding (ByteString)

decodeResponse :: FromJSON a => ByteString -> Either (FetchError a) a
decodeResponse :: forall a. FromJSON a => ByteString -> Either (FetchError a) a
decodeResponse = (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

class (RequestType a, ToJSON (Args a), FromJSON a) => Fetch a where
  type Args a :: Type
  fetch :: Monad m => (ByteString -> m ByteString) -> Args a -> m (Either (FetchError a) a)

instance (RequestType a, ToJSON (Args a), FromJSON a) => Fetch a where
  type Args a = RequestArgs a
  fetch :: forall (m :: * -> *).
Monad m =>
(ByteString -> m ByteString)
-> Args a -> m (Either (FetchError a) a)
fetch ByteString -> m ByteString
f Args a
args = forall a. FromJSON a => ByteString -> Either (FetchError a) a
decodeResponse forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> m ByteString
f (forall a. ToJSON a => a -> ByteString
encode forall a b. (a -> b) -> a -> b
$ forall a.
(RequestType a, ToJSON (RequestArgs a)) =>
Request a -> GQLRequest
toRequest Request a
request)
    where
      request :: Request a
      request :: Request a
request = forall a. RequestArgs a -> Request a
Request Args a
args