Commit 2dc0600b authored by Przemyslaw Kaminski's avatar Przemyslaw Kaminski

[graphql] (does not compile) attempt to lift to GargServer

parent d0f938b1
...@@ -171,6 +171,8 @@ library: ...@@ -171,6 +171,8 @@ library:
- monad-control - monad-control
- monad-logger - monad-logger
- morpheus-graphql - morpheus-graphql
- morpheus-graphql-app
- morpheus-graphql-core
- morpheus-graphql-subscriptions - morpheus-graphql-subscriptions
- mtl - mtl
- natural-transformation - natural-transformation
......
{-# LANGUAGE DeriveAnyClass #-} {-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DuplicateRecordFields #-} -- permit duplications for field names in multiple constructors {-# LANGUAGE DuplicateRecordFields #-} -- permit duplications for field names in multiple constructors
{-# LANGUAGE KindSignatures #-} -- for use of Endpoint (name :: Symbol) {-# LANGUAGE KindSignatures #-} -- for use of Endpoint (name :: Symbol)
{-# LANGUAGE PartialTypeSignatures #-} -- to automatically use suggested type hole signatures during compilation
{-# LANGUAGE TypeOperators #-} {-# LANGUAGE TypeOperators #-}
{-# OPTIONS_GHC -fno-warn-unused-imports #-} {-# OPTIONS_GHC -fno-warn-unused-imports #-}
module Gargantext.API.GraphQL where module Gargantext.API.GraphQL where
import Control.Monad.Base (liftBase)
import Control.Monad.IO.Class (liftIO) import Control.Monad.IO.Class (liftIO)
import Data.ByteString.Lazy.Char8 import Data.ByteString.Lazy.Char8
( ByteString ( ByteString
...@@ -15,6 +17,8 @@ import Data.List.NonEmpty (NonEmpty ((:|))) ...@@ -15,6 +17,8 @@ import Data.List.NonEmpty (NonEmpty ((:|)))
import Data.Morpheus import Data.Morpheus
( App ( App
, deriveApp ) , deriveApp )
import Data.Morpheus.App.Internal.Resolving
( failure )
import Data.Morpheus.Server import Data.Morpheus.Server
( httpPlayground ( httpPlayground
) )
...@@ -32,15 +36,22 @@ import Data.Morpheus.Types ...@@ -32,15 +36,22 @@ import Data.Morpheus.Types
, GQLType , GQLType
, ResolverQ , ResolverQ
, RootResolver(..) , RootResolver(..)
, Undefined , Undefined(..)
, lift
, liftEither , liftEither
, publish , publish
, render , render
) )
import Data.Morpheus.Types.Internal.AST
( msg )
import Data.Text (Text) import Data.Text (Text)
import qualified Data.Text.Lazy as LT import qualified Data.Text.Lazy as LT
import Data.Text.Lazy.Encoding (decodeUtf8) import Data.Text.Lazy.Encoding (decodeUtf8)
import Data.Typeable (Typeable) import Data.Typeable (Typeable)
import Gargantext.API.Prelude (GargServer)
import Gargantext.Database.Prelude (Cmd)
import Gargantext.Database.Query.Table.User (getUsersWithId)
import Gargantext.Database.Schema.User (UserPoly(..), UserLight)
import GHC.Generics (Generic) import GHC.Generics (Generic)
import GHC.TypeLits import GHC.TypeLits
import Network.HTTP.Media ((//), (/:)) import Network.HTTP.Media ((//), (/:))
...@@ -60,25 +71,17 @@ import Servant ...@@ -60,25 +71,17 @@ import Servant
Server, Server,
) )
import Prelude import Prelude
import Gargantext.Database.Prelude (Cmd)
-- | Our simple datatype.
data User
= User
{ name :: Text
, user_id :: Int
} deriving (Generic, GQLType)
-- | Represents possible GraphQL queries. -- | Represents possible GraphQL queries.
data Query m data Query m
= Query = Query
{ user :: UserArgs -> m User { user :: UserArgs -> m UserLight
} deriving (Generic, GQLType) } deriving (Generic, GQLType)
-- | Arguments to the "user" query. -- | Arguments to the "user" query.
data UserArgs data UserArgs
= UserArgs = UserArgs
{ name :: Text { user_id :: Int
} deriving (Generic, GQLType) } deriving (Generic, GQLType)
-- | Possible GraphQL Events, i.e. here we describe how we will -- | Possible GraphQL Events, i.e. here we describe how we will
...@@ -93,12 +96,12 @@ data Channel ...@@ -93,12 +96,12 @@ data Channel
-- | This type describes what data we will operate on. -- | This type describes what data we will operate on.
data Contet data Contet
= UserContet User = UserContet UserLight
-- | The main GraphQL resolver: how queries, mutations and -- | The main GraphQL resolver: how queries, mutations and
-- subscriptions are handled. -- subscriptions are handled.
rootResolver :: RootResolver IO EVENT Query Undefined Undefined rootResolver :: RootResolver _ EVENT Query Undefined Undefined
rootResolver = rootResolver =
RootResolver RootResolver
{ queryResolver = Query { user = resolveUser } { queryResolver = Query { user = resolveUser }
...@@ -106,15 +109,25 @@ rootResolver = ...@@ -106,15 +109,25 @@ rootResolver =
, subscriptionResolver = Undefined } , subscriptionResolver = Undefined }
-- | Function to resolve user from a query. -- | Function to resolve user from a query.
resolveUser :: UserArgs -> ResolverQ e IO User resolveUser :: UserArgs -> ResolverQ e _ UserLight
resolveUser UserArgs { name } = liftEither $ dbUser name resolveUser UserArgs { user_id } = do
liftEither $ dbUser user_id
-- user <- lift $ dbUser user_id
-- case user of
-- --Left err -> failure $ msg err
-- Left err -> error "fail"
-- Right u -> pure u
-- | Inner function to fetch the user from DB. -- | Inner function to fetch the user from DB.
dbUser :: Text -> IO (Either String User) dbUser :: Int -> Cmd err (Either String UserLight)
dbUser name = pure $ Right $ User { name, user_id = 1 } dbUser user_id = do
users <- getUsersWithId user_id
case users of
[] -> pure $ Left "User not found"
(user:_) -> pure $ Right user
-- | Main GraphQL "app". -- | Main GraphQL "app".
app :: App EVENT IO app :: App EVENT _
app = deriveApp rootResolver app = deriveApp rootResolver
---------------------------------------------- ----------------------------------------------
...@@ -127,7 +140,7 @@ data HTML deriving (Typeable) ...@@ -127,7 +140,7 @@ data HTML deriving (Typeable)
instance Accept HTML where instance Accept HTML where
contentTypes _ = "text" // "html" /: ("charset", "utf-8") :| ["text" // "html"] contentTypes _ = "text" // "html" /: ("charset", "utf-8") :| ["text" // "html"]
instance MimeRender HTML ByteString where instance MimeRender HTML ByteString where
mimeRender _ = id mimeRender _ = Prelude.id
-- | Servant route for the app we defined above. -- | Servant route for the app we defined above.
type GQAPI = ReqBody '[JSON] GQLRequest :> Post '[JSON] GQLResponse type GQAPI = ReqBody '[JSON] GQLRequest :> Post '[JSON] GQLResponse
...@@ -151,7 +164,9 @@ type API = "gql" :> (GQAPI :<|> Playground) ...@@ -151,7 +164,9 @@ type API = "gql" :> (GQAPI :<|> Playground)
-- withSchema = pure . LT.toStrict . decodeUtf8 . render -- withSchema = pure . LT.toStrict . decodeUtf8 . render
-- | Implementation of our API. -- | Implementation of our API.
api :: Server API --api :: Server API
api :: GargServer API
api = do api = do
--(wsApp, publish') <- liftIO $ webSocketsApp app --(wsApp, publish') <- liftIO $ webSocketsApp app
(liftIO . httpPubApp [] app) :<|> pure httpPlayground --(liftIO . httpPubApp [] app) :<|> pure httpPlayground
(liftBase . httpPubApp [] app) :<|> pure httpPlayground
...@@ -62,7 +62,11 @@ server env = do ...@@ -62,7 +62,11 @@ server env = do
(Proxy :: Proxy AuthContext) (Proxy :: Proxy AuthContext)
transform transform
(serverGargAPI (env ^. hasConfig . gc_url_backend_api)) (serverGargAPI (env ^. hasConfig . gc_url_backend_api))
:<|> GraphQL.api :<|> hoistServerWithContext
(Proxy :: Proxy GraphQL.API)
(Proxy :: Proxy AuthContext)
transform
GraphQL.api
:<|> frontEndServer :<|> frontEndServer
where where
transform :: forall a. GargM env GargError a -> Handler a transform :: forall a. GargM env GargError a -> Handler a
......
...@@ -13,12 +13,14 @@ Functions to deal with users, database side. ...@@ -13,12 +13,14 @@ Functions to deal with users, database side.
{-# OPTIONS_GHC -fno-warn-orphans #-} {-# OPTIONS_GHC -fno-warn-orphans #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE FunctionalDependencies #-} {-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE Arrows #-} {-# LANGUAGE Arrows #-}
{-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TemplateHaskell #-}
module Gargantext.Database.Schema.User where module Gargantext.Database.Schema.User where
import Data.Morpheus.Types (GQLType)
import Data.Text (Text) import Data.Text (Text)
import Data.Time (UTCTime) import Data.Time (UTCTime)
import Gargantext.Prelude import Gargantext.Prelude
...@@ -41,7 +43,7 @@ data UserLight = UserLight { userLight_id :: !Int ...@@ -41,7 +43,7 @@ data UserLight = UserLight { userLight_id :: !Int
, userLight_username :: !Text , userLight_username :: !Text
, userLight_email :: !Text , userLight_email :: !Text
, userLight_password :: !Text , userLight_password :: !Text
} deriving (Show, Generic) } deriving (Show, Generic, GQLType)
toUserLight :: UserDB -> UserLight toUserLight :: UserDB -> UserLight
toUserLight (UserDB id p _ _ u _ _ e _ _ _ ) = UserLight id u e p toUserLight (UserDB id p _ _ u _ _ e _ _ _ ) = UserLight id u e p
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment