2017-01-28 18:15:14 +01:00
|
|
|
{-# LANGUAGE LambdaCase #-}
|
2019-06-21 10:44:58 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2019-09-25 05:35:36 +02:00
|
|
|
|
|
|
|
-- | @GraphQL@ document parser.
|
2019-11-03 10:42:10 +01:00
|
|
|
module Language.GraphQL.AST.Parser
|
2019-07-14 05:58:05 +02:00
|
|
|
( document
|
|
|
|
) where
|
2015-09-13 13:51:37 +02:00
|
|
|
|
2019-12-25 06:45:29 +01:00
|
|
|
import Control.Applicative (Alternative(..), optional)
|
2019-06-21 10:44:58 +02:00
|
|
|
import Data.List.NonEmpty (NonEmpty(..))
|
2019-07-07 06:31:53 +02:00
|
|
|
import Language.GraphQL.AST
|
2019-12-26 13:00:47 +01:00
|
|
|
import qualified Language.GraphQL.AST.Document as Document
|
2019-11-03 10:42:10 +01:00
|
|
|
import Language.GraphQL.AST.Lexer
|
2019-12-25 06:45:29 +01:00
|
|
|
import Text.Megaparsec (lookAhead, option, try, (<?>))
|
2015-09-13 13:51:37 +02:00
|
|
|
|
2019-08-26 10:14:46 +02:00
|
|
|
-- | Parser for the GraphQL documents.
|
2019-12-26 13:00:47 +01:00
|
|
|
document :: Parser Document.Document
|
2019-12-25 06:45:29 +01:00
|
|
|
document = unicodeBOM
|
|
|
|
>> spaceConsumer
|
2019-12-26 13:00:47 +01:00
|
|
|
>> lexeme (manyNE $ Document.ExecutableDefinition <$> definition)
|
2015-09-13 13:51:37 +02:00
|
|
|
|
2019-12-25 06:45:29 +01:00
|
|
|
definition :: Parser ExecutableDefinition
|
2015-09-13 13:51:37 +02:00
|
|
|
definition = DefinitionOperation <$> operationDefinition
|
|
|
|
<|> DefinitionFragment <$> fragmentDefinition
|
|
|
|
<?> "definition error!"
|
|
|
|
|
|
|
|
operationDefinition :: Parser OperationDefinition
|
2017-01-28 18:15:14 +01:00
|
|
|
operationDefinition = OperationSelectionSet <$> selectionSet
|
|
|
|
<|> OperationDefinition <$> operationType
|
2017-02-04 00:08:40 +01:00
|
|
|
<*> optional name
|
2017-01-28 18:15:14 +01:00
|
|
|
<*> opt variableDefinitions
|
|
|
|
<*> opt directives
|
|
|
|
<*> selectionSet
|
|
|
|
<?> "operationDefinition error"
|
2015-09-13 17:44:31 +02:00
|
|
|
|
2017-01-28 18:15:14 +01:00
|
|
|
operationType :: Parser OperationType
|
2019-06-21 10:44:58 +02:00
|
|
|
operationType = Query <$ symbol "query"
|
|
|
|
<|> Mutation <$ symbol "mutation"
|
|
|
|
<?> "operationType error"
|
2015-09-13 13:51:37 +02:00
|
|
|
|
2017-01-28 18:15:14 +01:00
|
|
|
-- * SelectionSet
|
2015-09-13 13:51:37 +02:00
|
|
|
|
|
|
|
selectionSet :: Parser SelectionSet
|
2017-01-28 18:15:14 +01:00
|
|
|
selectionSet = braces $ manyNE selection
|
|
|
|
|
|
|
|
selectionSetOpt :: Parser SelectionSetOpt
|
2019-06-21 10:44:58 +02:00
|
|
|
selectionSetOpt = braces $ some selection
|
2015-09-13 13:51:37 +02:00
|
|
|
|
|
|
|
selection :: Parser Selection
|
2019-12-25 06:45:29 +01:00
|
|
|
selection = field
|
|
|
|
<|> try fragmentSpread
|
|
|
|
<|> inlineFragment
|
|
|
|
<?> "selection error!"
|
2015-09-13 13:51:37 +02:00
|
|
|
|
2017-01-28 18:15:14 +01:00
|
|
|
-- * Field
|
|
|
|
|
2019-12-25 06:45:29 +01:00
|
|
|
field :: Parser Selection
|
|
|
|
field = Field
|
|
|
|
<$> optional alias
|
|
|
|
<*> name
|
|
|
|
<*> opt arguments
|
|
|
|
<*> opt directives
|
|
|
|
<*> opt selectionSetOpt
|
2015-09-13 13:51:37 +02:00
|
|
|
|
|
|
|
alias :: Parser Alias
|
2019-06-21 10:44:58 +02:00
|
|
|
alias = try $ name <* colon
|
2015-09-13 13:51:37 +02:00
|
|
|
|
2017-01-28 18:15:14 +01:00
|
|
|
-- * Arguments
|
|
|
|
|
2019-10-01 06:59:30 +02:00
|
|
|
arguments :: Parser [Argument]
|
2019-06-21 10:44:58 +02:00
|
|
|
arguments = parens $ some argument
|
2015-09-13 13:51:37 +02:00
|
|
|
|
|
|
|
argument :: Parser Argument
|
2019-06-21 10:44:58 +02:00
|
|
|
argument = Argument <$> name <* colon <*> value
|
2015-09-13 13:51:37 +02:00
|
|
|
|
|
|
|
-- * Fragments
|
|
|
|
|
2019-12-25 06:45:29 +01:00
|
|
|
fragmentSpread :: Parser Selection
|
|
|
|
fragmentSpread = FragmentSpread
|
|
|
|
<$ spread
|
|
|
|
<*> fragmentName
|
|
|
|
<*> opt directives
|
|
|
|
|
|
|
|
inlineFragment :: Parser Selection
|
|
|
|
inlineFragment = InlineFragment
|
|
|
|
<$ spread
|
|
|
|
<*> optional typeCondition
|
|
|
|
<*> opt directives
|
|
|
|
<*> selectionSet
|
2015-09-13 13:51:37 +02:00
|
|
|
|
|
|
|
fragmentDefinition :: Parser FragmentDefinition
|
2015-09-14 11:49:20 +02:00
|
|
|
fragmentDefinition = FragmentDefinition
|
2019-06-21 10:44:58 +02:00
|
|
|
<$ symbol "fragment"
|
2017-01-28 18:15:14 +01:00
|
|
|
<*> name
|
|
|
|
<*> typeCondition
|
|
|
|
<*> opt directives
|
|
|
|
<*> selectionSet
|
|
|
|
|
2019-08-29 07:40:50 +02:00
|
|
|
fragmentName :: Parser Name
|
2019-06-21 10:44:58 +02:00
|
|
|
fragmentName = but (symbol "on") *> name
|
2015-09-13 13:51:37 +02:00
|
|
|
|
|
|
|
typeCondition :: Parser TypeCondition
|
2019-06-21 10:44:58 +02:00
|
|
|
typeCondition = symbol "on" *> name
|
2015-09-13 13:51:37 +02:00
|
|
|
|
2017-01-28 18:15:14 +01:00
|
|
|
-- * Input Values
|
2015-09-13 13:51:37 +02:00
|
|
|
|
|
|
|
value :: Parser Value
|
2019-11-03 10:42:10 +01:00
|
|
|
value = Variable <$> variable
|
|
|
|
<|> Float <$> try float
|
|
|
|
<|> Int <$> integer
|
|
|
|
<|> Boolean <$> booleanValue
|
|
|
|
<|> Null <$ symbol "null"
|
|
|
|
<|> String <$> blockString
|
|
|
|
<|> String <$> string
|
|
|
|
<|> Enum <$> try enumValue
|
|
|
|
<|> List <$> listValue
|
|
|
|
<|> Object <$> objectValue
|
2015-09-21 09:28:51 +02:00
|
|
|
<?> "value error!"
|
2017-01-28 18:15:14 +01:00
|
|
|
where
|
|
|
|
booleanValue :: Parser Bool
|
2019-06-21 10:44:58 +02:00
|
|
|
booleanValue = True <$ symbol "true"
|
|
|
|
<|> False <$ symbol "false"
|
2015-09-18 18:11:11 +02:00
|
|
|
|
2017-01-28 18:15:14 +01:00
|
|
|
enumValue :: Parser Name
|
2019-06-21 10:44:58 +02:00
|
|
|
enumValue = but (symbol "true") *> but (symbol "false") *> but (symbol "null") *> name
|
2015-09-18 18:11:11 +02:00
|
|
|
|
2017-01-28 18:15:14 +01:00
|
|
|
listValue :: Parser [Value]
|
2019-06-21 10:44:58 +02:00
|
|
|
listValue = brackets $ some value
|
2015-09-13 13:51:37 +02:00
|
|
|
|
2017-01-28 18:15:14 +01:00
|
|
|
objectValue :: Parser [ObjectField]
|
2019-06-21 10:44:58 +02:00
|
|
|
objectValue = braces $ some objectField
|
2015-09-13 13:51:37 +02:00
|
|
|
|
|
|
|
objectField :: Parser ObjectField
|
2019-06-21 10:44:58 +02:00
|
|
|
objectField = ObjectField <$> name <* symbol ":" <*> value
|
2015-09-13 13:51:37 +02:00
|
|
|
|
2017-01-28 18:15:14 +01:00
|
|
|
-- * Variables
|
2015-09-13 13:51:37 +02:00
|
|
|
|
2019-10-01 06:59:30 +02:00
|
|
|
variableDefinitions :: Parser [VariableDefinition]
|
2019-06-21 10:44:58 +02:00
|
|
|
variableDefinitions = parens $ some variableDefinition
|
2015-09-13 13:51:37 +02:00
|
|
|
|
2017-01-28 18:15:14 +01:00
|
|
|
variableDefinition :: Parser VariableDefinition
|
|
|
|
variableDefinition = VariableDefinition <$> variable
|
2019-06-21 10:44:58 +02:00
|
|
|
<* colon
|
2017-01-28 18:15:14 +01:00
|
|
|
<*> type_
|
|
|
|
<*> optional defaultValue
|
2019-07-18 05:10:02 +02:00
|
|
|
variable :: Parser Name
|
2019-06-21 10:44:58 +02:00
|
|
|
variable = dollar *> name
|
2015-09-13 13:51:37 +02:00
|
|
|
|
2019-07-18 05:10:02 +02:00
|
|
|
defaultValue :: Parser Value
|
2019-06-21 10:44:58 +02:00
|
|
|
defaultValue = equals *> value
|
2017-01-28 18:15:14 +01:00
|
|
|
|
|
|
|
-- * Input Types
|
2015-09-13 13:51:37 +02:00
|
|
|
|
|
|
|
type_ :: Parser Type
|
2019-11-22 08:00:50 +01:00
|
|
|
type_ = try (TypeNonNull <$> nonNullType)
|
|
|
|
<|> TypeList <$> brackets type_
|
|
|
|
<|> TypeNamed <$> name
|
2015-09-21 09:28:51 +02:00
|
|
|
<?> "type_ error!"
|
2015-09-13 13:51:37 +02:00
|
|
|
|
|
|
|
nonNullType :: Parser NonNullType
|
2019-06-21 10:44:58 +02:00
|
|
|
nonNullType = NonNullTypeNamed <$> name <* bang
|
|
|
|
<|> NonNullTypeList <$> brackets type_ <* bang
|
2015-09-21 09:28:51 +02:00
|
|
|
<?> "nonNullType error!"
|
2015-09-13 13:51:37 +02:00
|
|
|
|
2017-01-28 18:15:14 +01:00
|
|
|
-- * Directives
|
|
|
|
|
2019-10-01 06:59:30 +02:00
|
|
|
directives :: Parser [Directive]
|
2019-06-21 10:44:58 +02:00
|
|
|
directives = some directive
|
2017-01-28 18:15:14 +01:00
|
|
|
|
|
|
|
directive :: Parser Directive
|
|
|
|
directive = Directive
|
2019-06-21 10:44:58 +02:00
|
|
|
<$ at
|
2017-01-28 18:15:14 +01:00
|
|
|
<*> name
|
|
|
|
<*> opt arguments
|
|
|
|
|
2015-09-13 13:51:37 +02:00
|
|
|
-- * Internal
|
|
|
|
|
2017-01-28 18:15:14 +01:00
|
|
|
opt :: Monoid a => Parser a -> Parser a
|
|
|
|
opt = option mempty
|
|
|
|
|
|
|
|
-- Hack to reverse parser success
|
|
|
|
but :: Parser a -> Parser ()
|
|
|
|
but pn = False <$ lookAhead pn <|> pure True >>= \case
|
2019-06-21 10:44:58 +02:00
|
|
|
False -> empty
|
|
|
|
True -> pure ()
|
2017-01-28 18:15:14 +01:00
|
|
|
|
|
|
|
manyNE :: Alternative f => f a -> f (NonEmpty a)
|
|
|
|
manyNE p = (:|) <$> p <*> many p
|