diff options
| author | Eugen Wissner <belka@caraus.de> | 2019-12-26 13:00:47 +0100 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2019-12-26 13:07:21 +0100 |
| commit | 56d88310df7c92a1721cc0dfa08a1d232c47c14b (patch) | |
| tree | 15604a675752a64e4a3be68e8848c7133e7ad5c8 /src/Language/GraphQL/AST | |
| parent | e3a495a778e8ccec18e5d5c494ab3b0eed31b13c (diff) | |
| download | graphql-56d88310df7c92a1721cc0dfa08a1d232c47c14b.tar.gz | |
Add definition module
Diffstat (limited to 'src/Language/GraphQL/AST')
| -rw-r--r-- | src/Language/GraphQL/AST/DirectiveLocation.hs | 34 | ||||
| -rw-r--r-- | src/Language/GraphQL/AST/Document.hs | 107 | ||||
| -rw-r--r-- | src/Language/GraphQL/AST/Encoder.hs | 10 | ||||
| -rw-r--r-- | src/Language/GraphQL/AST/Parser.hs | 5 |
4 files changed, 150 insertions, 6 deletions
diff --git a/src/Language/GraphQL/AST/DirectiveLocation.hs b/src/Language/GraphQL/AST/DirectiveLocation.hs new file mode 100644 index 0000000..12c7e18 --- /dev/null +++ b/src/Language/GraphQL/AST/DirectiveLocation.hs @@ -0,0 +1,34 @@ +module Language.GraphQL.AST.DirectiveLocation + ( DirectiveLocation + , ExecutableDirectiveLocation + , TypeSystemDirectiveLocation + ) where + +data DirectiveLocation + = ExecutableDirectiveLocation ExecutableDirectiveLocation + | TypeSystemDirectiveLocation TypeSystemDirectiveLocation + deriving (Eq, Show) + +data ExecutableDirectiveLocation + = Query + | Mutation + | Subscription + | Field + | FragmentDefinition + | FragmentSpread + | InlineFragment + deriving (Eq, Show) + +data TypeSystemDirectiveLocation + = Schema + | Scalar + | Object + | FieldDefinition + | ArgumentDefinition + | Interface + | Union + | Enum + | EnumValue + | InputObject + | InputFieldDefinition + deriving (Eq, Show) diff --git a/src/Language/GraphQL/AST/Document.hs b/src/Language/GraphQL/AST/Document.hs new file mode 100644 index 0000000..0350ce1 --- /dev/null +++ b/src/Language/GraphQL/AST/Document.hs @@ -0,0 +1,107 @@ +-- | This module defines data structures representing a GraphQL document. +module Language.GraphQL.AST.Document + ( Definition(..) + , Document + , ExecutableDefinition(..) + ) where + +import Data.List.NonEmpty (NonEmpty) +import Data.Text (Text) +import Language.GraphQL.AST + ( ExecutableDefinition(..) + , Directive + , Name + , OperationType + , Type + , Value + ) +import Language.GraphQL.AST.DirectiveLocation + +-- | GraphQL document. +type Document = NonEmpty Definition + +type NamedType = Name + +newtype Description = Description (Maybe Text) + deriving (Eq, Show) + +type RootOperationTypeDefinitions = NonEmpty RootOperationTypeDefinition + +data RootOperationTypeDefinition + = RootOperationTypeDefinition OperationType NamedType + deriving (Eq, Show) + +-- | All kinds of definitions that can occur in a GraphQL document. +data Definition + = ExecutableDefinition ExecutableDefinition + | TypeSystemDefinition TypeSystemDefinition + | TypeSystemExtension TypeSystemExtension + deriving (Eq, Show) + +data TypeSystemDefinition + = SchemaDefinition [Directive] RootOperationTypeDefinitions + | TypeDefinition TypeDefinition + | DirectiveDefinition Description Name ArgumentsDefinition DirectiveLocation + deriving (Eq, Show) + +data SchemaExtension + = SchemaOperationExtension [Directive] RootOperationTypeDefinitions + | SchemaDirectiveExtension (NonEmpty Directive) + deriving (Eq, Show) + +data TypeSystemExtension + = SchemaExtension SchemaExtension + | TypeExtension TypeExtension + deriving (Eq, Show) + +newtype ImplementsInterfaces = ImplementsInterfaces (NonEmpty NamedType) + deriving (Eq, Show) +newtype ImplementsInterfacesOpt = ImplementsInterfacesOpt [NamedType] + deriving (Eq, Show) + +newtype UnionMemberTypes = UnionMemberTypes (NonEmpty NamedType) + deriving (Eq, Show) +newtype UnionMemberTypesOpt = UnionMemberTypesOpt [NamedType] + deriving (Eq, Show) + +newtype InputFieldsDefinition = InputFieldsDefinition (NonEmpty InputValueDefinition) + deriving (Eq, Show) +newtype InputFieldsDefinitionOpt = InputFieldsDefinitionOpt [InputValueDefinition] + deriving (Eq, Show) + +data InputValueDefinition + = InputValueDefinition Description Name Type (Maybe Value) [Directive] + deriving (Eq, Show) + +newtype ArgumentsDefinition = ArgumentsDefinition [InputValueDefinition] + deriving (Eq, Show) + +data EnumValueDefinition = EnumValueDefinition Description Name [Directive] + deriving (Eq, Show) + +data FieldDefinition = FieldDefinition Description Name ArgumentsDefinition Type + deriving (Eq, Show) + +data TypeDefinition + = ScalarTypeDefinition Description Name [Directive] + | ObjectTypeDefinition Description Name ImplementsInterfacesOpt [Directive] [FieldDefinition] + | InterfaceTypeDefinition Description Name [Directive] [FieldDefinition] + | UnionTypeDefinition Description Name [Directive] UnionMemberTypesOpt + | EnumTypeDefinition Description Name [Directive] [EnumValueDefinition] + | InputObjectTypeDefinition Description Name [Directive] InputFieldsDefinitionOpt + deriving (Eq, Show) + +data TypeExtension + = ScalarTypeExtension Name (NonEmpty Directive) + | ObjectTypeFieldsDefinitionExtension Name ImplementsInterfacesOpt [Directive] (NonEmpty FieldDefinition) + | ObjectTypeDirectivesExtension Name ImplementsInterfacesOpt (NonEmpty Directive) + | ObjectTypeImplementsInterfacesExtension Name ImplementsInterfaces + | InterfaceTypeFieldsDefinitionExtension Name [Directive] (NonEmpty FieldDefinition) + | InterfaceTypeDirectivesExtension Name (NonEmpty Directive) + | UnionTypeUnionMemberTypesExtension Name [Directive] UnionMemberTypes + | UnionDirectivesExtension Name (NonEmpty Directive) + | EnumTypeEnumValuesDefinitionExtension Name [Directive] (NonEmpty EnumValueDefinition) + | EnumTypeDirectivesExtension Name (NonEmpty Directive) + | InputObjectTypeInputFieldsDefinitionExtension Name [Directive] InputFieldsDefinition + | InputObjectTypeDirectivesExtension Name (NonEmpty Directive) + deriving (Eq, Show) diff --git a/src/Language/GraphQL/AST/Encoder.hs b/src/Language/GraphQL/AST/Encoder.hs index b7378dc..e33068d 100644 --- a/src/Language/GraphQL/AST/Encoder.hs +++ b/src/Language/GraphQL/AST/Encoder.hs @@ -26,6 +26,7 @@ import qualified Data.Text.Lazy.Builder as Builder import Data.Text.Lazy.Builder.Int (decimal, hexadecimal) import Data.Text.Lazy.Builder.RealFloat (realFloat) import qualified Language.GraphQL.AST as Full +import Language.GraphQL.AST.Document -- | Instructs the encoder whether the GraphQL document should be minified or -- pretty printed. @@ -43,17 +44,18 @@ pretty = Pretty 0 minified :: Formatter minified = Minified --- | Converts a 'Full.Document' into a string. -document :: Formatter -> Full.Document -> Lazy.Text +-- | Converts a Document' into a string. +document :: Formatter -> Document -> Lazy.Text document formatter defs | Pretty _ <- formatter = Lazy.Text.intercalate "\n" encodeDocument | Minified <-formatter = Lazy.Text.snoc (mconcat encodeDocument) '\n' where encodeDocument = foldr executableDefinition [] defs - executableDefinition (Full.ExecutableDefinition x) acc = definition formatter x : acc + executableDefinition (ExecutableDefinition x) acc = definition formatter x : acc + executableDefinition _ acc = acc -- | Converts a 'Full.Definition' into a string. -definition :: Formatter -> Full.ExecutableDefinition -> Lazy.Text +definition :: Formatter -> ExecutableDefinition -> Lazy.Text definition formatter x | Pretty _ <- formatter = Lazy.Text.snoc (encodeDefinition x) '\n' | Minified <- formatter = encodeDefinition x diff --git a/src/Language/GraphQL/AST/Parser.hs b/src/Language/GraphQL/AST/Parser.hs index dfe1d4a..ad2b96d 100644 --- a/src/Language/GraphQL/AST/Parser.hs +++ b/src/Language/GraphQL/AST/Parser.hs @@ -9,14 +9,15 @@ module Language.GraphQL.AST.Parser import Control.Applicative (Alternative(..), optional) import Data.List.NonEmpty (NonEmpty(..)) import Language.GraphQL.AST +import qualified Language.GraphQL.AST.Document as Document import Language.GraphQL.AST.Lexer import Text.Megaparsec (lookAhead, option, try, (<?>)) -- | Parser for the GraphQL documents. -document :: Parser Document +document :: Parser Document.Document document = unicodeBOM >> spaceConsumer - >> lexeme (manyNE $ ExecutableDefinition <$> definition) + >> lexeme (manyNE $ Document.ExecutableDefinition <$> definition) definition :: Parser ExecutableDefinition definition = DefinitionOperation <$> operationDefinition |
