summaryrefslogtreecommitdiff
path: root/Data/GraphQL/Schema.hs
blob: 8ceb11f2ed092c4ab3bf9c6c030c3578fce63564 (plain)
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
47
48
49
50
51
{-# LANGUAGE CPP #-}
module Data.GraphQL.Schema where

#if !MIN_VERSION_base(4,8,0)
import Control.Applicative ((<$>))
#endif

import Data.Aeson (ToJSON(toJSON))
import Data.HashMap.Strict (HashMap)
import Data.Text (Text)

data Schema f = Schema (QueryRoot f) -- (Maybe  MutationRoot)

type QueryRoot f = Resolver f

type Resolver f = Input -> f Output

data Output = OutputObject (HashMap Text Output)
            | OutputList [Output]
            | OutputScalar Scalar
            | OutputEnum Text
              deriving (Show)
           -- | OutputUnion [Output]
           -- | OutputNonNull (Output)

type Argument = (Text, Scalar)

data Input = InputField Text [Argument] [Input]
             deriving (Show)

-- TODO: Make ScalarInt Int32
data Scalar = ScalarInt     Int
            | ScalarFloat   Double
            | ScalarString  Text
            | ScalarBoolean Bool
            | ScalarID      Text
              deriving (Show)

instance ToJSON Scalar where
    toJSON (ScalarInt     x) = toJSON x
    toJSON (ScalarFloat   x) = toJSON x
    toJSON (ScalarString  x) = toJSON x
    toJSON (ScalarBoolean x) = toJSON x
    toJSON (ScalarID      x) = toJSON x

instance ToJSON Output where
    toJSON (OutputObject x) = toJSON $ toJSON <$> x
    toJSON (OutputList   x) = toJSON $ toJSON <$> x
    toJSON (OutputScalar x) = toJSON x
    toJSON (OutputEnum   x) = toJSON x