aboutsummaryrefslogtreecommitdiff
path: root/src/Language/GraphQL/AST
diff options
context:
space:
mode:
Diffstat (limited to 'src/Language/GraphQL/AST')
-rw-r--r--src/Language/GraphQL/AST/Document.hs14
-rw-r--r--src/Language/GraphQL/AST/Encoder.hs6
-rw-r--r--src/Language/GraphQL/AST/Parser.hs60
3 files changed, 38 insertions, 42 deletions
diff --git a/src/Language/GraphQL/AST/Document.hs b/src/Language/GraphQL/AST/Document.hs
index a78b007..a698d2e 100644
--- a/src/Language/GraphQL/AST/Document.hs
+++ b/src/Language/GraphQL/AST/Document.hs
@@ -226,6 +226,12 @@ type TypeCondition = Name
-- ** Input Values
+-- | Escapes a single character according to the GraphQL escaping rules for
+-- double-quoted string values.
+--
+-- Characters, that should be escaped, are written as escaped characters with a
+-- backslash or Unicode with an \"\\u\". Other characters are returned as
+-- strings.
escape :: Char -> String
escape char'
| char' == '\\' = "\\\\"
@@ -257,7 +263,7 @@ data Value
| Boolean Bool
| Null
| Enum Name
- | List [Value]
+ | List [Node Value]
| Object [ObjectField Value]
deriving Eq
@@ -281,7 +287,7 @@ data ConstValue
| ConstBoolean Bool
| ConstNull
| ConstEnum Name
- | ConstList [ConstValue]
+ | ConstList [Node ConstValue]
| ConstObject [ObjectField ConstValue]
deriving Eq
@@ -318,13 +324,13 @@ instance Functor ObjectField where
-- Each operation can include a list of variables:
--
-- @
--- query (protagonist: String = "Zarathustra") {
+-- query (protagonist: String = \"Zarathustra\") {
-- getAuthor(protagonist: $protagonist)
-- }
-- @
--
-- This query defines an optional variable @protagonist@ of type @String@,
--- its default value is "Zarathustra". If no default value is defined and no
+-- its default value is \"Zarathustra\". If no default value is defined and no
-- value is provided, a variable can still be @null@ if its type is nullable.
--
-- Variables are usually passed along with the query, but not in the query
diff --git a/src/Language/GraphQL/AST/Encoder.hs b/src/Language/GraphQL/AST/Encoder.hs
index f04f385..0d448df 100644
--- a/src/Language/GraphQL/AST/Encoder.hs
+++ b/src/Language/GraphQL/AST/Encoder.hs
@@ -219,7 +219,7 @@ fromConstValue (Full.ConstBoolean x) = Full.Boolean x
fromConstValue Full.ConstNull = Full.Null
fromConstValue (Full.ConstString string) = Full.String string
fromConstValue (Full.ConstEnum x) = Full.Enum x
-fromConstValue (Full.ConstList x) = Full.List $ fromConstValue <$> x
+fromConstValue (Full.ConstList x) = Full.List $ fmap fromConstValue <$> x
fromConstValue (Full.ConstObject x) = Full.Object $ fromConstObjectField <$> x
where
fromConstObjectField Full.ObjectField{value = value', ..} =
@@ -266,8 +266,8 @@ stringValue (Pretty indentation) string =
= Builder.fromLazyText (indent (indentation + 1))
<> line' <> newline <> acc
-listValue :: Formatter -> [Full.Value] -> Lazy.Text
-listValue formatter = bracketsCommas formatter $ value formatter
+listValue :: Formatter -> [Full.Node Full.Value] -> Lazy.Text
+listValue formatter = bracketsCommas formatter $ value formatter . Full.node
objectValue :: Formatter -> [Full.ObjectField Full.Value] -> Lazy.Text
objectValue formatter = intercalate $ objectField formatter
diff --git a/src/Language/GraphQL/AST/Parser.hs b/src/Language/GraphQL/AST/Parser.hs
index 46c8fa3..19251ab 100644
--- a/src/Language/GraphQL/AST/Parser.hs
+++ b/src/Language/GraphQL/AST/Parser.hs
@@ -14,11 +14,7 @@ import qualified Control.Applicative.Combinators.NonEmpty as NonEmpty
import Data.List.NonEmpty (NonEmpty(..))
import Data.Text (Text)
import qualified Language.GraphQL.AST.DirectiveLocation as Directive
-import Language.GraphQL.AST.DirectiveLocation
- ( DirectiveLocation
- , ExecutableDirectiveLocation
- , TypeSystemDirectiveLocation
- )
+import Language.GraphQL.AST.DirectiveLocation (DirectiveLocation)
import qualified Language.GraphQL.AST.Document as Full
import Language.GraphQL.AST.Lexer
import Text.Megaparsec
@@ -96,34 +92,28 @@ directiveLocations = optional pipe
<?> "DirectiveLocations"
directiveLocation :: Parser DirectiveLocation
-directiveLocation
- = Directive.ExecutableDirectiveLocation <$> executableDirectiveLocation
- <|> Directive.TypeSystemDirectiveLocation <$> typeSystemDirectiveLocation
+directiveLocation = e (Directive.Query <$ symbol "QUERY")
+ <|> e (Directive.Mutation <$ symbol "MUTATION")
+ <|> e (Directive.Subscription <$ symbol "SUBSCRIPTION")
+ <|> t (Directive.FieldDefinition <$ symbol "FIELD_DEFINITION")
+ <|> e (Directive.Field <$ symbol "FIELD")
+ <|> e (Directive.FragmentDefinition <$ "FRAGMENT_DEFINITION")
+ <|> e (Directive.FragmentSpread <$ "FRAGMENT_SPREAD")
+ <|> e (Directive.InlineFragment <$ "INLINE_FRAGMENT")
+ <|> t (Directive.Schema <$ symbol "SCHEMA")
+ <|> t (Directive.Scalar <$ symbol "SCALAR")
+ <|> t (Directive.Object <$ symbol "OBJECT")
+ <|> t (Directive.ArgumentDefinition <$ symbol "ARGUMENT_DEFINITION")
+ <|> t (Directive.Interface <$ symbol "INTERFACE")
+ <|> t (Directive.Union <$ symbol "UNION")
+ <|> t (Directive.EnumValue <$ symbol "ENUM_VALUE")
+ <|> t (Directive.Enum <$ symbol "ENUM")
+ <|> t (Directive.InputObject <$ symbol "INPUT_OBJECT")
+ <|> t (Directive.InputFieldDefinition <$ symbol "INPUT_FIELD_DEFINITION")
<?> "DirectiveLocation"
-
-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"
- <?> "ExecutableDirectiveLocation"
-
-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"
- <?> "TypeSystemDirectiveLocation"
+ where
+ e = fmap Directive.ExecutableDirectiveLocation
+ t = fmap Directive.TypeSystemDirectiveLocation
typeDefinition :: Full.Description -> Parser Full.TypeDefinition
typeDefinition description' = scalarTypeDefinition description'
@@ -460,7 +450,7 @@ value = Full.Variable <$> variable
<|> Full.Null <$ nullValue
<|> Full.String <$> stringValue
<|> Full.Enum <$> try enumValue
- <|> Full.List <$> brackets (some value)
+ <|> Full.List <$> brackets (some $ valueNode value)
<|> Full.Object <$> braces (some $ objectField $ valueNode value)
<?> "Value"
@@ -471,8 +461,8 @@ constValue = Full.ConstFloat <$> try float
<|> Full.ConstNull <$ nullValue
<|> Full.ConstString <$> stringValue
<|> Full.ConstEnum <$> try enumValue
- <|> Full.ConstList <$> brackets (some constValue)
- <|> Full.ConstObject <$> braces (some $ objectField $ valueNode constValue)
+ <|> Full.ConstList <$> brackets (many $ valueNode constValue)
+ <|> Full.ConstObject <$> braces (many $ objectField $ valueNode constValue)
<?> "Value"
booleanValue :: Parser Bool