-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResolvers.hs
46 lines (37 loc) · 1.54 KB
/
Resolvers.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
{-# LANGUAGE DeriveAnyClass, DeriveGeneric, DuplicateRecordFields, TypeFamilies #-}
module Resolvers (ResolverContext, newContext, resolve) where
import Prelude hiding (id)
import Control.Monad.IO.Class (liftIO)
import Data.Morpheus (interpreter)
import Data.Morpheus.Types (RootResolver (..), Undefined (..), GQLType (..), ResolverQ)
import Data.ByteString.Lazy (ByteString)
import GHC.Generics (Generic)
import qualified Schema as S
import qualified Projections as P
data Query m = Query
{ patient :: PatientArguments -> m (Maybe S.Patient)
, patients :: m [S.Patient]
} deriving (Generic, GQLType)
newtype PatientArguments = PatientArguments
{ id :: S.PatientId
} deriving (Generic, GQLType)
-- resolvers
newtype ResolverContext = ResolverContext P.Connection
newContext :: P.Connection -> ResolverContext
newContext = ResolverContext
resolvePatients :: ResolverContext -> ResolverQ e IO [S.Patient]
resolvePatients (ResolverContext db) = liftIO $ P.fetchPatients db
resolvePatient :: ResolverContext -> PatientArguments -> ResolverQ e IO (Maybe S.Patient)
resolvePatient (ResolverContext db) (PatientArguments pid) = liftIO $ P.fetchPatient db pid
resolve :: ResolverContext -> ByteString -> IO ByteString
resolve context = interpreter root
where
root :: RootResolver IO () Query Undefined Undefined
root = RootResolver
{ queryResolver = Query
{ patient = resolvePatient context
, patients = resolvePatients context
}
, mutationResolver = Undefined
, subscriptionResolver = Undefined
}