2015-09-13 13:51:37 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2017-01-28 18:15:14 +01:00
|
|
|
{-# LANGUAGE LambdaCase #-}
|
2016-03-15 14:02:34 +01:00
|
|
|
-- | This module defines a parser for @GraphQL@ request documents.
|
2015-09-13 13:51:37 +02:00
|
|
|
module Data.GraphQL.Parser where
|
|
|
|
|
|
|
|
import Prelude hiding (takeWhile)
|
2015-09-16 10:36:44 +02:00
|
|
|
|
2017-01-28 18:15:14 +01:00
|
|
|
import Control.Applicative ((<|>), Alternative, empty, many, optional)
|
2015-09-14 15:43:09 +02:00
|
|
|
import Control.Monad (when)
|
2015-09-22 14:02:12 +02:00
|
|
|
import Data.Char (isDigit, isSpace)
|
2015-09-18 16:29:21 +02:00
|
|
|
import Data.Foldable (traverse_)
|
2017-01-28 18:15:14 +01:00
|
|
|
import Data.Monoid ((<>))
|
|
|
|
import Data.List.NonEmpty (NonEmpty((:|)))
|
2016-12-09 00:34:46 +01:00
|
|
|
import Data.Scientific (floatingOrInteger)
|
2015-09-18 16:29:21 +02:00
|
|
|
|
2015-09-18 15:43:22 +02:00
|
|
|
import Data.Text (Text, append)
|
2017-01-28 18:15:14 +01:00
|
|
|
import Data.Attoparsec.Combinator (lookAhead)
|
2015-09-13 15:32:16 +02:00
|
|
|
import Data.Attoparsec.Text
|
2015-09-13 13:51:37 +02:00
|
|
|
( Parser
|
|
|
|
, (<?>)
|
|
|
|
, anyChar
|
2015-09-13 15:32:16 +02:00
|
|
|
, endOfLine
|
2015-09-18 15:43:22 +02:00
|
|
|
, inClass
|
2015-09-13 13:51:37 +02:00
|
|
|
, many1
|
|
|
|
, manyTill
|
2015-09-14 11:49:20 +02:00
|
|
|
, option
|
2015-09-14 15:43:09 +02:00
|
|
|
, peekChar
|
2017-01-28 18:15:14 +01:00
|
|
|
, scientific
|
2015-09-18 15:43:22 +02:00
|
|
|
, takeWhile
|
|
|
|
, takeWhile1
|
2015-09-13 13:51:37 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
import Data.GraphQL.AST
|
|
|
|
|
|
|
|
-- * Name
|
|
|
|
|
|
|
|
name :: Parser Name
|
2015-09-18 15:43:22 +02:00
|
|
|
name = tok $ append <$> takeWhile1 isA_z
|
|
|
|
<*> takeWhile ((||) <$> isDigit <*> isA_z)
|
|
|
|
where
|
|
|
|
-- `isAlpha` handles many more Unicode Chars
|
2017-01-28 18:15:14 +01:00
|
|
|
isA_z = inClass $ '_' : ['A'..'Z'] <> ['a'..'z']
|
2015-09-18 15:43:22 +02:00
|
|
|
|
2015-09-13 13:51:37 +02:00
|
|
|
-- * Document
|
|
|
|
|
|
|
|
document :: Parser Document
|
2017-01-28 18:15:14 +01:00
|
|
|
document = whiteSpace *> 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
|
|
|
|
operationType = Query <$ tok "query"
|
|
|
|
<|> Mutation <$ tok "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
|
|
|
|
selectionSetOpt = braces $ many1 selection
|
2015-09-13 13:51:37 +02:00
|
|
|
|
|
|
|
selection :: Parser Selection
|
2017-01-28 18:15:14 +01:00
|
|
|
selection = SelectionField <$> field
|
2015-09-13 13:51:37 +02:00
|
|
|
<|> 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
|
2015-09-14 11:49:20 +02:00
|
|
|
alias = name <* tok ":"
|
2015-09-13 13:51:37 +02:00
|
|
|
|
2017-01-28 18:15:14 +01:00
|
|
|
-- * Arguments
|
|
|
|
|
|
|
|
arguments :: Parser Arguments
|
2015-09-14 11:49:20 +02:00
|
|
|
arguments = parens $ many1 argument
|
2015-09-13 13:51:37 +02:00
|
|
|
|
|
|
|
argument :: Parser Argument
|
2015-09-14 11:49:20 +02:00
|
|
|
argument = Argument <$> name <* tok ":" <*> value
|
2015-09-13 13:51:37 +02:00
|
|
|
|
|
|
|
-- * Fragments
|
|
|
|
|
|
|
|
fragmentSpread :: Parser FragmentSpread
|
2017-01-28 18:15:14 +01:00
|
|
|
fragmentSpread = FragmentSpread <$ tok "..."
|
|
|
|
<*> fragmentName
|
|
|
|
<*> opt directives
|
|
|
|
|
2015-09-13 13:51:37 +02:00
|
|
|
inlineFragment :: Parser InlineFragment
|
2017-01-28 18:15:14 +01:00
|
|
|
inlineFragment = InlineFragment <$ tok "..."
|
|
|
|
<*> 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
|
2017-01-28 18:15:14 +01:00
|
|
|
<$ tok "fragment"
|
|
|
|
<*> name
|
|
|
|
<*> typeCondition
|
|
|
|
<*> opt directives
|
|
|
|
<*> selectionSet
|
|
|
|
|
|
|
|
fragmentName :: Parser FragmentName
|
|
|
|
fragmentName = but (tok "on") *> name
|
2015-09-13 13:51:37 +02:00
|
|
|
|
|
|
|
typeCondition :: Parser TypeCondition
|
2017-01-28 18:15:14 +01:00
|
|
|
typeCondition = tok "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
|
2016-12-18 16:19:59 +01:00
|
|
|
<|> tok (either ValueFloat ValueInt . floatingOrInteger <$> scientific)
|
2015-09-22 10:39:14 +02:00
|
|
|
<|> ValueBoolean <$> booleanValue
|
2017-01-28 18:15:14 +01:00
|
|
|
<|> ValueNull <$ tok "null"
|
2015-09-18 18:11:11 +02:00
|
|
|
<|> ValueString <$> stringValue
|
2017-01-28 18:15:14 +01:00
|
|
|
<|> ValueEnum <$> 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
|
|
|
|
booleanValue = True <$ tok "true"
|
|
|
|
<|> False <$ tok "false"
|
2015-09-13 13:51:37 +02:00
|
|
|
|
2017-01-28 18:15:14 +01:00
|
|
|
-- TODO: Escape characters. Look at `jsstring_` in aeson package.
|
|
|
|
stringValue :: Parser Text
|
|
|
|
stringValue = quotes (takeWhile (/= '"'))
|
2015-09-18 18:11:11 +02:00
|
|
|
|
2017-01-28 18:15:14 +01:00
|
|
|
enumValue :: Parser Name
|
|
|
|
enumValue = but (tok "true") *> but (tok "false") *> but (tok "null") *> name
|
2015-09-18 18:11:11 +02:00
|
|
|
|
2017-01-28 18:15:14 +01:00
|
|
|
listValue :: Parser [Value]
|
|
|
|
listValue = brackets $ many1 value
|
2015-09-13 13:51:37 +02:00
|
|
|
|
2017-01-28 18:15:14 +01:00
|
|
|
objectValue :: Parser [ObjectField]
|
|
|
|
objectValue = braces $ many1 objectField
|
2015-09-13 13:51:37 +02:00
|
|
|
|
|
|
|
objectField :: Parser ObjectField
|
2015-09-14 11:49:20 +02:00
|
|
|
objectField = ObjectField <$> name <* tok ":" <*> 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
|
|
|
|
variableDefinitions = parens $ many1 variableDefinition
|
2015-09-13 13:51:37 +02:00
|
|
|
|
2017-01-28 18:15:14 +01:00
|
|
|
variableDefinition :: Parser VariableDefinition
|
|
|
|
variableDefinition = VariableDefinition <$> variable
|
|
|
|
<* tok ":"
|
|
|
|
<*> type_
|
|
|
|
<*> optional defaultValue
|
|
|
|
|
|
|
|
variable :: Parser Variable
|
|
|
|
variable = tok "$" *> name
|
2015-09-13 13:51:37 +02:00
|
|
|
|
2017-01-28 18:15:14 +01:00
|
|
|
defaultValue :: Parser DefaultValue
|
|
|
|
defaultValue = tok "=" *> value
|
|
|
|
|
|
|
|
-- * Input Types
|
2015-09-13 13:51:37 +02:00
|
|
|
|
|
|
|
type_ :: Parser Type
|
2017-01-28 18:15:14 +01:00
|
|
|
type_ = TypeNamed <$> name
|
|
|
|
<|> TypeList <$> brackets type_
|
2015-09-13 13:51:37 +02:00
|
|
|
<|> TypeNonNull <$> nonNullType
|
2015-09-21 09:28:51 +02:00
|
|
|
<?> "type_ error!"
|
2015-09-13 13:51:37 +02:00
|
|
|
|
|
|
|
nonNullType :: Parser NonNullType
|
2017-01-28 18:15:14 +01:00
|
|
|
nonNullType = NonNullTypeNamed <$> name <* tok "!"
|
|
|
|
<|> NonNullTypeList <$> brackets type_ <* tok "!"
|
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
|
|
|
|
directives = many1 directive
|
|
|
|
|
|
|
|
directive :: Parser Directive
|
|
|
|
directive = Directive
|
|
|
|
<$ tok "@"
|
|
|
|
<*> name
|
|
|
|
<*> opt arguments
|
|
|
|
|
2015-09-13 13:51:37 +02:00
|
|
|
-- * Internal
|
|
|
|
|
2015-09-14 11:49:20 +02:00
|
|
|
tok :: Parser a -> Parser a
|
|
|
|
tok p = p <* whiteSpace
|
|
|
|
|
|
|
|
parens :: Parser a -> Parser a
|
|
|
|
parens = between "(" ")"
|
2015-09-13 13:51:37 +02:00
|
|
|
|
2015-09-14 11:49:20 +02:00
|
|
|
braces :: Parser a -> Parser a
|
|
|
|
braces = between "{" "}"
|
2015-09-13 13:51:37 +02:00
|
|
|
|
2015-09-14 14:11:32 +02:00
|
|
|
quotes :: Parser a -> Parser a
|
|
|
|
quotes = between "\"" "\""
|
|
|
|
|
2015-09-14 11:49:20 +02:00
|
|
|
brackets :: Parser a -> Parser a
|
|
|
|
brackets = between "[" "]"
|
2015-09-13 13:51:37 +02:00
|
|
|
|
2015-09-14 11:49:20 +02:00
|
|
|
between :: Parser Text -> Parser Text -> Parser a -> Parser a
|
|
|
|
between open close p = tok open *> p <* tok close
|
2015-09-13 13:51:37 +02:00
|
|
|
|
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
|
|
|
|
False -> empty
|
|
|
|
True -> pure ()
|
|
|
|
|
|
|
|
manyNE :: Alternative f => f a -> f (NonEmpty a)
|
|
|
|
manyNE p = (:|) <$> p <*> many p
|
2015-09-13 13:51:37 +02:00
|
|
|
|
2015-09-14 11:49:20 +02:00
|
|
|
whiteSpace :: Parser ()
|
2015-09-18 16:29:21 +02:00
|
|
|
whiteSpace = peekChar >>= traverse_ (\c ->
|
|
|
|
if isSpace c || c == ','
|
|
|
|
then anyChar *> whiteSpace
|
|
|
|
else when (c == '#') $ manyTill anyChar endOfLine *> whiteSpace)
|