Introduce Node data type to simplify OperationDefinition

Thanks to @swolchok for the suggestion.
This commit is contained in:
Danny Navarro 2015-09-21 10:05:09 +02:00
parent 88ca3d1866
commit 3d97b3e2ff
2 changed files with 16 additions and 15 deletions

View File

@ -16,12 +16,12 @@ data Definition = DefinitionOperation OperationDefinition
| DefinitionType TypeDefinition
deriving (Eq,Show)
data OperationDefinition =
Query Name [VariableDefinition] [Directive] SelectionSet
| Mutation Name [VariableDefinition] [Directive] SelectionSet
-- Not official yet
-- -- | Subscription Name [VariableDefinition] [Directive] SelectionSet
deriving (Eq,Show)
data OperationDefinition = Query Node
| Mutation Node
deriving (Eq,Show)
data Node = Node Name [VariableDefinition] [Directive] SelectionSet
deriving (Eq,Show)
data VariableDefinition = VariableDefinition Variable Type (Maybe DefaultValue)
deriving (Eq,Show)

View File

@ -45,7 +45,6 @@ name = tok $ append <$> takeWhile1 isA_z
-- `isAlpha` handles many more Unicode Chars
isA_z = inClass $ '_' : ['A'..'Z'] ++ ['a'..'z']
-- * Document
document :: Parser Document
@ -54,7 +53,8 @@ document = whiteSpace
-- Try SelectionSet when no definition
<|> (Document . pure
. DefinitionOperation
. Query mempty empty empty
. Query
. Node mempty empty empty
<$> selectionSet)
<?> "document error!"
@ -66,14 +66,15 @@ definition = DefinitionOperation <$> operationDefinition
operationDefinition :: Parser OperationDefinition
operationDefinition =
op Query "query"
<|> op Mutation "mutation"
Query <$ tok "query" <*> node
<|> Mutation <$ tok "mutation" <*> node
<?> "operationDefinition error!"
where
op f n = f <$ tok n <*> tok name
<*> optempty variableDefinitions
<*> optempty directives
<*> selectionSet
node :: Parser Node
node = Node <$> name
<*> optempty variableDefinitions
<*> optempty directives
<*> selectionSet
variableDefinitions :: Parser [VariableDefinition]
variableDefinitions = parens (many1 variableDefinition)