summaryrefslogtreecommitdiff
path: root/src/Language/GraphQL/AST
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2020-09-09 17:04:31 +0200
committerEugen Wissner <belka@caraus.de>2020-09-09 17:04:31 +0200
commitc2c57b636392ae67a118ce5be04ad8f4b1304ed5 (patch)
tree317992e1bcca871e7b31dd8d131a67cba6d98152 /src/Language/GraphQL/AST
parentf6ff0ab9c785273e3ceeac6b9d636c5ec519a008 (diff)
downloadgraphql-c2c57b636392ae67a118ce5be04ad8f4b1304ed5.tar.gz
Validate all fragments are used
Diffstat (limited to 'src/Language/GraphQL/AST')
-rw-r--r--src/Language/GraphQL/AST/Document.hs37
-rw-r--r--src/Language/GraphQL/AST/Encoder.hs16
-rw-r--r--src/Language/GraphQL/AST/Parser.hs6
3 files changed, 20 insertions, 39 deletions
diff --git a/src/Language/GraphQL/AST/Document.hs b/src/Language/GraphQL/AST/Document.hs
index 5cfadc5..cc657f4 100644
--- a/src/Language/GraphQL/AST/Document.hs
+++ b/src/Language/GraphQL/AST/Document.hs
@@ -5,8 +5,7 @@
-- <https://facebook.github.io/graphql/ Facebook's GraphQL Specification>.
-- for more information.
module Language.GraphQL.AST.Document
- ( Alias
- , Argument(..)
+ ( Argument(..)
, ArgumentsDefinition(..)
, ConstValue(..)
, Definition(..)
@@ -15,6 +14,7 @@ module Language.GraphQL.AST.Document
, Document
, EnumValueDefinition(..)
, ExecutableDefinition(..)
+ , Field(..)
, FieldDefinition(..)
, FragmentDefinition(..)
, FragmentSpread(..)
@@ -118,9 +118,14 @@ type SelectionSet = NonEmpty Selection
-- | Field selection.
type SelectionSetOpt = [Selection]
--- | Selection is a single entry in a selection set. It can be a single field,
--- fragment spread or inline fragment.
---
+-- | Selection is a single entry in a selection set. It can be a single 'Field',
+-- 'FragmentSpread' or an 'InlineFragment'.
+data Selection
+ = FieldSelection Field
+ | FragmentSpreadSelection FragmentSpread
+ | InlineFragmentSelection InlineFragment
+ deriving (Eq, Show)
+
-- The only required property of a field is its name. Optionally it can also
-- have an alias, arguments, directives and a list of subfields.
--
@@ -134,10 +139,8 @@ type SelectionSetOpt = [Selection]
-- }
-- }
-- @
-data Selection
- = Field (Maybe Alias) Name [Argument] [Directive] SelectionSetOpt Location
- | FragmentSpreadSelection FragmentSpread
- | InlineFragmentSelection InlineFragment
+data Field =
+ Field (Maybe Name) Name [Argument] [Directive] SelectionSetOpt Location
deriving (Eq, Show)
-- Inline fragments don't have any name and the type condition ("on UserType")
@@ -189,22 +192,6 @@ data FragmentSpread = FragmentSpread Name [Directive] Location
-- Here "id" is an argument for the field "user" and its value is 4.
data Argument = Argument Name Value deriving (Eq,Show)
--- ** Field Alias
-
--- | Alternative field name.
---
--- @
--- {
--- smallPic: profilePic(size: 64)
--- bigPic: profilePic(size: 1024)
--- }
--- @
---
--- Here "smallPic" and "bigPic" are aliases for the same field, "profilePic",
--- used to distinquish between profile pictures with different arguments
--- (sizes).
-type Alias = Name
-
-- ** Fragments
-- | Fragment definition.
diff --git a/src/Language/GraphQL/AST/Encoder.hs b/src/Language/GraphQL/AST/Encoder.hs
index 0757867..dcc24fe 100644
--- a/src/Language/GraphQL/AST/Encoder.hs
+++ b/src/Language/GraphQL/AST/Encoder.hs
@@ -126,8 +126,8 @@ indent indentation = Lazy.Text.replicate (fromIntegral indentation) indentSymbol
selection :: Formatter -> Selection -> Lazy.Text
selection formatter = Lazy.Text.append indent' . encodeSelection
where
- encodeSelection (Field alias name args directives' selections _) =
- field incrementIndent alias name args directives' selections
+ encodeSelection (FieldSelection fieldSelection) =
+ field incrementIndent fieldSelection
encodeSelection (InlineFragmentSelection fragmentSelection) =
inlineFragment incrementIndent fragmentSelection
encodeSelection (FragmentSpreadSelection fragmentSelection) =
@@ -142,15 +142,9 @@ selection formatter = Lazy.Text.append indent' . encodeSelection
colon :: Formatter -> Lazy.Text
colon formatter = eitherFormat formatter ": " ":"
--- | Converts Field into a string
-field :: Formatter ->
- Maybe Name ->
- Name ->
- [Argument] ->
- [Directive] ->
- [Selection] ->
- Lazy.Text
-field formatter alias name args dirs set
+-- | Converts Field into a string.
+field :: Formatter -> Field -> Lazy.Text
+field formatter (Field alias name args dirs set _)
= optempty prependAlias (fold alias)
<> Lazy.Text.fromStrict name
<> optempty (arguments formatter) args
diff --git a/src/Language/GraphQL/AST/Parser.hs b/src/Language/GraphQL/AST/Parser.hs
index e68956f..136067b 100644
--- a/src/Language/GraphQL/AST/Parser.hs
+++ b/src/Language/GraphQL/AST/Parser.hs
@@ -376,12 +376,12 @@ selectionSetOpt :: Parser SelectionSetOpt
selectionSetOpt = listOptIn braces selection <?> "SelectionSet"
selection :: Parser Selection
-selection = field
+selection = FieldSelection <$> field
<|> FragmentSpreadSelection <$> try fragmentSpread
<|> InlineFragmentSelection <$> inlineFragment
<?> "Selection"
-field :: Parser Selection
+field :: Parser Field
field = label "Field" $ do
location <- getLocation
alias' <- optional alias
@@ -391,7 +391,7 @@ field = label "Field" $ do
selectionSetOpt' <- selectionSetOpt
pure $ Field alias' name' arguments' directives' selectionSetOpt' location
-alias :: Parser Alias
+alias :: Parser Name
alias = try (name <* colon) <?> "Alias"
arguments :: Parser [Argument]