@@ -123,7 +123,7 @@ getSelectionSet (Mutation _ _ ss) = ss
123123-- | Type alias for 'Query' and 'Mutation' constructors of 'Operation'.
124124type OperationType value = VariableDefinitions -> Directives value -> SelectionSetByType value -> Operation value
125125
126- type Operations value = Map Name (Operation value )
126+ type Operations value = Map ( Maybe Name ) (Operation value )
127127
128128-- | Turn a parsed document into a known valid one.
129129--
@@ -132,9 +132,9 @@ type Operations value = Map Name (Operation value)
132132validate :: Schema -> AST. QueryDocument -> Either (NonEmpty ValidationError ) (QueryDocument VariableValue )
133133validate schema (AST. QueryDocument defns) = runValidator $ do
134134 let (operations, fragments) = splitBy splitDefns defns
135- let (anonymous, named ) = splitBy splitOps operations
135+ let (anonymous, maybeNamed ) = splitBy splitOps operations
136136 (frags, visitedFrags) <- resolveFragmentDefinitions =<< validateFragmentDefinitions schema fragments
137- case (anonymous, named ) of
137+ case (anonymous, maybeNamed ) of
138138 ([] , ops) -> do
139139 (validOps, usedFrags) <- runStateT (validateOperations schema frags ops) mempty
140140 assertAllFragmentsUsed frags (visitedFrags <> usedFrags)
@@ -146,7 +146,7 @@ validate schema (AST.QueryDocument defns) = runValidator $ do
146146 validValuesSS <- validateValues ss
147147 resolvedValuesSS <- resolveVariables emptyVariableDefinitions validValuesSS
148148 pure (LoneAnonymousOperation (Query emptyVariableDefinitions emptyDirectives resolvedValuesSS))
149- _ -> throwE (MixedAnonymousOperations (length anonymous) (map fst named ))
149+ _ -> throwE (MixedAnonymousOperations (length anonymous) (map fst maybeNamed ))
150150
151151 where
152152 splitBy :: (a -> Either b c ) -> [a ] -> ([b ], [c ])
@@ -156,17 +156,17 @@ validate schema (AST.QueryDocument defns) = runValidator $ do
156156 splitDefns (AST. DefinitionFragment frag) = Right frag
157157
158158 splitOps (AST. AnonymousQuery ss) = Left ss
159- splitOps (AST. Query node@ (AST. Node name _ _ _)) = Right (name , (Query , node))
160- splitOps (AST. Mutation node@ (AST. Node name _ _ _)) = Right (name , (Mutation , node))
159+ splitOps (AST. Query node@ (AST. Node maybeName _ _ _)) = Right (maybeName , (Query , node))
160+ splitOps (AST. Mutation node@ (AST. Node maybeName _ _ _)) = Right (maybeName , (Mutation , node))
161161
162- assertAllFragmentsUsed :: Fragments value -> Set Name -> Validation ()
162+ assertAllFragmentsUsed :: Fragments value -> Set ( Maybe Name ) -> Validation ()
163163 assertAllFragmentsUsed fragments used =
164- let unused = Map. keysSet fragments `Set.difference` used
164+ let unused = ( Set. map pure ( Map. keysSet fragments)) `Set.difference` used
165165 in unless (Set. null unused) (throwE (UnusedFragments unused))
166166
167167-- * Operations
168168
169- validateOperations :: Schema -> Fragments AST. Value -> [(Name , (OperationType AST. Value , AST. Node ))] -> StateT (Set Name ) Validation (Operations AST. Value )
169+ validateOperations :: Schema -> Fragments AST. Value -> [(Maybe Name , (OperationType AST. Value , AST. Node ))] -> StateT (Set ( Maybe Name ) ) Validation (Operations AST. Value )
170170validateOperations schema fragments ops = do
171171 deduped <- lift (mapErrors DuplicateOperation (makeMap ops))
172172 traverse validateNode deduped
@@ -219,7 +219,7 @@ validateOperation (Mutation vars directives selectionSet) = do
219219-- We do this /before/ validating the values (since that's much easier once
220220-- everything is in a nice structure and away from the AST), which means we
221221-- can't yet evaluate directives.
222- validateSelectionSet :: Schema -> Fragments AST. Value -> [AST. Selection ] -> StateT (Set Name ) Validation (SelectionSetByType AST. Value )
222+ validateSelectionSet :: Schema -> Fragments AST. Value -> [AST. Selection ] -> StateT (Set ( Maybe Name ) ) Validation (SelectionSetByType AST. Value )
223223validateSelectionSet schema fragments selections = do
224224 unresolved <- lift $ traverse (validateSelection schema) selections
225225 resolved <- traverse (resolveSelection fragments) unresolved
@@ -508,14 +508,14 @@ validateSelection schema selection =
508508-- We're doing a standard depth-first traversal of fragment references, where
509509-- references are by name, so the set of names can be thought of as a record
510510-- of visited references.
511- resolveSelection :: Fragments a -> Selection' UnresolvedFragmentSpread a -> StateT (Set Name ) Validation (Selection' FragmentSpread a )
511+ resolveSelection :: Fragments a -> Selection' UnresolvedFragmentSpread a -> StateT (Set ( Maybe Name ) ) Validation (Selection' FragmentSpread a )
512512resolveSelection fragments = traverseFragmentSpreads resolveFragmentSpread
513513 where
514514 resolveFragmentSpread (UnresolvedFragmentSpread name directive) = do
515515 case Map. lookup name fragments of
516516 Nothing -> lift (throwE (NoSuchFragment name))
517517 Just fragment -> do
518- modify (Set. insert name)
518+ modify (Set. insert ( pure name) )
519519 pure (FragmentSpread name directive fragment)
520520
521521-- * Fragment definitions
@@ -577,7 +577,7 @@ validateTypeCondition schema (NamedType typeCond) =
577577--
578578-- <https://facebook.github.io/graphql/#sec-Fragment-spread-target-defined>
579579-- <https://facebook.github.io/graphql/#sec-Fragment-spreads-must-not-form-cycles>
580- resolveFragmentDefinitions :: Map Name (FragmentDefinition UnresolvedFragmentSpread value ) -> Validation (Fragments value , Set Name )
580+ resolveFragmentDefinitions :: Map Name (FragmentDefinition UnresolvedFragmentSpread value ) -> Validation (Fragments value , Set ( Maybe Name ) )
581581resolveFragmentDefinitions allFragments =
582582 splitResult <$> traverse resolveFragment allFragments
583583 where
@@ -595,12 +595,12 @@ resolveFragmentDefinitions allFragments =
595595 FragmentDefinition name cond directives <$> traverse (traverseFragmentSpreads resolveSpread) ss
596596
597597 resolveSpread (UnresolvedFragmentSpread name directives) = do
598- visited <- Set. member name <$> get
598+ visited <- Set. member ( pure name) <$> get
599599 when visited (lift (throwE (CircularFragmentSpread name)))
600600 case Map. lookup name allFragments of
601601 Nothing -> lift (throwE (NoSuchFragment name))
602602 Just definition -> do
603- modify (Set. insert name)
603+ modify (Set. insert ( pure name) )
604604 FragmentSpread name directives <$> resolveFragment' definition
605605
606606-- * Arguments
@@ -727,12 +727,12 @@ data ValidationError
727727 -- with the given name.
728728 --
729729 -- <https://facebook.github.io/graphql/#sec-Operation-Name-Uniqueness>
730- = DuplicateOperation Name
730+ = DuplicateOperation ( Maybe Name )
731731 -- | 'MixedAnonymousOperations' means there was more than one operation
732732 -- defined in a document with an anonymous operation.
733733 --
734734 -- <https://facebook.github.io/graphql/#sec-Lone-Anonymous-Operation>
735- | MixedAnonymousOperations Int [Name ]
735+ | MixedAnonymousOperations Int [Maybe Name ]
736736 -- | 'DuplicateArgument' means that multiple copies of the same argument was
737737 -- given to the same field, directive, etc.
738738 | DuplicateArgument Name
@@ -755,7 +755,7 @@ data ValidationError
755755 | CircularFragmentSpread Name
756756 -- | 'UnusedFragments' means that fragments were defined that weren't used.
757757 -- <https://facebook.github.io/graphql/#sec-Fragments-Must-Be-Used>
758- | UnusedFragments (Set Name )
758+ | UnusedFragments (Set ( Maybe Name ) )
759759 -- | Variables were defined without being used.
760760 -- <https://facebook.github.io/graphql/#sec-All-Variables-Used>
761761 | UnusedVariables (Set Variable )
@@ -777,10 +777,10 @@ data ValidationError
777777 deriving (Eq , Show )
778778
779779instance GraphQLError ValidationError where
780- formatError (DuplicateOperation name ) = " More than one operation named '" <> show name <> " '"
781- formatError (MixedAnonymousOperations n names )
782- | n > 1 && null names = " Multiple anonymous operations defined. Found " <> show n
783- | otherwise = " Document contains both anonymous operations (" <> show n <> " ) and named operations (" <> show names <> " )"
780+ formatError (DuplicateOperation maybeName ) = " More than one operation named '" <> show maybeName <> " '"
781+ formatError (MixedAnonymousOperations n maybeNames )
782+ | n > 1 && null maybeNames = " Multiple anonymous operations defined. Found " <> show n
783+ | otherwise = " Document contains both anonymous operations (" <> show n <> " ) and named operations (" <> show maybeNames <> " )"
784784 formatError (DuplicateArgument name) = " More than one argument named '" <> show name <> " '"
785785 formatError (DuplicateFragmentDefinition name) = " More than one fragment named '" <> show name <> " '"
786786 formatError (NoSuchFragment name) = " No fragment named '" <> show name <> " '"
0 commit comments