1 {-# LANGUAGE DeriveAnyClass #-}
2 {-# LANGUAGE DuplicateRecordFields #-} -- permit duplications for field names in multiple constructors
3 {-# LANGUAGE KindSignatures #-} -- for use of Endpoint (name :: Symbol)
4 {-# LANGUAGE PartialTypeSignatures #-} -- to automatically use suggested type hole signatures during compilation
5 {-# LANGUAGE TypeOperators #-}
7 {-# OPTIONS_GHC -fno-warn-unused-imports #-}
9 module Gargantext.API.GraphQL where
11 import Control.Monad.Base (liftBase)
12 import Control.Monad.IO.Class (liftIO)
13 import Data.ByteString.Lazy.Char8
16 import Data.List.NonEmpty (NonEmpty ((:|)))
17 import Data.Maybe (fromMaybe)
21 import Data.Morpheus.Server
24 import Data.Morpheus.Subscriptions
32 import Data.Morpheus.Types
44 import Data.Morpheus.Types.Internal.AST
46 import Data.Text (Text)
47 import qualified Data.Text.Lazy as LT
48 import Data.Text.Lazy.Encoding (decodeUtf8)
49 import Data.Typeable (Typeable)
50 import qualified Gargantext.API.GraphQL.User as GQLUser
51 import qualified Gargantext.API.GraphQL.UserInfo as GQLUserInfo
52 import Gargantext.API.Prelude (GargServerT, GargM, GargError)
53 import Gargantext.Database.Prelude (Cmd, HasConnectionPool, HasConfig)
54 import Gargantext.Database.Schema.User (UserPoly(..))
55 import Gargantext.Prelude
56 import GHC.Generics (Generic)
58 import Network.HTTP.Media ((//), (/:))
59 import Network.WebSockets
62 import qualified Prelude as Prelude
76 -- | Represents possible GraphQL queries.
79 { user_infos :: GQLUserInfo.UserInfoArgs -> m [GQLUserInfo.UserInfo]
80 , users :: GQLUser.UserArgs -> m [GQLUser.User m]
81 } deriving (Generic, GQLType)
85 { update_user_info :: GQLUserInfo.UserInfoMArgs -> m GQLUserInfo.UserInfo }
86 deriving (Generic, GQLType)
88 -- | Possible GraphQL Events, i.e. here we describe how we will
89 -- manipulate the data.
90 type EVENT m = Event Channel (Contet m)
92 -- | Channels are possible actions to call when manipulating the data.
96 deriving (Eq, Show, Generic, Hashable)
98 -- | This type describes what data we will operate on.
100 = UserContet [GQLUser.User m]
101 | UserInfoContet [GQLUserInfo.UserInfo]
103 -- | The main GraphQL resolver: how queries, mutations and
104 -- subscriptions are handled.
106 :: (HasConnectionPool env, HasConfig env)
107 => RootResolver (GargM env GargError) e Query Mutation Undefined
110 { queryResolver = Query { user_infos = GQLUserInfo.resolveUserInfos
111 , users = GQLUser.resolveUsers }
112 , mutationResolver = Mutation { update_user_info = GQLUserInfo.updateUserInfo }
113 , subscriptionResolver = Undefined }
115 -- | Main GraphQL "app".
117 :: (Typeable env, HasConnectionPool env, HasConfig env)
118 => App (EVENT (GargM env GargError)) (GargM env GargError)
119 app = deriveApp rootResolver
121 ----------------------------------------------
123 -- Now for some boilerplate to integrate the above GraphQL app with
126 -- | HTML type is needed for the GraphQL Playground.
127 data HTML deriving (Typeable)
128 instance Accept HTML where
129 contentTypes _ = "text" // "html" /: ("charset", "utf-8") :| ["text" // "html"]
130 instance MimeRender HTML ByteString where
131 mimeRender _ = Prelude.id
133 -- | Servant route for the app we defined above.
134 type GQAPI = ReqBody '[JSON] GQLRequest :> Post '[JSON] GQLResponse
135 -- type Schema = "schema" :> Get '[PlainText] Text
136 -- | Servant route for the playground.
137 type Playground = Get '[HTML] ByteString
138 -- type API' (name :: Symbol) = name :> (GQAPI :<|> Schema :<|> Playground)
139 -- | Our API consists of `GQAPI` and `Playground`.
140 type API = "gql" :> (GQAPI :<|> Playground)
143 -- ( SubApp ServerApp e
149 -- serveEndpoint publish app' = (liftIO . httpPubApp publish app') :<|> withSchema app' :<|> pure httpPlayground
151 -- withSchema :: (Applicative f) => App e m -> f Text
152 -- withSchema = pure . LT.toStrict . decodeUtf8 . render
154 -- | Implementation of our API.
157 :: (Typeable env, HasConnectionPool env, HasConfig env)
158 => ServerT API (GargM env GargError)
159 api = httpPubApp [] app :<|> pure httpPlayground