Reuse common types from AST.Core

This commit is contained in:
Eugen Wissner 2019-07-18 05:10:02 +02:00
parent 74801b0483
commit bc6a7dddd1
5 changed files with 26 additions and 53 deletions

View File

@ -6,7 +6,6 @@ module Language.GraphQL.AST
( Alias
, Argument(..)
, Arguments
, DefaultValue
, Definition(..)
, Directive(..)
, Directives
@ -16,11 +15,9 @@ module Language.GraphQL.AST
, FragmentName
, FragmentSpread(..)
, InlineFragment(..)
, ListValue
, Name
, NonNullType(..)
, ObjectField(..)
, ObjectValue
, OperationDefinition(..)
, OperationType(..)
, Selection(..)
@ -29,7 +26,6 @@ module Language.GraphQL.AST
, Type(..)
, TypeCondition
, Value(..)
, Variable
, VariableDefinition(..)
, VariableDefinitions
) where
@ -37,10 +33,9 @@ module Language.GraphQL.AST
import Data.Int (Int32)
import Data.List.NonEmpty (NonEmpty)
import Data.Text (Text)
-- * Name
type Name = Text
import Language.GraphQL.AST.Core ( Alias
, Name
)
-- * Document
@ -78,8 +73,6 @@ data Selection = SelectionField Field
data Field = Field (Maybe Alias) Name Arguments Directives SelectionSetOpt
deriving (Eq,Show)
type Alias = Name
-- * Arguments
type Arguments = [Argument]
@ -101,48 +94,29 @@ type FragmentName = Name
type TypeCondition = Name
-- Input Values
-- * Input values
data Value = ValueVariable Variable
| ValueInt IntValue
| ValueFloat FloatValue
| ValueString StringValue
| ValueBoolean BooleanValue
data Value = ValueVariable Name
| ValueInt Int32
| ValueFloat Double
| ValueString Text
| ValueBoolean Bool
| ValueNull
| ValueEnum EnumValue
| ValueList ListValue
| ValueObject ObjectValue
deriving (Eq,Show)
| ValueEnum Name
| ValueList [Value]
| ValueObject [ObjectField]
deriving (Eq, Show)
type IntValue = Int32
-- GraphQL Float is double precison
type FloatValue = Double
type StringValue = Text
type BooleanValue = Bool
type EnumValue = Name
type ListValue = [Value]
type ObjectValue = [ObjectField]
data ObjectField = ObjectField Name Value deriving (Eq,Show)
data ObjectField = ObjectField Name Value deriving (Eq, Show)
-- * Variables
type VariableDefinitions = [VariableDefinition]
data VariableDefinition = VariableDefinition Variable Type (Maybe DefaultValue)
data VariableDefinition = VariableDefinition Name Type (Maybe Value)
deriving (Eq,Show)
type Variable = Name
type DefaultValue = Value
-- * Input Types
-- * Input types
data Type = TypeNamed Name
| TypeList Type

View File

@ -1,6 +1,7 @@
-- | This is the AST meant to be executed.
module Language.GraphQL.AST.Core
( Argument(..)
( Alias
, Argument(..)
, Document
, Field(..)
, Name
@ -15,6 +16,7 @@ import Data.String
import Data.Text (Text)
-- | Name
type Name = Text
type Document = NonEmpty Operation

View File

@ -10,16 +10,13 @@ import Data.Either (partitionEithers)
import Data.Foldable (fold, foldMap)
import qualified Data.List.NonEmpty as NonEmpty
import Data.Monoid (Alt(Alt,getAlt), (<>))
import Data.Text (Text)
import qualified Language.GraphQL.AST as Full
import qualified Language.GraphQL.AST.Core as Core
import qualified Language.GraphQL.Schema as Schema
type Name = Text
-- | Replaces a fragment name by a list of 'Field'. If the name doesn't match an
-- empty list is returned.
type Fragmenter = Name -> [Core.Field]
type Fragmenter = Core.Name -> [Core.Field]
-- TODO: Replace Maybe by MonadThrow with CustomError
document :: Schema.Subs -> Full.Document -> Maybe Core.Document

View File

@ -41,10 +41,10 @@ variableDefinition :: VariableDefinition -> Text
variableDefinition (VariableDefinition var ty dv) =
variable var <> ":" <> type_ ty <> maybe mempty defaultValue dv
defaultValue :: DefaultValue -> Text
defaultValue :: Value -> Text
defaultValue val = "=" <> value val
variable :: Variable -> Text
variable :: Name -> Text
variable var = "$" <> var
selectionSet :: SelectionSet -> Text
@ -113,10 +113,10 @@ booleanValue False = "false"
stringValue :: Text -> Text
stringValue = quotes
listValue :: ListValue -> Text
listValue :: [Value] -> Text
listValue = bracketsCommas value
objectValue :: ObjectValue -> Text
objectValue :: [ObjectField] -> Text
objectValue = bracesCommas objectField
objectField :: ObjectField -> Text

View File

@ -140,10 +140,10 @@ variableDefinition = VariableDefinition <$> variable
<* colon
<*> type_
<*> optional defaultValue
variable :: Parser Variable
variable :: Parser Name
variable = dollar *> name
defaultValue :: Parser DefaultValue
defaultValue :: Parser Value
defaultValue = equals *> value
-- * Input Types