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 | DefinitionType TypeDefinition
deriving (Eq,Show) deriving (Eq,Show)
data OperationDefinition = data OperationDefinition = Query Node
Query Name [VariableDefinition] [Directive] SelectionSet | Mutation Node
| Mutation Name [VariableDefinition] [Directive] SelectionSet deriving (Eq,Show)
-- Not official yet
-- -- | Subscription Name [VariableDefinition] [Directive] SelectionSet data Node = Node Name [VariableDefinition] [Directive] SelectionSet
deriving (Eq,Show) deriving (Eq,Show)
data VariableDefinition = VariableDefinition Variable Type (Maybe DefaultValue) data VariableDefinition = VariableDefinition Variable Type (Maybe DefaultValue)
deriving (Eq,Show) deriving (Eq,Show)

View File

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