2017-01-28 18:15:14 +01:00
|
|
|
{-# LANGUAGE LambdaCase #-}
|
2019-06-21 10:44:58 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2015-09-13 13:51:37 +02:00
|
|
|
module Data.GraphQL.Parser where
|
|
|
|
|
2019-06-21 10:44:58 +02:00
|
|
|
import Control.Applicative ( Alternative(..)
|
|
|
|
, optional
|
|
|
|
)
|
2015-09-13 13:51:37 +02:00
|
|
|
import Data.GraphQL.AST
|
2019-06-21 10:44:58 +02:00
|
|
|
import Language.GraphQL.Lexer
|
|
|
|
import Data.List.NonEmpty (NonEmpty(..))
|
|
|
|
import Text.Megaparsec ( lookAhead
|
|
|
|
, option
|
|
|
|
, try
|
|
|
|
, (<?>)
|
|
|
|
)
|
2015-09-13 13:51:37 +02:00
|
|
|
|
|
|
|
document :: Parser Document
|
2019-06-21 10:44:58 +02:00
|
|
|
document = spaceConsumer >> lexeme (manyNE definition)
|
2015-09-13 13:51:37 +02:00
|
|
|
|
|
|
|
definition :: Parser Definition
|
|
|
|
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
|
2017-01-28 18:15:14 +01:00
|
|
|
selection = SelectionField <$> field
|
2019-06-21 10:44:58 +02:00
|
|
|
<|> try (SelectionFragmentSpread <$> fragmentSpread)
|
2017-01-28 18:15:14 +01:00
|
|
|
<|> SelectionInlineFragment <$> inlineFragment
|
2015-09-13 13:51:37 +02:00
|
|
|
<?> "selection error!"
|
|
|
|
|
2017-01-28 18:15:14 +01:00
|
|
|
-- * Field
|
|
|
|
|
2015-09-13 13:51:37 +02:00
|
|
|
field :: Parser Field
|
2017-01-28 18:15:14 +01:00
|
|
|
field = Field <$> optional alias
|
2015-09-14 11:49:20 +02:00
|
|
|
<*> name
|
2017-01-28 18:15:14 +01:00
|
|
|
<*> 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
|
|
|
|
|
|
|
|
arguments :: Parser Arguments
|
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
|
|
|
|
|
|
|
|
fragmentSpread :: Parser FragmentSpread
|
2019-06-21 10:44:58 +02:00
|
|
|
fragmentSpread = FragmentSpread <$ spread
|
2017-01-28 18:15:14 +01:00
|
|
|
<*> fragmentName
|
|
|
|
<*> opt directives
|
|
|
|
|
2015-09-13 13:51:37 +02:00
|
|
|
inlineFragment :: Parser InlineFragment
|
2019-06-21 10:44:58 +02:00
|
|
|
inlineFragment = InlineFragment <$ spread
|
2017-01-28 18:15:14 +01:00
|
|
|
<*> 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
|
|
|
|
|
|
|
|
fragmentName :: Parser FragmentName
|
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
|
2015-09-14 14:11:32 +02:00
|
|
|
value = ValueVariable <$> variable
|
2019-06-21 10:44:58 +02:00
|
|
|
<|> ValueFloat <$> try float
|
|
|
|
<|> ValueInt <$> integer
|
2015-09-22 10:39:14 +02:00
|
|
|
<|> ValueBoolean <$> booleanValue
|
2019-06-21 10:44:58 +02:00
|
|
|
<|> ValueNull <$ symbol "null"
|
|
|
|
<|> ValueString <$> string
|
|
|
|
<|> ValueString <$> blockString
|
|
|
|
<|> ValueEnum <$> try enumValue
|
2015-09-18 14:55:59 +02:00
|
|
|
<|> ValueList <$> listValue
|
|
|
|
<|> ValueObject <$> 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
|
|
|
|
2017-01-28 18:15:14 +01:00
|
|
|
variableDefinitions :: Parser VariableDefinitions
|
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
|
|
|
|
variable :: Parser Variable
|
2019-06-21 10:44:58 +02:00
|
|
|
variable = dollar *> name
|
2015-09-13 13:51:37 +02:00
|
|
|
|
2017-01-28 18:15:14 +01:00
|
|
|
defaultValue :: Parser DefaultValue
|
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-06-21 10:44:58 +02:00
|
|
|
type_ = try (TypeNamed <$> name <* but "!")
|
|
|
|
<|> TypeList <$> brackets type_
|
|
|
|
<|> TypeNonNull <$> nonNullType
|
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
|
|
|
|
|
|
|
|
directives :: Parser Directives
|
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
|