From eb90a4091c1f2586640ee49d6f91fc83c05239f6 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Sun, 24 May 2020 13:51:00 +0200 Subject: Check point --- src/Language/GraphQL/Type/Definition.hs | 20 ++++-------- src/Language/GraphQL/Type/Directive.hs | 5 +-- src/Language/GraphQL/Type/In.hs | 26 +++++++++++++++ src/Language/GraphQL/Type/Out.hs | 58 +++++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 16 deletions(-) create mode 100644 src/Language/GraphQL/Type/In.hs create mode 100644 src/Language/GraphQL/Type/Out.hs (limited to 'src/Language/GraphQL/Type') diff --git a/src/Language/GraphQL/Type/Definition.hs b/src/Language/GraphQL/Type/Definition.hs index 559611b..54eac85 100644 --- a/src/Language/GraphQL/Type/Definition.hs +++ b/src/Language/GraphQL/Type/Definition.hs @@ -8,7 +8,6 @@ module Language.GraphQL.Type.Definition ( Argument(..) , EnumType(..) , Field(..) - , FieldResolver(..) , InputField(..) , InputObjectType(..) , InputType(..) @@ -31,13 +30,13 @@ module Language.GraphQL.Type.Definition , string ) where -import qualified Data.Aeson as Aeson import Data.HashMap.Strict (HashMap) import Data.Set (Set) import Data.Text (Text) -import Language.GraphQL.AST.Core (Name, Value) +import Language.GraphQL.AST.Document (Name) import Language.GraphQL.Trans -import qualified Language.GraphQL.Type as Type +import qualified Language.GraphQL.Type.In as In +import qualified Language.GraphQL.Type.Out as Out import Prelude hiding (id) -- | Object type definition. @@ -51,17 +50,10 @@ data Field m = Field (Maybe Text) -- ^ Description. (OutputType m) -- ^ Field type. (HashMap Name Argument) -- ^ Arguments. - (FieldResolver m) -- ^ Resolver. - --- | Resolving a field can result in a leaf value or an object, which is --- represented as a list of nested resolvers, used to resolve the fields of that --- object. -data FieldResolver m - = ValueResolver (ActionT m Aeson.Value) - | NestingResolver (ActionT m (Type.Wrapping (FieldResolver m))) + (ActionT m (Out.Value m)) -- ^ Resolver. -- | Field argument definition. -data Argument = Argument (Maybe Text) InputType (Maybe Value) +data Argument = Argument (Maybe Text) InputType (Maybe In.Value) -- | Scalar type definition. -- @@ -77,7 +69,7 @@ data ScalarType = ScalarType Name (Maybe Text) data EnumType = EnumType Name (Maybe Text) (Set Text) -- | Single field of an 'InputObjectType'. -data InputField = InputField (Maybe Text) InputType (Maybe Value) +data InputField = InputField (Maybe Text) InputType (Maybe In.Value) -- | Input object type definition. -- diff --git a/src/Language/GraphQL/Type/Directive.hs b/src/Language/GraphQL/Type/Directive.hs index afd97da..9675df8 100644 --- a/src/Language/GraphQL/Type/Directive.hs +++ b/src/Language/GraphQL/Type/Directive.hs @@ -6,6 +6,7 @@ module Language.GraphQL.Type.Directive import qualified Data.HashMap.Strict as HashMap import Language.GraphQL.AST.Core +import qualified Language.GraphQL.Type.In as In -- | Directive processing status. data Status @@ -36,7 +37,7 @@ skip = handle skip' where skip' directive'@(Directive "skip" (Arguments arguments)) = case HashMap.lookup "if" arguments of - (Just (Boolean True)) -> Skip + (Just (In.Boolean True)) -> Skip _ -> Include directive' skip' directive' = Continue directive' @@ -45,6 +46,6 @@ include = handle include' where include' directive'@(Directive "include" (Arguments arguments)) = case HashMap.lookup "if" arguments of - (Just (Boolean True)) -> Include directive' + (Just (In.Boolean True)) -> Include directive' _ -> Skip include' directive' = Continue directive' diff --git a/src/Language/GraphQL/Type/In.hs b/src/Language/GraphQL/Type/In.hs new file mode 100644 index 0000000..a6d35e2 --- /dev/null +++ b/src/Language/GraphQL/Type/In.hs @@ -0,0 +1,26 @@ +-- | This module is intended to be imported qualified, to avoid name clashes +-- with 'Language.GraphQL.Type.Out'. +module Language.GraphQL.Type.In + ( Value(..) + ) where + +import Data.HashMap.Strict (HashMap) +import Data.Int (Int32) +import Data.String (IsString(..)) +import Data.Text (Text) +import Language.GraphQL.AST.Document (Name) + +-- | Represents accordingly typed GraphQL values. +data Value + = Int Int32 + | Float Double -- ^ GraphQL Float is double precision + | String Text + | Boolean Bool + | Null + | Enum Name + | List [Value] + | Object (HashMap Name Value) + deriving (Eq, Show) + +instance IsString Value where + fromString = String . fromString diff --git a/src/Language/GraphQL/Type/Out.hs b/src/Language/GraphQL/Type/Out.hs new file mode 100644 index 0000000..96bc9cf --- /dev/null +++ b/src/Language/GraphQL/Type/Out.hs @@ -0,0 +1,58 @@ +{-# LANGUAGE OverloadedStrings #-} + +-- | This module is intended to be imported qualified, to avoid name clashes +-- with 'Language.GraphQL.Type.In'. +module Language.GraphQL.Type.Out + ( Value(..) + ) where + +import Data.HashMap.Strict (HashMap) +import qualified Data.HashMap.Strict as HashMap +import Data.Int (Int32) +import Data.String (IsString(..)) +import Data.Text (Text) +import qualified Data.Text as Text +import Language.GraphQL.AST.Document (Name) +import Language.GraphQL.Trans + +-- | GraphQL distinguishes between "wrapping" and "named" types. Each wrapping +-- type can wrap other wrapping or named types. Wrapping types are lists and +-- Non-Null types (named types are nullable by default). +-- +-- This 'Value' type doesn\'t reflect this distinction exactly but it is used +-- in the resolvers to take into account that the returned value can be nullable +-- or an (arbitrary nested) list. +data Value m + = Int Int32 + | Float Double + | String Text + | Boolean Bool + | Null + | Enum Name + | List [Value m] -- ^ Arbitrary nested list. + | Object (HashMap Name (ActionT m (Value m))) + +instance IsString (Value m) where + fromString = String . fromString + +instance Show (Value m) where + show (Int integer) = "Int " ++ show integer + show (Float float) = "Float " ++ show float + show (String text) = Text.unpack $ "String " <> text + show (Boolean True) = "Boolean True" + show (Boolean False) = "Boolean False" + show Null = "Null" + show (Enum enum) = Text.unpack $ "Enum " <> enum + show (List list) = show list + show (Object object) = Text.unpack + $ "Object [" <> Text.intercalate ", " (HashMap.keys object) <> "]" + +instance Eq (Value m) where + (Int this) == (Int that) = this == that + (Float this) == (Float that) = this == that + (String this) == (String that) = this == that + (Boolean this) == (Boolean that) = this == that + (Enum this) == (Enum that) = this == that + (List this) == (List that) = this == that + (Object this) == (Object that) = HashMap.keys this == HashMap.keys that + _ == _ = False -- cgit v1.2.3