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.Lens ((#))
12 import Control.Monad.Base (liftBase)
13 import Control.Monad.IO.Class (liftIO)
14 import Data.ByteString.Lazy.Char8
17 import Data.List.NonEmpty (NonEmpty ((:|)))
18 import Data.Maybe (fromMaybe)
22 import Data.Morpheus.Server
25 import Data.Morpheus.Subscriptions
33 import Data.Morpheus.Types
45 import Data.Morpheus.Types.Internal.AST
47 import Data.Text (Text)
48 import qualified Data.Text.Lazy as LT
49 import Data.Text.Lazy.Encoding (decodeUtf8)
50 import Data.Typeable (Typeable)
51 import Gargantext.API.Admin.Auth.Types (AuthenticatedUser)
52 import qualified Gargantext.API.GraphQL.User as GQLUser
53 import qualified Gargantext.API.GraphQL.UserInfo as GQLUserInfo
54 import Gargantext.API.Prelude (GargServerT, GargM, GargError, _ServerError)
55 import Gargantext.Database.Prelude (Cmd, HasConnectionPool, HasConfig)
56 import Gargantext.Database.Schema.User (UserPoly(..))
57 import Gargantext.Prelude
58 import GHC.Generics (Generic)
60 import Network.HTTP.Media ((//), (/:))
61 import Network.WebSockets
64 import qualified Prelude as Prelude
78 import qualified Servant.Auth as SA
79 import qualified Servant.Auth.Server as SAS
81 -- | Represents possible GraphQL queries.
84 { user_infos :: GQLUserInfo.UserInfoArgs -> m [GQLUserInfo.UserInfo]
85 , users :: GQLUser.UserArgs -> m [GQLUser.User m]
86 } deriving (Generic, GQLType)
90 { update_user_info :: GQLUserInfo.UserInfoMArgs -> m Int }
91 deriving (Generic, GQLType)
93 -- | Possible GraphQL Events, i.e. here we describe how we will
94 -- manipulate the data.
95 type EVENT m = Event Channel (Contet m)
97 -- | Channels are possible actions to call when manipulating the data.
101 deriving (Eq, Show, Generic, Hashable)
103 -- | This type describes what data we will operate on.
105 = UserContet [GQLUser.User m]
106 | UserInfoContet [GQLUserInfo.UserInfo]
108 -- | The main GraphQL resolver: how queries, mutations and
109 -- subscriptions are handled.
111 :: (HasConnectionPool env, HasConfig env)
112 => RootResolver (GargM env GargError) e Query Mutation Undefined
115 { queryResolver = Query { user_infos = GQLUserInfo.resolveUserInfos
116 , users = GQLUser.resolveUsers }
117 , mutationResolver = Mutation { update_user_info = GQLUserInfo.updateUserInfo }
118 , subscriptionResolver = Undefined }
120 -- | Main GraphQL "app".
122 :: (Typeable env, HasConnectionPool env, HasConfig env)
123 => App (EVENT (GargM env GargError)) (GargM env GargError)
124 app = deriveApp rootResolver
126 ----------------------------------------------
128 -- Now for some boilerplate to integrate the above GraphQL app with
131 -- | HTML type is needed for the GraphQL Playground.
132 data HTML deriving (Typeable)
133 instance Accept HTML where
134 contentTypes _ = "text" // "html" /: ("charset", "utf-8") :| ["text" // "html"]
135 instance MimeRender HTML ByteString where
136 mimeRender _ = Prelude.id
138 -- | Servant route for the app we defined above.
139 type GQAPI = ReqBody '[JSON] GQLRequest :> Post '[JSON] GQLResponse
140 -- type Schema = "schema" :> Get '[PlainText] Text
141 -- | Servant route for the playground.
142 type Playground = Get '[HTML] ByteString
143 -- type API' (name :: Symbol) = name :> (GQAPI :<|> Schema :<|> Playground)
144 -- | Our API consists of `GQAPI` and `Playground`.
145 type API = SA.Auth '[SA.JWT, SA.Cookie] AuthenticatedUser
146 :> "gql" :> (GQAPI :<|> Playground)
149 -- ( SubApp ServerApp e
155 -- serveEndpoint publish app' = (liftIO . httpPubApp publish app') :<|> withSchema app' :<|> pure httpPlayground
157 -- withSchema :: (Applicative f) => App e m -> f Text
158 -- withSchema = pure . LT.toStrict . decodeUtf8 . render
160 -- | Implementation of our API.
163 :: (Typeable env, HasConnectionPool env, HasConfig env)
164 => ServerT API (GargM env GargError)
165 api (SAS.Authenticated _auser) = httpPubApp [] app :<|> pure httpPlayground
166 api _ = panic "401 in graphql" --SAS.throwAll (_ServerError # err401)