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 module Gargantext.API.GraphQL where
9 import Data.ByteString.Lazy.Char8
12 import Data.List.NonEmpty (NonEmpty ((:|)))
17 import Data.Morpheus.Server
20 import Data.Morpheus.Subscriptions
25 import Data.Morpheus.Types
33 import Gargantext.API.Admin.Auth.Types (AuthenticatedUser)
34 import Gargantext.API.Admin.Orchestrator.Types (JobLog)
35 import Gargantext.API.Prelude (HasJobEnv')
36 import qualified Gargantext.API.GraphQL.Annuaire as GQLA
37 import qualified Gargantext.API.GraphQL.AsyncTask as GQLAT
38 import qualified Gargantext.API.GraphQL.IMT as GQLIMT
39 import qualified Gargantext.API.GraphQL.Node as GQLNode
40 import qualified Gargantext.API.GraphQL.User as GQLUser
41 import qualified Gargantext.API.GraphQL.UserInfo as GQLUserInfo
42 import qualified Gargantext.API.GraphQL.TreeFirstLevel as GQLTree
43 import Gargantext.API.Prelude (GargM, GargError)
44 import Gargantext.Core.Mail.Types (HasMail)
45 import Gargantext.Database.Prelude (HasConnectionPool, HasConfig)
46 import Gargantext.Prelude
47 import GHC.Generics (Generic)
48 import Network.HTTP.Media ((//), (/:))
49 import qualified Prelude
61 import qualified Servant.Auth as SA
62 import qualified Servant.Auth.Server as SAS
63 import Gargantext.API.Admin.Types (HasSettings)
65 -- | Represents possible GraphQL queries.
68 { annuaire_contacts :: GQLA.AnnuaireContactArgs -> m [GQLA.AnnuaireContact]
69 , imt_schools :: GQLIMT.SchoolsArgs -> m [GQLIMT.School]
70 , job_logs :: GQLAT.JobLogArgs -> m (Map Int JobLog)
71 , nodes :: GQLNode.NodeArgs -> m [GQLNode.Node]
72 , node_parent :: GQLNode.NodeParentArgs -> m [GQLNode.Node]
73 , user_infos :: GQLUserInfo.UserInfoArgs -> m [GQLUserInfo.UserInfo]
74 , users :: GQLUser.UserArgs -> m [GQLUser.User m]
75 , tree :: GQLTree.TreeArgs -> m (GQLTree.TreeFirstLevel m)
76 } deriving (Generic, GQLType)
80 { update_user_info :: GQLUserInfo.UserInfoMArgs -> m Int }
81 deriving (Generic, GQLType)
83 -- | Possible GraphQL Events, i.e. here we describe how we will
84 -- manipulate the data.
85 type EVENT m = Event Channel (Contet m)
87 -- | Channels are possible actions to call when manipulating the data.
91 deriving (Eq, Show, Generic, Hashable)
93 -- | This type describes what data we will operate on.
95 = UserContet [GQLUser.User m]
96 | UserInfoContet [GQLUserInfo.UserInfo]
98 -- | The main GraphQL resolver: how queries, mutations and
99 -- subscriptions are handled.
101 :: (HasConnectionPool env, HasConfig env, HasMail env, HasJobEnv' env, HasSettings env)
102 => RootResolver (GargM env GargError) e Query Mutation Undefined
105 { queryResolver = Query { annuaire_contacts = GQLA.resolveAnnuaireContacts
106 , imt_schools = GQLIMT.resolveSchools
107 , job_logs = GQLAT.resolveJobLogs
108 , nodes = GQLNode.resolveNodes
109 , node_parent = GQLNode.resolveNodeParent
110 , user_infos = GQLUserInfo.resolveUserInfos
111 , users = GQLUser.resolveUsers
112 , tree = GQLTree.resolveTree }
113 , mutationResolver = Mutation { update_user_info = GQLUserInfo.updateUserInfo }
114 , subscriptionResolver = Undefined }
116 -- | Main GraphQL "app".
118 :: (Typeable env, HasConnectionPool env, HasConfig env, HasMail env, HasJobEnv' env, HasSettings env)
119 => App (EVENT (GargM env GargError)) (GargM env GargError)
120 app = deriveApp rootResolver
122 ----------------------------------------------
124 -- Now for some boilerplate to integrate the above GraphQL app with
127 -- | HTML type is needed for the GraphQL Playground.
128 data HTML deriving (Typeable)
129 instance Accept HTML where
130 contentTypes _ = "text" // "html" /: ("charset", "utf-8") :| ["text" // "html"]
131 instance MimeRender HTML ByteString where
132 mimeRender _ = Prelude.id
134 -- | Servant route for the app we defined above.
135 type GQAPI = ReqBody '[JSON] GQLRequest :> Post '[JSON] GQLResponse
136 -- type Schema = "schema" :> Get '[PlainText] Text
137 -- | Servant route for the playground.
138 type Playground = Get '[HTML] ByteString
139 -- type API' (name :: Symbol) = name :> (GQAPI :<|> Schema :<|> Playground)
140 -- | Our API consists of `GQAPI` and `Playground`.
141 type API = SA.Auth '[SA.JWT, SA.Cookie] AuthenticatedUser
142 :> "gql" :> (GQAPI :<|> Playground)
148 -- ( SubApp ServerApp e
154 -- serveEndpoint publish app' = (liftIO . httpPubApp publish app') :<|> withSchema app' :<|> pure httpPlayground
156 -- withSchema :: (Applicative f) => App e m -> f Text
157 -- withSchema = pure . LT.toStrict . decodeUtf8 . render
159 -- | Implementation of our API.
162 :: (Typeable env, HasConnectionPool env, HasConfig env, HasMail env, HasJobEnv' env, HasSettings env)
163 => ServerT API (GargM env GargError)
164 api (SAS.Authenticated _auser) = httpPubApp [] app :<|> pure httpPlayground
165 api _ = panic "401 in graphql" -- SAS.throwAll (_ServerError # err401)