{-# LANGUAGE ConstrainedClassMethods #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE NoImplicitPrelude #-}

module Data.Morpheus.Client.Fetch
  ( Fetch (..),
    deriveFetch,
  )
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.Internal.TH
  ( applyCons,
    toCon,
    typeInstanceDec,
  )
import Data.Morpheus.Types.IO
  ( GQLRequest (..),
    JSONResponse (..),
  )
import Data.Morpheus.Types.Internal.AST
  ( FieldName,
    TypeName,
  )
import Data.Text
  ( pack,
  )
import Language.Haskell.TH
import Relude hiding (ByteString, Type)

fixVars :: A.Value -> Maybe A.Value
fixVars :: Value -> Maybe Value
fixVars Value
x
  | Value
x Value -> Value -> Bool
forall a. Eq a => a -> a -> Bool
== Value
A.emptyArray = Maybe Value
forall a. Maybe a
Nothing
  | Bool
otherwise = Value -> Maybe Value
forall a. a -> Maybe a
Just Value
x

class Fetch a where
  type Args a :: *
  __fetch ::
    (Monad m, Show a, ToJSON (Args a), FromJSON a) =>
    String ->
    FieldName ->
    (ByteString -> m ByteString) ->
    Args a ->
    m (Either String a)
  __fetch String
strQuery FieldName
opName ByteString -> m ByteString
trans Args a
vars = (ByteString -> Either String (JSONResponse a)
forall a. FromJSON a => ByteString -> Either String a
eitherDecode (ByteString -> Either String (JSONResponse a))
-> (JSONResponse a -> Either String a)
-> ByteString
-> Either String a
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> JSONResponse a -> Either String a
forall b a. (Show b, IsString a) => JSONResponse b -> Either a b
processResponse) (ByteString -> Either String a)
-> m ByteString -> m (Either String a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> m ByteString
trans (GQLRequest -> ByteString
forall a. ToJSON a => a -> ByteString
encode GQLRequest
gqlReq)
    where
      gqlReq :: GQLRequest
gqlReq = GQLRequest :: Maybe FieldName -> Text -> Maybe Value -> GQLRequest
GQLRequest {operationName :: Maybe FieldName
operationName = FieldName -> Maybe FieldName
forall a. a -> Maybe a
Just FieldName
opName, query :: Text
query = String -> Text
pack String
strQuery, variables :: Maybe Value
variables = Value -> Maybe Value
fixVars (Args a -> Value
forall a. ToJSON a => a -> Value
toJSON Args a
vars)}
      -------------------------------------------------------------
      processResponse :: JSONResponse b -> Either a b
processResponse JSONResponse {responseData :: forall a. JSONResponse a -> Maybe a
responseData = Just b
x} = b -> Either a b
forall a b. b -> Either a b
Right b
x
      processResponse JSONResponse b
invalidResponse = a -> Either a b
forall a b. a -> Either a b
Left (JSONResponse b -> a
forall b a. (Show a, IsString b) => a -> b
show JSONResponse b
invalidResponse)
  fetch :: (Monad m, FromJSON a) => (ByteString -> m ByteString) -> Args a -> m (Either String a)

deriveFetch :: Type -> TypeName -> String -> Q [Dec]
deriveFetch :: Type -> TypeName -> String -> Q [Dec]
deriveFetch Type
resultType TypeName
typeName String
queryString =
  Dec -> [Dec]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Dec -> [Dec]) -> Q Dec -> Q [Dec]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CxtQ -> TypeQ -> [Q Dec] -> Q Dec
instanceD ([TypeQ] -> CxtQ
cxt []) TypeQ
iHead [Q Dec]
methods
  where
    iHead :: TypeQ
iHead = Name -> [TypeName] -> TypeQ
forall con cons.
(ToName con, ToName cons) =>
con -> [cons] -> TypeQ
applyCons ''Fetch [TypeName
typeName]
    methods :: [Q Dec]
methods =
      [ Name -> [ClauseQ] -> Q Dec
funD 'fetch [[PatQ] -> BodyQ -> [Q Dec] -> ClauseQ
clause [] (ExpQ -> BodyQ
normalB [|__fetch queryString typeName|]) []],
        Dec -> Q Dec
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Dec -> Q Dec) -> Dec -> Q Dec
forall a b. (a -> b) -> a -> b
$ Name -> Type -> Type -> Dec
typeInstanceDec ''Args (TypeName -> Type
forall a b. ToCon a b => a -> b
toCon TypeName
typeName) Type
resultType
      ]