From 73555332681a3702db5e277f21a53c628c3a524f Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Tue, 25 Aug 2020 21:03:42 +0200 Subject: Validate single root field in subscriptions --- src/Language/GraphQL/AST/Parser.hs | 45 +++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 20 deletions(-) (limited to 'src/Language/GraphQL/AST/Parser.hs') diff --git a/src/Language/GraphQL/AST/Parser.hs b/src/Language/GraphQL/AST/Parser.hs index 687d8f5..7bc51cb 100644 --- a/src/Language/GraphQL/AST/Parser.hs +++ b/src/Language/GraphQL/AST/Parser.hs @@ -21,7 +21,8 @@ import Language.GraphQL.AST.DirectiveLocation import Language.GraphQL.AST.Document import Language.GraphQL.AST.Lexer import Text.Megaparsec - ( SourcePos(..) + ( MonadParsec(..) + , SourcePos(..) , getSourcePos , lookAhead , option @@ -37,15 +38,11 @@ document = unicodeBOM *> lexeme (NonEmpty.some definition) definition :: Parser Definition -definition = executableDefinition' +definition = ExecutableDefinition <$> executableDefinition <|> typeSystemDefinition' <|> typeSystemExtension' "Definition" where - executableDefinition' = do - location <- getLocation - definition' <- executableDefinition - pure $ ExecutableDefinition definition' location typeSystemDefinition' = do location <- getLocation definition' <- typeSystemDefinition @@ -349,16 +346,22 @@ operationTypeDefinition = OperationTypeDefinition "OperationTypeDefinition" operationDefinition :: Parser OperationDefinition -operationDefinition = SelectionSet <$> selectionSet +operationDefinition = shorthand <|> operationDefinition' "OperationDefinition" where - operationDefinition' - = OperationDefinition <$> operationType - <*> optional name - <*> variableDefinitions - <*> directives - <*> selectionSet + shorthand = do + location <- getLocation + selectionSet' <- selectionSet + pure $ SelectionSet selectionSet' location + operationDefinition' = do + location <- getLocation + operationType' <- operationType + operationName <- optional name + variableDefinitions' <- variableDefinitions + directives' <- directives + selectionSet' <- selectionSet + pure $ OperationDefinition operationType' operationName variableDefinitions' directives' selectionSet' location operationType :: Parser OperationType operationType = Query <$ symbol "query" @@ -412,13 +415,15 @@ inlineFragment = InlineFragment "InlineFragment" fragmentDefinition :: Parser FragmentDefinition -fragmentDefinition = FragmentDefinition - <$ symbol "fragment" - <*> name - <*> typeCondition - <*> directives - <*> selectionSet - "FragmentDefinition" +fragmentDefinition = label "FragmentDefinition" $ do + location <- getLocation + _ <- symbol "fragment" + fragmentName' <- name + typeCondition' <- typeCondition + directives' <- directives + selectionSet' <- selectionSet + pure $ FragmentDefinition + fragmentName' typeCondition' directives' selectionSet' location fragmentName :: Parser Name fragmentName = but (symbol "on") *> name "FragmentName" -- cgit v1.2.3