2017-01-28 18:15:14 +01:00
|
|
|
{-# LANGUAGE LambdaCase #-}
|
2019-06-21 10:44:58 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2020-07-20 21:29:12 +02:00
|
|
|
{-# LANGUAGE RecordWildCards #-}
|
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
|
|
|
|
2020-07-09 08:11:12 +02:00
|
|
|
import Control.Applicative (Alternative(..), liftA2, optional)
|
2020-01-12 07:07:04 +01:00
|
|
|
import Control.Applicative.Combinators (sepBy1)
|
2020-01-12 07:19:28 +01:00
|
|
|
import qualified Control.Applicative.Combinators.NonEmpty as NonEmpty
|
2020-01-28 11:08:28 +01:00
|
|
|
import Data.List.NonEmpty (NonEmpty(..))
|
2020-01-07 13:56:58 +01:00
|
|
|
import Data.Text (Text)
|
2020-01-15 20:20:50 +01:00
|
|
|
import qualified Language.GraphQL.AST.DirectiveLocation as Directive
|
|
|
|
import Language.GraphQL.AST.DirectiveLocation
|
|
|
|
( DirectiveLocation
|
|
|
|
, ExecutableDirectiveLocation
|
|
|
|
, TypeSystemDirectiveLocation
|
|
|
|
)
|
2020-01-03 07:20:48 +01:00
|
|
|
import Language.GraphQL.AST.Document
|
2019-11-03 10:42:10 +01:00
|
|
|
import Language.GraphQL.AST.Lexer
|
2020-07-20 21:29:12 +02:00
|
|
|
import Text.Megaparsec
|
2020-08-25 21:03:42 +02:00
|
|
|
( MonadParsec(..)
|
|
|
|
, SourcePos(..)
|
2020-07-20 21:29:12 +02:00
|
|
|
, getSourcePos
|
|
|
|
, lookAhead
|
|
|
|
, option
|
|
|
|
, try
|
|
|
|
, unPos
|
|
|
|
, (<?>)
|
|
|
|
)
|
2015-09-13 13:51:37 +02:00
|
|
|
|
2019-08-26 10:14:46 +02:00
|
|
|
-- | Parser for the GraphQL documents.
|
2020-01-03 07:20:48 +01:00
|
|
|
document :: Parser Document
|
2019-12-25 06:45:29 +01:00
|
|
|
document = unicodeBOM
|
2020-07-09 08:11:12 +02:00
|
|
|
*> spaceConsumer
|
|
|
|
*> lexeme (NonEmpty.some definition)
|
2015-09-13 13:51:37 +02:00
|
|
|
|
2020-01-03 07:20:48 +01:00
|
|
|
definition :: Parser Definition
|
2020-08-25 21:03:42 +02:00
|
|
|
definition = ExecutableDefinition <$> executableDefinition
|
2020-07-20 21:29:12 +02:00
|
|
|
<|> typeSystemDefinition'
|
|
|
|
<|> typeSystemExtension'
|
2020-01-03 07:20:48 +01:00
|
|
|
<?> "Definition"
|
2020-07-20 21:29:12 +02:00
|
|
|
where
|
|
|
|
typeSystemDefinition' = do
|
|
|
|
location <- getLocation
|
|
|
|
definition' <- typeSystemDefinition
|
|
|
|
pure $ TypeSystemDefinition definition' location
|
|
|
|
typeSystemExtension' = do
|
|
|
|
location <- getLocation
|
|
|
|
definition' <- typeSystemExtension
|
|
|
|
pure $ TypeSystemExtension definition' location
|
|
|
|
|
|
|
|
getLocation :: Parser Location
|
|
|
|
getLocation = fromSourcePosition <$> getSourcePos
|
|
|
|
where
|
|
|
|
fromSourcePosition SourcePos{..} =
|
|
|
|
Location (wordFromPosition sourceLine) (wordFromPosition sourceColumn)
|
|
|
|
wordFromPosition = fromIntegral . unPos
|
2020-01-03 07:20:48 +01:00
|
|
|
|
|
|
|
executableDefinition :: Parser ExecutableDefinition
|
|
|
|
executableDefinition = DefinitionOperation <$> operationDefinition
|
|
|
|
<|> DefinitionFragment <$> fragmentDefinition
|
|
|
|
<?> "ExecutableDefinition"
|
|
|
|
|
|
|
|
typeSystemDefinition :: Parser TypeSystemDefinition
|
|
|
|
typeSystemDefinition = schemaDefinition
|
2020-07-09 08:11:12 +02:00
|
|
|
<|> typeSystemDefinitionWithDescription
|
2020-01-05 07:42:04 +01:00
|
|
|
<?> "TypeSystemDefinition"
|
2020-07-09 08:11:12 +02:00
|
|
|
where
|
|
|
|
typeSystemDefinitionWithDescription = description
|
|
|
|
>>= liftA2 (<|>) typeDefinition' directiveDefinition
|
|
|
|
typeDefinition' description' = TypeDefinition
|
|
|
|
<$> typeDefinition description'
|
2020-01-05 07:42:04 +01:00
|
|
|
|
2020-01-25 16:37:17 +01:00
|
|
|
typeSystemExtension :: Parser TypeSystemExtension
|
|
|
|
typeSystemExtension = SchemaExtension <$> schemaExtension
|
2020-01-26 11:55:15 +01:00
|
|
|
<|> TypeExtension <$> typeExtension
|
2020-01-25 16:37:17 +01:00
|
|
|
<?> "TypeSystemExtension"
|
|
|
|
|
2020-07-09 08:11:12 +02:00
|
|
|
directiveDefinition :: Description -> Parser TypeSystemDefinition
|
|
|
|
directiveDefinition description' = DirectiveDefinition description'
|
|
|
|
<$ symbol "directive"
|
2020-01-15 20:20:50 +01:00
|
|
|
<* at
|
|
|
|
<*> name
|
|
|
|
<*> argumentsDefinition
|
|
|
|
<* symbol "on"
|
|
|
|
<*> directiveLocations
|
2020-01-26 11:55:15 +01:00
|
|
|
<?> "DirectiveDefinition"
|
2020-01-15 20:20:50 +01:00
|
|
|
|
|
|
|
directiveLocations :: Parser (NonEmpty DirectiveLocation)
|
|
|
|
directiveLocations = optional pipe
|
|
|
|
*> directiveLocation `NonEmpty.sepBy1` pipe
|
2020-07-10 08:43:47 +02:00
|
|
|
<?> "DirectiveLocations"
|
2020-01-15 20:20:50 +01:00
|
|
|
|
|
|
|
directiveLocation :: Parser DirectiveLocation
|
|
|
|
directiveLocation
|
|
|
|
= Directive.ExecutableDirectiveLocation <$> executableDirectiveLocation
|
|
|
|
<|> Directive.TypeSystemDirectiveLocation <$> typeSystemDirectiveLocation
|
2020-07-10 08:43:47 +02:00
|
|
|
<?> "DirectiveLocation"
|
2020-01-15 20:20:50 +01:00
|
|
|
|
|
|
|
executableDirectiveLocation :: Parser ExecutableDirectiveLocation
|
|
|
|
executableDirectiveLocation = Directive.Query <$ symbol "QUERY"
|
|
|
|
<|> Directive.Mutation <$ symbol "MUTATION"
|
|
|
|
<|> Directive.Subscription <$ symbol "SUBSCRIPTION"
|
|
|
|
<|> Directive.Field <$ symbol "FIELD"
|
|
|
|
<|> Directive.FragmentDefinition <$ "FRAGMENT_DEFINITION"
|
|
|
|
<|> Directive.FragmentSpread <$ "FRAGMENT_SPREAD"
|
|
|
|
<|> Directive.InlineFragment <$ "INLINE_FRAGMENT"
|
2020-07-10 08:43:47 +02:00
|
|
|
<?> "ExecutableDirectiveLocation"
|
2020-01-15 20:20:50 +01:00
|
|
|
|
|
|
|
typeSystemDirectiveLocation :: Parser TypeSystemDirectiveLocation
|
|
|
|
typeSystemDirectiveLocation = Directive.Schema <$ symbol "SCHEMA"
|
|
|
|
<|> Directive.Scalar <$ symbol "SCALAR"
|
|
|
|
<|> Directive.Object <$ symbol "OBJECT"
|
|
|
|
<|> Directive.FieldDefinition <$ symbol "FIELD_DEFINITION"
|
|
|
|
<|> Directive.ArgumentDefinition <$ symbol "ARGUMENT_DEFINITION"
|
|
|
|
<|> Directive.Interface <$ symbol "INTERFACE"
|
|
|
|
<|> Directive.Union <$ symbol "UNION"
|
|
|
|
<|> Directive.Enum <$ symbol "ENUM"
|
|
|
|
<|> Directive.EnumValue <$ symbol "ENUM_VALUE"
|
|
|
|
<|> Directive.InputObject <$ symbol "INPUT_OBJECT"
|
|
|
|
<|> Directive.InputFieldDefinition <$ symbol "INPUT_FIELD_DEFINITION"
|
2020-07-10 08:43:47 +02:00
|
|
|
<?> "TypeSystemDirectiveLocation"
|
2020-01-15 20:20:50 +01:00
|
|
|
|
2020-07-09 08:11:12 +02:00
|
|
|
typeDefinition :: Description -> Parser TypeDefinition
|
|
|
|
typeDefinition description' = scalarTypeDefinition description'
|
|
|
|
<|> objectTypeDefinition description'
|
|
|
|
<|> interfaceTypeDefinition description'
|
|
|
|
<|> unionTypeDefinition description'
|
|
|
|
<|> enumTypeDefinition description'
|
|
|
|
<|> inputObjectTypeDefinition description'
|
2020-01-05 07:42:04 +01:00
|
|
|
<?> "TypeDefinition"
|
|
|
|
|
2020-01-26 11:55:15 +01:00
|
|
|
typeExtension :: Parser TypeExtension
|
|
|
|
typeExtension = scalarTypeExtension
|
|
|
|
<|> objectTypeExtension
|
|
|
|
<|> interfaceTypeExtension
|
|
|
|
<|> unionTypeExtension
|
|
|
|
<|> enumTypeExtension
|
|
|
|
<|> inputObjectTypeExtension
|
|
|
|
<?> "TypeExtension"
|
|
|
|
|
2020-07-09 08:11:12 +02:00
|
|
|
scalarTypeDefinition :: Description -> Parser TypeDefinition
|
|
|
|
scalarTypeDefinition description' = ScalarTypeDefinition description'
|
|
|
|
<$ symbol "scalar"
|
2020-01-05 07:42:04 +01:00
|
|
|
<*> name
|
2020-01-15 20:20:50 +01:00
|
|
|
<*> directives
|
2020-01-05 07:42:04 +01:00
|
|
|
<?> "ScalarTypeDefinition"
|
|
|
|
|
2020-01-26 11:55:15 +01:00
|
|
|
scalarTypeExtension :: Parser TypeExtension
|
2020-01-28 11:08:28 +01:00
|
|
|
scalarTypeExtension = extend "scalar" "ScalarTypeExtension"
|
|
|
|
$ (ScalarTypeExtension <$> name <*> NonEmpty.some directive) :| []
|
2020-01-26 11:55:15 +01:00
|
|
|
|
2020-07-09 08:11:12 +02:00
|
|
|
objectTypeDefinition :: Description -> Parser TypeDefinition
|
|
|
|
objectTypeDefinition description' = ObjectTypeDefinition description'
|
|
|
|
<$ symbol "type"
|
2020-01-05 07:42:04 +01:00
|
|
|
<*> name
|
2020-01-07 13:56:58 +01:00
|
|
|
<*> option (ImplementsInterfaces []) (implementsInterfaces sepBy1)
|
2020-01-15 20:20:50 +01:00
|
|
|
<*> directives
|
2020-01-05 07:42:04 +01:00
|
|
|
<*> braces (many fieldDefinition)
|
|
|
|
<?> "ObjectTypeDefinition"
|
|
|
|
|
2020-01-26 11:55:15 +01:00
|
|
|
objectTypeExtension :: Parser TypeExtension
|
2020-01-28 11:08:28 +01:00
|
|
|
objectTypeExtension = extend "type" "ObjectTypeExtension"
|
|
|
|
$ fieldsDefinitionExtension :|
|
|
|
|
[ directivesExtension
|
|
|
|
, implementsInterfacesExtension
|
|
|
|
]
|
2020-01-26 11:55:15 +01:00
|
|
|
where
|
|
|
|
fieldsDefinitionExtension = ObjectTypeFieldsDefinitionExtension
|
|
|
|
<$> name
|
|
|
|
<*> option (ImplementsInterfaces []) (implementsInterfaces sepBy1)
|
|
|
|
<*> directives
|
|
|
|
<*> braces (NonEmpty.some fieldDefinition)
|
|
|
|
directivesExtension = ObjectTypeDirectivesExtension
|
|
|
|
<$> name
|
|
|
|
<*> option (ImplementsInterfaces []) (implementsInterfaces sepBy1)
|
|
|
|
<*> NonEmpty.some directive
|
|
|
|
implementsInterfacesExtension = ObjectTypeImplementsInterfacesExtension
|
|
|
|
<$> name
|
|
|
|
<*> implementsInterfaces NonEmpty.sepBy1
|
|
|
|
|
2020-01-05 07:42:04 +01:00
|
|
|
description :: Parser Description
|
|
|
|
description = Description
|
2020-07-10 08:43:47 +02:00
|
|
|
<$> optional stringValue
|
2020-01-05 07:42:04 +01:00
|
|
|
<?> "Description"
|
|
|
|
|
2020-07-09 08:11:12 +02:00
|
|
|
unionTypeDefinition :: Description -> Parser TypeDefinition
|
|
|
|
unionTypeDefinition description' = UnionTypeDefinition description'
|
|
|
|
<$ symbol "union"
|
2020-01-07 13:56:58 +01:00
|
|
|
<*> name
|
2020-01-15 20:20:50 +01:00
|
|
|
<*> directives
|
2020-01-07 13:56:58 +01:00
|
|
|
<*> option (UnionMemberTypes []) (unionMemberTypes sepBy1)
|
|
|
|
<?> "UnionTypeDefinition"
|
|
|
|
|
2020-01-26 11:55:15 +01:00
|
|
|
unionTypeExtension :: Parser TypeExtension
|
2020-01-28 11:08:28 +01:00
|
|
|
unionTypeExtension = extend "union" "UnionTypeExtension"
|
|
|
|
$ unionMemberTypesExtension :| [directivesExtension]
|
2020-01-26 11:55:15 +01:00
|
|
|
where
|
|
|
|
unionMemberTypesExtension = UnionTypeUnionMemberTypesExtension
|
|
|
|
<$> name
|
|
|
|
<*> directives
|
|
|
|
<*> unionMemberTypes NonEmpty.sepBy1
|
|
|
|
directivesExtension = UnionTypeDirectivesExtension
|
|
|
|
<$> name
|
|
|
|
<*> NonEmpty.some directive
|
|
|
|
|
2020-01-07 13:56:58 +01:00
|
|
|
unionMemberTypes ::
|
|
|
|
Foldable t =>
|
|
|
|
(Parser Text -> Parser Text -> Parser (t NamedType)) ->
|
|
|
|
Parser (UnionMemberTypes t)
|
|
|
|
unionMemberTypes sepBy' = UnionMemberTypes
|
|
|
|
<$ equals
|
|
|
|
<* optional pipe
|
|
|
|
<*> name `sepBy'` pipe
|
|
|
|
<?> "UnionMemberTypes"
|
2020-01-05 07:42:04 +01:00
|
|
|
|
2020-07-09 08:11:12 +02:00
|
|
|
interfaceTypeDefinition :: Description -> Parser TypeDefinition
|
|
|
|
interfaceTypeDefinition description' = InterfaceTypeDefinition description'
|
|
|
|
<$ symbol "interface"
|
2020-01-11 08:32:25 +01:00
|
|
|
<*> name
|
2020-01-15 20:20:50 +01:00
|
|
|
<*> directives
|
2020-01-11 08:32:25 +01:00
|
|
|
<*> braces (many fieldDefinition)
|
|
|
|
<?> "InterfaceTypeDefinition"
|
|
|
|
|
2020-01-26 11:55:15 +01:00
|
|
|
interfaceTypeExtension :: Parser TypeExtension
|
2020-01-28 11:08:28 +01:00
|
|
|
interfaceTypeExtension = extend "interface" "InterfaceTypeExtension"
|
|
|
|
$ fieldsDefinitionExtension :| [directivesExtension]
|
2020-01-26 11:55:15 +01:00
|
|
|
where
|
|
|
|
fieldsDefinitionExtension = InterfaceTypeFieldsDefinitionExtension
|
|
|
|
<$> name
|
|
|
|
<*> directives
|
|
|
|
<*> braces (NonEmpty.some fieldDefinition)
|
|
|
|
directivesExtension = InterfaceTypeDirectivesExtension
|
|
|
|
<$> name
|
|
|
|
<*> NonEmpty.some directive
|
|
|
|
|
2020-07-09 08:11:12 +02:00
|
|
|
enumTypeDefinition :: Description -> Parser TypeDefinition
|
|
|
|
enumTypeDefinition description' = EnumTypeDefinition description'
|
|
|
|
<$ symbol "enum"
|
2020-01-12 07:07:04 +01:00
|
|
|
<*> name
|
2020-01-15 20:20:50 +01:00
|
|
|
<*> directives
|
2020-01-26 11:55:15 +01:00
|
|
|
<*> listOptIn braces enumValueDefinition
|
2020-01-12 07:07:04 +01:00
|
|
|
<?> "EnumTypeDefinition"
|
2020-01-26 11:55:15 +01:00
|
|
|
|
|
|
|
enumTypeExtension :: Parser TypeExtension
|
2020-01-28 11:08:28 +01:00
|
|
|
enumTypeExtension = extend "enum" "EnumTypeExtension"
|
|
|
|
$ enumValuesDefinitionExtension :| [directivesExtension]
|
2020-01-12 07:07:04 +01:00
|
|
|
where
|
2020-01-26 11:55:15 +01:00
|
|
|
enumValuesDefinitionExtension = EnumTypeEnumValuesDefinitionExtension
|
|
|
|
<$> name
|
|
|
|
<*> directives
|
|
|
|
<*> braces (NonEmpty.some enumValueDefinition)
|
|
|
|
directivesExtension = EnumTypeDirectivesExtension
|
|
|
|
<$> name
|
|
|
|
<*> NonEmpty.some directive
|
2020-01-12 07:07:04 +01:00
|
|
|
|
2020-07-09 08:11:12 +02:00
|
|
|
inputObjectTypeDefinition :: Description -> Parser TypeDefinition
|
|
|
|
inputObjectTypeDefinition description' = InputObjectTypeDefinition description'
|
|
|
|
<$ symbol "input"
|
2020-01-12 07:07:04 +01:00
|
|
|
<*> name
|
2020-01-15 20:20:50 +01:00
|
|
|
<*> directives
|
2020-01-26 11:55:15 +01:00
|
|
|
<*> listOptIn braces inputValueDefinition
|
2020-01-12 07:07:04 +01:00
|
|
|
<?> "InputObjectTypeDefinition"
|
2020-01-26 11:55:15 +01:00
|
|
|
|
|
|
|
inputObjectTypeExtension :: Parser TypeExtension
|
2020-01-28 11:08:28 +01:00
|
|
|
inputObjectTypeExtension = extend "input" "InputObjectTypeExtension"
|
|
|
|
$ inputFieldsDefinitionExtension :| [directivesExtension]
|
2020-01-12 07:07:04 +01:00
|
|
|
where
|
2020-01-26 11:55:15 +01:00
|
|
|
inputFieldsDefinitionExtension = InputObjectTypeInputFieldsDefinitionExtension
|
|
|
|
<$> name
|
|
|
|
<*> directives
|
|
|
|
<*> braces (NonEmpty.some inputValueDefinition)
|
|
|
|
directivesExtension = InputObjectTypeDirectivesExtension
|
|
|
|
<$> name
|
|
|
|
<*> NonEmpty.some directive
|
2020-01-12 07:07:04 +01:00
|
|
|
|
|
|
|
enumValueDefinition :: Parser EnumValueDefinition
|
|
|
|
enumValueDefinition = EnumValueDefinition
|
|
|
|
<$> description
|
|
|
|
<*> enumValue
|
2020-01-15 20:20:50 +01:00
|
|
|
<*> directives
|
2020-01-12 07:07:04 +01:00
|
|
|
<?> "EnumValueDefinition"
|
|
|
|
|
2020-01-07 13:56:58 +01:00
|
|
|
implementsInterfaces ::
|
|
|
|
Foldable t =>
|
|
|
|
(Parser Text -> Parser Text -> Parser (t NamedType)) ->
|
|
|
|
Parser (ImplementsInterfaces t)
|
|
|
|
implementsInterfaces sepBy' = ImplementsInterfaces
|
2020-01-05 07:42:04 +01:00
|
|
|
<$ symbol "implements"
|
|
|
|
<* optional amp
|
2020-01-07 13:56:58 +01:00
|
|
|
<*> name `sepBy'` amp
|
2020-01-05 07:42:04 +01:00
|
|
|
<?> "ImplementsInterfaces"
|
|
|
|
|
|
|
|
inputValueDefinition :: Parser InputValueDefinition
|
|
|
|
inputValueDefinition = InputValueDefinition
|
|
|
|
<$> description
|
|
|
|
<*> name
|
|
|
|
<* colon
|
|
|
|
<*> type'
|
|
|
|
<*> defaultValue
|
2020-01-15 20:20:50 +01:00
|
|
|
<*> directives
|
2020-01-05 07:42:04 +01:00
|
|
|
<?> "InputValueDefinition"
|
|
|
|
|
|
|
|
argumentsDefinition :: Parser ArgumentsDefinition
|
|
|
|
argumentsDefinition = ArgumentsDefinition
|
2020-01-15 20:20:50 +01:00
|
|
|
<$> listOptIn parens inputValueDefinition
|
2020-01-05 07:42:04 +01:00
|
|
|
<?> "ArgumentsDefinition"
|
|
|
|
|
|
|
|
fieldDefinition :: Parser FieldDefinition
|
|
|
|
fieldDefinition = FieldDefinition
|
|
|
|
<$> description
|
|
|
|
<*> name
|
2020-01-15 20:20:50 +01:00
|
|
|
<*> argumentsDefinition
|
2020-01-05 07:42:04 +01:00
|
|
|
<* colon
|
|
|
|
<*> type'
|
2020-01-15 20:20:50 +01:00
|
|
|
<*> directives
|
2020-01-05 07:42:04 +01:00
|
|
|
<?> "FieldDefinition"
|
2020-01-03 07:20:48 +01:00
|
|
|
|
|
|
|
schemaDefinition :: Parser TypeSystemDefinition
|
|
|
|
schemaDefinition = SchemaDefinition
|
|
|
|
<$ symbol "schema"
|
2020-01-15 20:20:50 +01:00
|
|
|
<*> directives
|
2020-01-03 07:20:48 +01:00
|
|
|
<*> operationTypeDefinitions
|
|
|
|
<?> "SchemaDefinition"
|
2020-01-25 16:37:17 +01:00
|
|
|
|
|
|
|
operationTypeDefinitions :: Parser (NonEmpty OperationTypeDefinition)
|
|
|
|
operationTypeDefinitions = braces $ NonEmpty.some operationTypeDefinition
|
|
|
|
|
|
|
|
schemaExtension :: Parser SchemaExtension
|
2020-01-28 11:08:28 +01:00
|
|
|
schemaExtension = extend "schema" "SchemaExtension"
|
|
|
|
$ schemaOperationExtension :| [directivesExtension]
|
2020-01-12 07:07:04 +01:00
|
|
|
where
|
2020-01-28 11:08:28 +01:00
|
|
|
directivesExtension = SchemaDirectivesExtension
|
|
|
|
<$> NonEmpty.some directive
|
2020-01-25 16:37:17 +01:00
|
|
|
schemaOperationExtension = SchemaOperationExtension
|
|
|
|
<$> directives
|
|
|
|
<*> operationTypeDefinitions
|
2020-01-03 07:20:48 +01:00
|
|
|
|
|
|
|
operationTypeDefinition :: Parser OperationTypeDefinition
|
|
|
|
operationTypeDefinition = OperationTypeDefinition
|
|
|
|
<$> operationType <* colon
|
|
|
|
<*> name
|
|
|
|
<?> "OperationTypeDefinition"
|
2015-09-13 13:51:37 +02:00
|
|
|
|
|
|
|
operationDefinition :: Parser OperationDefinition
|
2020-08-25 21:03:42 +02:00
|
|
|
operationDefinition = shorthand
|
2019-12-28 07:07:58 +01:00
|
|
|
<|> operationDefinition'
|
2020-07-10 08:43:47 +02:00
|
|
|
<?> "OperationDefinition"
|
2019-12-28 07:07:58 +01:00
|
|
|
where
|
2020-08-25 21:03:42 +02:00
|
|
|
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
|
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"
|
2020-07-11 06:34:10 +02:00
|
|
|
<|> Subscription <$ symbol "subscription"
|
|
|
|
<?> "OperationType"
|
2015-09-13 13:51:37 +02:00
|
|
|
|
|
|
|
selectionSet :: Parser SelectionSet
|
2020-07-10 08:43:47 +02:00
|
|
|
selectionSet = braces (NonEmpty.some selection) <?> "SelectionSet"
|
2017-01-28 18:15:14 +01:00
|
|
|
|
|
|
|
selectionSetOpt :: Parser SelectionSetOpt
|
2020-07-10 08:43:47 +02:00
|
|
|
selectionSetOpt = listOptIn braces selection <?> "SelectionSet"
|
2015-09-13 13:51:37 +02:00
|
|
|
|
|
|
|
selection :: Parser Selection
|
2020-09-09 17:04:31 +02:00
|
|
|
selection = FieldSelection <$> field
|
2020-09-07 22:01:49 +02:00
|
|
|
<|> FragmentSpreadSelection <$> try fragmentSpread
|
|
|
|
<|> InlineFragmentSelection <$> inlineFragment
|
2020-07-10 08:43:47 +02:00
|
|
|
<?> "Selection"
|
2017-01-28 18:15:14 +01:00
|
|
|
|
2020-09-09 17:04:31 +02:00
|
|
|
field :: Parser Field
|
2020-09-05 10:00:58 +02:00
|
|
|
field = label "Field" $ do
|
|
|
|
location <- getLocation
|
|
|
|
alias' <- optional alias
|
|
|
|
name' <- name
|
|
|
|
arguments' <- arguments
|
|
|
|
directives' <- directives
|
|
|
|
selectionSetOpt' <- selectionSetOpt
|
|
|
|
pure $ Field alias' name' arguments' directives' selectionSetOpt' location
|
2015-09-13 13:51:37 +02:00
|
|
|
|
2020-09-09 17:04:31 +02:00
|
|
|
alias :: Parser Name
|
2020-07-10 08:43:47 +02:00
|
|
|
alias = try (name <* colon) <?> "Alias"
|
2017-01-28 18:15:14 +01:00
|
|
|
|
2019-10-01 06:59:30 +02:00
|
|
|
arguments :: Parser [Argument]
|
2020-07-10 08:43:47 +02:00
|
|
|
arguments = listOptIn parens argument <?> "Arguments"
|
2015-09-13 13:51:37 +02:00
|
|
|
|
|
|
|
argument :: Parser Argument
|
2020-09-14 07:49:33 +02:00
|
|
|
argument = label "Argument" $ do
|
|
|
|
location <- getLocation
|
|
|
|
name' <- name
|
|
|
|
colon
|
2020-09-21 07:28:40 +02:00
|
|
|
value' <- valueNode
|
2020-09-14 07:49:33 +02:00
|
|
|
pure $ Argument name' value' location
|
2015-09-13 13:51:37 +02:00
|
|
|
|
2020-09-07 22:01:49 +02:00
|
|
|
fragmentSpread :: Parser FragmentSpread
|
2020-08-31 11:06:27 +02:00
|
|
|
fragmentSpread = label "FragmentSpread" $ do
|
|
|
|
location <- getLocation
|
|
|
|
_ <- spread
|
|
|
|
fragmentName' <- fragmentName
|
|
|
|
directives' <- directives
|
|
|
|
pure $ FragmentSpread fragmentName' directives' location
|
2019-12-25 06:45:29 +01:00
|
|
|
|
2020-09-07 22:01:49 +02:00
|
|
|
inlineFragment :: Parser InlineFragment
|
2020-09-05 10:00:58 +02:00
|
|
|
inlineFragment = label "InlineFragment" $ do
|
|
|
|
location <- getLocation
|
|
|
|
_ <- spread
|
|
|
|
typeCondition' <- optional typeCondition
|
|
|
|
directives' <- directives
|
|
|
|
selectionSet' <- selectionSet
|
|
|
|
pure $ InlineFragment typeCondition' directives' selectionSet' location
|
2015-09-13 13:51:37 +02:00
|
|
|
|
|
|
|
fragmentDefinition :: Parser FragmentDefinition
|
2020-08-25 21:03:42 +02:00
|
|
|
fragmentDefinition = label "FragmentDefinition" $ do
|
|
|
|
location <- getLocation
|
|
|
|
_ <- symbol "fragment"
|
|
|
|
fragmentName' <- name
|
|
|
|
typeCondition' <- typeCondition
|
|
|
|
directives' <- directives
|
|
|
|
selectionSet' <- selectionSet
|
|
|
|
pure $ FragmentDefinition
|
|
|
|
fragmentName' typeCondition' directives' selectionSet' location
|
2017-01-28 18:15:14 +01:00
|
|
|
|
2019-08-29 07:40:50 +02:00
|
|
|
fragmentName :: Parser Name
|
2020-07-10 08:43:47 +02:00
|
|
|
fragmentName = but (symbol "on") *> name <?> "FragmentName"
|
2015-09-13 13:51:37 +02:00
|
|
|
|
|
|
|
typeCondition :: Parser TypeCondition
|
2020-07-10 08:43:47 +02:00
|
|
|
typeCondition = symbol "on" *> name <?> "TypeCondition"
|
2015-09-13 13:51:37 +02:00
|
|
|
|
2020-09-21 07:28:40 +02:00
|
|
|
valueNode :: Parser (Node Value)
|
|
|
|
valueNode = do
|
|
|
|
location <- getLocation
|
|
|
|
value' <- value
|
|
|
|
pure $ Node value' location
|
|
|
|
|
2015-09-13 13:51:37 +02:00
|
|
|
value :: Parser Value
|
2019-11-03 10:42:10 +01:00
|
|
|
value = Variable <$> variable
|
2020-05-22 10:11:48 +02:00
|
|
|
<|> Float <$> try float
|
|
|
|
<|> Int <$> integer
|
|
|
|
<|> Boolean <$> booleanValue
|
2020-07-10 08:43:47 +02:00
|
|
|
<|> Null <$ nullValue
|
|
|
|
<|> String <$> stringValue
|
2020-05-22 10:11:48 +02:00
|
|
|
<|> Enum <$> try enumValue
|
|
|
|
<|> List <$> brackets (some value)
|
|
|
|
<|> Object <$> braces (some $ objectField value)
|
2020-07-10 08:43:47 +02:00
|
|
|
<?> "Value"
|
2015-09-18 18:11:11 +02:00
|
|
|
|
2020-05-22 10:11:48 +02:00
|
|
|
constValue :: Parser ConstValue
|
|
|
|
constValue = ConstFloat <$> try float
|
|
|
|
<|> ConstInt <$> integer
|
|
|
|
<|> ConstBoolean <$> booleanValue
|
2020-07-10 08:43:47 +02:00
|
|
|
<|> ConstNull <$ nullValue
|
|
|
|
<|> ConstString <$> stringValue
|
2020-05-22 10:11:48 +02:00
|
|
|
<|> ConstEnum <$> try enumValue
|
|
|
|
<|> ConstList <$> brackets (some constValue)
|
|
|
|
<|> ConstObject <$> braces (some $ objectField constValue)
|
2020-07-10 08:43:47 +02:00
|
|
|
<?> "Value"
|
2015-09-13 13:51:37 +02:00
|
|
|
|
2020-05-22 10:11:48 +02:00
|
|
|
booleanValue :: Parser Bool
|
|
|
|
booleanValue = True <$ symbol "true"
|
|
|
|
<|> False <$ symbol "false"
|
2020-07-10 08:43:47 +02:00
|
|
|
<?> "BooleanValue"
|
2015-09-13 13:51:37 +02:00
|
|
|
|
2020-01-12 07:07:04 +01:00
|
|
|
enumValue :: Parser Name
|
2020-07-10 08:43:47 +02:00
|
|
|
enumValue = but (symbol "true")
|
|
|
|
*> but (symbol "false")
|
|
|
|
*> but (symbol "null")
|
|
|
|
*> name
|
|
|
|
<?> "EnumValue"
|
2020-01-12 07:07:04 +01:00
|
|
|
|
2020-07-10 08:43:47 +02:00
|
|
|
stringValue :: Parser Text
|
|
|
|
stringValue = blockString <|> string <?> "StringValue"
|
|
|
|
|
|
|
|
nullValue :: Parser Text
|
|
|
|
nullValue = symbol "null" <?> "NullValue"
|
2015-09-13 13:51:37 +02:00
|
|
|
|
2020-07-10 08:43:47 +02:00
|
|
|
objectField :: Parser a -> Parser (ObjectField a)
|
2020-09-24 05:47:31 +02:00
|
|
|
objectField valueParser = label "ObjectField" $ do
|
|
|
|
location <- getLocation
|
|
|
|
fieldName <- name
|
|
|
|
colon
|
|
|
|
fieldValue <- valueParser
|
|
|
|
pure $ ObjectField fieldName fieldValue location
|
2015-09-13 13:51:37 +02:00
|
|
|
|
2019-10-01 06:59:30 +02:00
|
|
|
variableDefinitions :: Parser [VariableDefinition]
|
2020-01-13 08:11:22 +01:00
|
|
|
variableDefinitions = listOptIn parens variableDefinition
|
2020-07-10 08:43:47 +02:00
|
|
|
<?> "VariableDefinitions"
|
2015-09-13 13:51:37 +02:00
|
|
|
|
2017-01-28 18:15:14 +01:00
|
|
|
variableDefinition :: Parser VariableDefinition
|
2020-09-19 18:18:26 +02:00
|
|
|
variableDefinition = label "VariableDefinition" $ do
|
|
|
|
location <- getLocation
|
|
|
|
variableName <- variable
|
|
|
|
colon
|
|
|
|
variableType <- type'
|
|
|
|
variableValue <- defaultValue
|
|
|
|
pure $ VariableDefinition variableName variableType variableValue location
|
2020-01-13 08:11:22 +01:00
|
|
|
|
2019-07-18 05:10:02 +02:00
|
|
|
variable :: Parser Name
|
2020-07-10 08:43:47 +02:00
|
|
|
variable = dollar *> name <?> "Variable"
|
2015-09-13 13:51:37 +02:00
|
|
|
|
2020-05-22 10:11:48 +02:00
|
|
|
defaultValue :: Parser (Maybe ConstValue)
|
|
|
|
defaultValue = optional (equals *> constValue) <?> "DefaultValue"
|
2017-01-28 18:15:14 +01:00
|
|
|
|
2020-01-05 07:42:04 +01:00
|
|
|
type' :: Parser Type
|
|
|
|
type' = try (TypeNonNull <$> nonNullType)
|
|
|
|
<|> TypeList <$> brackets type'
|
2019-11-22 08:00:50 +01:00
|
|
|
<|> TypeNamed <$> name
|
2020-01-05 07:42:04 +01:00
|
|
|
<?> "Type"
|
2015-09-13 13:51:37 +02:00
|
|
|
|
|
|
|
nonNullType :: Parser NonNullType
|
2019-06-21 10:44:58 +02:00
|
|
|
nonNullType = NonNullTypeNamed <$> name <* bang
|
2020-07-10 08:43:47 +02:00
|
|
|
<|> NonNullTypeList <$> brackets type' <* bang
|
|
|
|
<?> "NonNullType"
|
2017-01-28 18:15:14 +01:00
|
|
|
|
2019-10-01 06:59:30 +02:00
|
|
|
directives :: Parser [Directive]
|
2020-07-10 08:43:47 +02:00
|
|
|
directives = many directive <?> "Directives"
|
2017-01-28 18:15:14 +01:00
|
|
|
|
|
|
|
directive :: Parser Directive
|
2020-09-18 07:32:58 +02:00
|
|
|
directive = label "Directive" $ do
|
|
|
|
location <- getLocation
|
|
|
|
at
|
|
|
|
directiveName <- name
|
|
|
|
directiveArguments <- arguments
|
|
|
|
pure $ Directive directiveName directiveArguments location
|
2015-09-13 13:51:37 +02:00
|
|
|
|
2020-01-13 08:11:22 +01:00
|
|
|
listOptIn :: (Parser [a] -> Parser [a]) -> Parser a -> Parser [a]
|
|
|
|
listOptIn surround = option [] . surround . some
|
2017-01-28 18:15:14 +01:00
|
|
|
|
|
|
|
-- 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 ()
|