{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Data.Morpheus.Parsing.Request.Operation
( parseOperation
)
where
import Data.Functor ( ($>) )
import Text.Megaparsec ( label
, optional
, (<?>)
, (<|>)
)
import Text.Megaparsec.Char ( string )
import Data.Morpheus.Parsing.Internal.Internal
( Parser
, getLocation
)
import Data.Morpheus.Parsing.Internal.Pattern
( optionalDirectives )
import Data.Morpheus.Parsing.Internal.Terms
( operator
, uniqTupleOpt
, parseName
, parseType
, spaceAndComments1
, variable
)
import Data.Morpheus.Parsing.Internal.Value
( parseDefaultValue )
import Data.Morpheus.Parsing.Request.Selection
( parseSelectionSet )
import Data.Morpheus.Types.Internal.Operation
(empty)
import Data.Morpheus.Types.Internal.AST
( Operation(..)
, Variable(..)
, OperationType(..)
, Ref(..)
, VariableContent(..)
, RAW
)
variableDefinition :: Parser (Variable RAW)
variableDefinition = label "VariableDefinition" $ do
(Ref variableName variablePosition) <- variable
operator ':'
variableType <- parseType
variableValue <- DefaultValue <$> parseDefaultValue
pure Variable{..}
parseOperationDefinition :: Parser (Operation RAW)
parseOperationDefinition = label "OperationDefinition" $ do
operationPosition <- getLocation
operationType <- parseOperationType
operationName <- optional parseName
operationArguments <- uniqTupleOpt variableDefinition
_directives <- optionalDirectives
operationSelection <- parseSelectionSet
pure Operation {..}
parseOperationType :: Parser OperationType
parseOperationType = label "OperationType" $ do
kind <-
(string "query" $> Query)
<|> (string "mutation" $> Mutation)
<|> (string "subscription" $> Subscription)
spaceAndComments1
return kind
parseAnonymousQuery :: Parser (Operation RAW)
parseAnonymousQuery = label "AnonymousQuery" $ do
operationPosition <- getLocation
operationSelection <- parseSelectionSet
pure
(Operation { operationName = Nothing
, operationType = Query
, operationArguments = empty
, ..
}
)
<?> "can't parse AnonymousQuery"
parseOperation :: Parser (Operation RAW)
parseOperation = parseAnonymousQuery <|> parseOperationDefinition