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.App.Internal.Resolving
23 import Data.Morpheus.Server
26 import Data.Morpheus.Subscriptions
34 import Data.Morpheus.Types
46 import Data.Morpheus.Types.Internal.AST
48 import Data.Text (Text)
49 import qualified Data.Text.Lazy as LT
50 import Data.Text.Lazy.Encoding (decodeUtf8)
51 import Data.Typeable (Typeable)
52 import Gargantext.API.GraphQL.User
53 import Gargantext.API.Prelude (GargServerT, GargM, GargError)
54 import Gargantext.Database.Prelude (Cmd, HasConnectionPool, HasConfig)
55 import Gargantext.Database.Schema.User (UserPoly(..), UserLight)
56 import Gargantext.Prelude
57 import GHC.Generics (Generic)
59 import Network.HTTP.Media ((//), (/:))
60 import Network.WebSockets
63 import qualified Prelude as Prelude
77 -- | Represents possible GraphQL queries.
80 { users :: UserArgs -> m [UserLight]
81 } deriving (Generic, GQLType)
83 -- | Possible GraphQL Events, i.e. here we describe how we will
84 -- manipulate the data.
85 type EVENT = Event Channel Contet
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 [UserLight]
98 -- | The main GraphQL resolver: how queries, mutations and
99 -- subscriptions are handled.
101 :: (HasConnectionPool env, HasConfig env)
102 => RootResolver (GargM env GargError) EVENT Query Undefined Undefined
105 { queryResolver = Query { users = resolveUsers }
106 , mutationResolver = Undefined
107 , subscriptionResolver = Undefined }
109 -- | Main GraphQL "app".
111 :: (Typeable env, HasConnectionPool env, HasConfig env)
112 => App EVENT (GargM env GargError)
113 app = deriveApp rootResolver
115 ----------------------------------------------
117 -- Now for some boilerplate to integrate the above GraphQL app with
120 -- | HTML type is needed for the GraphQL Playground.
121 data HTML deriving (Typeable)
122 instance Accept HTML where
123 contentTypes _ = "text" // "html" /: ("charset", "utf-8") :| ["text" // "html"]
124 instance MimeRender HTML ByteString where
125 mimeRender _ = Prelude.id
127 -- | Servant route for the app we defined above.
128 type GQAPI = ReqBody '[JSON] GQLRequest :> Post '[JSON] GQLResponse
129 -- type Schema = "schema" :> Get '[PlainText] Text
130 -- | Servant route for the playground.
131 type Playground = Get '[HTML] ByteString
132 -- type API' (name :: Symbol) = name :> (GQAPI :<|> Schema :<|> Playground)
133 -- | Our API consists of `GQAPI` and `Playground`.
134 type API = "gql" :> (GQAPI :<|> Playground)
137 -- ( SubApp ServerApp e
143 -- serveEndpoint publish app' = (liftIO . httpPubApp publish app') :<|> withSchema app' :<|> pure httpPlayground
145 -- withSchema :: (Applicative f) => App e m -> f Text
146 -- withSchema = pure . LT.toStrict . decodeUtf8 . render
148 -- | Implementation of our API.
151 :: (Typeable env, HasConnectionPool env, HasConfig env)
152 => ServerT API (GargM env GargError)
153 api = httpPubApp [] app :<|> pure httpPlayground