Parse SchemaDefinition

This commit is contained in:
Eugen Wissner 2020-01-03 07:20:48 +01:00
parent f4f076fa59
commit d9a2937b55
3 changed files with 44 additions and 15 deletions

View File

@ -5,7 +5,7 @@
module Language.GraphQL.AST.Document module Language.GraphQL.AST.Document
( Alias ( Alias
, Argument(..) , Argument(..)
, Definition(ExecutableDefinition) , Definition(ExecutableDefinition, TypeSystemDefinition)
, Directive(..) , Directive(..)
, Document , Document
, ExecutableDefinition(..) , ExecutableDefinition(..)
@ -15,11 +15,14 @@ module Language.GraphQL.AST.Document
, ObjectField(..) , ObjectField(..)
, OperationDefinition(..) , OperationDefinition(..)
, OperationType(..) , OperationType(..)
, OperationTypeDefinition(..)
, OperationTypeDefinitions
, Selection(..) , Selection(..)
, SelectionSet , SelectionSet
, SelectionSetOpt , SelectionSetOpt
, Type(..) , Type(..)
, TypeCondition , TypeCondition
, TypeSystemDefinition(..)
, Value(..) , Value(..)
, VariableDefinition(..) , VariableDefinition(..)
) where ) where
@ -228,7 +231,7 @@ data Directive = Directive Name [Argument] deriving (Eq, Show)
-- * Type System -- * Type System
data TypeSystemDefinition data TypeSystemDefinition
= SchemaDefinition [Directive] RootOperationTypeDefinitions = SchemaDefinition [Directive] OperationTypeDefinitions
| TypeDefinition TypeDefinition | TypeDefinition TypeDefinition
| DirectiveDefinition | DirectiveDefinition
Description Name ArgumentsDefinition DirectiveLocation Description Name ArgumentsDefinition DirectiveLocation
@ -243,14 +246,14 @@ data TypeSystemExtension
-- ** Schema -- ** Schema
type RootOperationTypeDefinitions = NonEmpty RootOperationTypeDefinition type OperationTypeDefinitions = NonEmpty OperationTypeDefinition
data RootOperationTypeDefinition data OperationTypeDefinition
= RootOperationTypeDefinition OperationType NamedType = OperationTypeDefinition OperationType NamedType
deriving (Eq, Show) deriving (Eq, Show)
data SchemaExtension data SchemaExtension
= SchemaOperationExtension [Directive] RootOperationTypeDefinitions = SchemaOperationExtension [Directive] OperationTypeDefinitions
| SchemaDirectiveExtension (NonEmpty Directive) | SchemaDirectiveExtension (NonEmpty Directive)
deriving (Eq, Show) deriving (Eq, Show)

View File

@ -8,21 +8,44 @@ module Language.GraphQL.AST.Parser
import Control.Applicative (Alternative(..), optional) import Control.Applicative (Alternative(..), optional)
import qualified Control.Applicative.Combinators.NonEmpty as NonEmpty import qualified Control.Applicative.Combinators.NonEmpty as NonEmpty
import Language.GraphQL.AST import Language.GraphQL.AST.Document
import qualified Language.GraphQL.AST.Document as Document
import Language.GraphQL.AST.Lexer import Language.GraphQL.AST.Lexer
import Text.Megaparsec (lookAhead, option, try, (<?>)) import Text.Megaparsec (lookAhead, option, try, (<?>))
-- | Parser for the GraphQL documents. -- | Parser for the GraphQL documents.
document :: Parser Document.Document document :: Parser Document
document = unicodeBOM document = unicodeBOM
>> spaceConsumer >> spaceConsumer
>> lexeme (NonEmpty.some $ Document.ExecutableDefinition <$> definition) >> lexeme (NonEmpty.some definition)
definition :: Parser ExecutableDefinition definition :: Parser Definition
definition = DefinitionOperation <$> operationDefinition definition = ExecutableDefinition <$> executableDefinition
<|> DefinitionFragment <$> fragmentDefinition <|> TypeSystemDefinition <$> typeSystemDefinition
<?> "definition error!" <?> "Definition"
executableDefinition :: Parser ExecutableDefinition
executableDefinition = DefinitionOperation <$> operationDefinition
<|> DefinitionFragment <$> fragmentDefinition
<?> "ExecutableDefinition"
typeSystemDefinition :: Parser TypeSystemDefinition
typeSystemDefinition = schemaDefinition
schemaDefinition :: Parser TypeSystemDefinition
schemaDefinition = SchemaDefinition
<$ symbol "schema"
<*> opt directives
<*> operationTypeDefinitions
<?> "SchemaDefinition"
operationTypeDefinitions :: Parser OperationTypeDefinitions
operationTypeDefinitions = braces $ manyNE operationTypeDefinition
operationTypeDefinition :: Parser OperationTypeDefinition
operationTypeDefinition = OperationTypeDefinition
<$> operationType <* colon
<*> name
<?> "OperationTypeDefinition"
operationDefinition :: Parser OperationDefinition operationDefinition :: Parser OperationDefinition
operationDefinition = SelectionSet <$> selectionSet operationDefinition = SelectionSet <$> selectionSet
@ -39,7 +62,7 @@ operationDefinition = SelectionSet <$> selectionSet
operationType :: Parser OperationType operationType :: Parser OperationType
operationType = Query <$ symbol "query" operationType = Query <$ symbol "query"
<|> Mutation <$ symbol "mutation" <|> Mutation <$ symbol "mutation"
<?> "operationType error" -- <?> Keep default error message
-- * SelectionSet -- * SelectionSet

View File

@ -42,3 +42,6 @@ spec = describe "Parser" $ do
mutation auth{ mutation auth{
test(username: """username""", password: """password""") test(username: """username""", password: """password""")
}|] }|]
it "parses minimal schema definition" $
parse document "" `shouldSucceedOn` [r|schema { query: Query }|]