@@ -123,7 +123,7 @@ getSelectionSet (Mutation _ _ ss) = ss
123
123
-- | Type alias for 'Query' and 'Mutation' constructors of 'Operation'.
124
124
type OperationType value = VariableDefinitions -> Directives value -> SelectionSetByType value -> Operation value
125
125
126
- type Operations value = Map Name (Operation value )
126
+ type Operations value = Map ( Maybe Name ) (Operation value )
127
127
128
128
-- | Turn a parsed document into a known valid one.
129
129
--
@@ -132,9 +132,9 @@ type Operations value = Map Name (Operation value)
132
132
validate :: Schema -> AST. QueryDocument -> Either (NonEmpty ValidationError ) (QueryDocument VariableValue )
133
133
validate schema (AST. QueryDocument defns) = runValidator $ do
134
134
let (operations, fragments) = splitBy splitDefns defns
135
- let (anonymous, named ) = splitBy splitOps operations
135
+ let (anonymous, maybeNamed ) = splitBy splitOps operations
136
136
(frags, visitedFrags) <- resolveFragmentDefinitions =<< validateFragmentDefinitions schema fragments
137
- case (anonymous, named ) of
137
+ case (anonymous, maybeNamed ) of
138
138
([] , ops) -> do
139
139
(validOps, usedFrags) <- runStateT (validateOperations schema frags ops) mempty
140
140
assertAllFragmentsUsed frags (visitedFrags <> usedFrags)
@@ -146,7 +146,7 @@ validate schema (AST.QueryDocument defns) = runValidator $ do
146
146
validValuesSS <- validateValues ss
147
147
resolvedValuesSS <- resolveVariables emptyVariableDefinitions validValuesSS
148
148
pure (LoneAnonymousOperation (Query emptyVariableDefinitions emptyDirectives resolvedValuesSS))
149
- _ -> throwE (MixedAnonymousOperations (length anonymous) (map fst named ))
149
+ _ -> throwE (MixedAnonymousOperations (length anonymous) (map fst maybeNamed ))
150
150
151
151
where
152
152
splitBy :: (a -> Either b c ) -> [a ] -> ([b ], [c ])
@@ -156,17 +156,17 @@ validate schema (AST.QueryDocument defns) = runValidator $ do
156
156
splitDefns (AST. DefinitionFragment frag) = Right frag
157
157
158
158
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))
161
161
162
- assertAllFragmentsUsed :: Fragments value -> Set Name -> Validation ()
162
+ assertAllFragmentsUsed :: Fragments value -> Set ( Maybe Name ) -> Validation ()
163
163
assertAllFragmentsUsed fragments used =
164
- let unused = Map. keysSet fragments `Set.difference` used
164
+ let unused = ( Set. map pure ( Map. keysSet fragments)) `Set.difference` used
165
165
in unless (Set. null unused) (throwE (UnusedFragments unused))
166
166
167
167
-- * Operations
168
168
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 )
170
170
validateOperations schema fragments ops = do
171
171
deduped <- lift (mapErrors DuplicateOperation (makeMap ops))
172
172
traverse validateNode deduped
@@ -219,7 +219,7 @@ validateOperation (Mutation vars directives selectionSet) = do
219
219
-- We do this /before/ validating the values (since that's much easier once
220
220
-- everything is in a nice structure and away from the AST), which means we
221
221
-- 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 )
223
223
validateSelectionSet schema fragments selections = do
224
224
unresolved <- lift $ traverse (validateSelection schema) selections
225
225
resolved <- traverse (resolveSelection fragments) unresolved
@@ -508,14 +508,14 @@ validateSelection schema selection =
508
508
-- We're doing a standard depth-first traversal of fragment references, where
509
509
-- references are by name, so the set of names can be thought of as a record
510
510
-- 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 )
512
512
resolveSelection fragments = traverseFragmentSpreads resolveFragmentSpread
513
513
where
514
514
resolveFragmentSpread (UnresolvedFragmentSpread name directive) = do
515
515
case Map. lookup name fragments of
516
516
Nothing -> lift (throwE (NoSuchFragment name))
517
517
Just fragment -> do
518
- modify (Set. insert name)
518
+ modify (Set. insert ( pure name) )
519
519
pure (FragmentSpread name directive fragment)
520
520
521
521
-- * Fragment definitions
@@ -577,7 +577,7 @@ validateTypeCondition schema (NamedType typeCond) =
577
577
--
578
578
-- <https://facebook.github.io/graphql/#sec-Fragment-spread-target-defined>
579
579
-- <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 ) )
581
581
resolveFragmentDefinitions allFragments =
582
582
splitResult <$> traverse resolveFragment allFragments
583
583
where
@@ -595,12 +595,12 @@ resolveFragmentDefinitions allFragments =
595
595
FragmentDefinition name cond directives <$> traverse (traverseFragmentSpreads resolveSpread) ss
596
596
597
597
resolveSpread (UnresolvedFragmentSpread name directives) = do
598
- visited <- Set. member name <$> get
598
+ visited <- Set. member ( pure name) <$> get
599
599
when visited (lift (throwE (CircularFragmentSpread name)))
600
600
case Map. lookup name allFragments of
601
601
Nothing -> lift (throwE (NoSuchFragment name))
602
602
Just definition -> do
603
- modify (Set. insert name)
603
+ modify (Set. insert ( pure name) )
604
604
FragmentSpread name directives <$> resolveFragment' definition
605
605
606
606
-- * Arguments
@@ -727,12 +727,12 @@ data ValidationError
727
727
-- with the given name.
728
728
--
729
729
-- <https://facebook.github.io/graphql/#sec-Operation-Name-Uniqueness>
730
- = DuplicateOperation Name
730
+ = DuplicateOperation ( Maybe Name )
731
731
-- | 'MixedAnonymousOperations' means there was more than one operation
732
732
-- defined in a document with an anonymous operation.
733
733
--
734
734
-- <https://facebook.github.io/graphql/#sec-Lone-Anonymous-Operation>
735
- | MixedAnonymousOperations Int [Name ]
735
+ | MixedAnonymousOperations Int [Maybe Name ]
736
736
-- | 'DuplicateArgument' means that multiple copies of the same argument was
737
737
-- given to the same field, directive, etc.
738
738
| DuplicateArgument Name
@@ -755,7 +755,7 @@ data ValidationError
755
755
| CircularFragmentSpread Name
756
756
-- | 'UnusedFragments' means that fragments were defined that weren't used.
757
757
-- <https://facebook.github.io/graphql/#sec-Fragments-Must-Be-Used>
758
- | UnusedFragments (Set Name )
758
+ | UnusedFragments (Set ( Maybe Name ) )
759
759
-- | Variables were defined without being used.
760
760
-- <https://facebook.github.io/graphql/#sec-All-Variables-Used>
761
761
| UnusedVariables (Set Variable )
@@ -777,10 +777,10 @@ data ValidationError
777
777
deriving (Eq , Show )
778
778
779
779
instance 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 <> " )"
784
784
formatError (DuplicateArgument name) = " More than one argument named '" <> show name <> " '"
785
785
formatError (DuplicateFragmentDefinition name) = " More than one fragment named '" <> show name <> " '"
786
786
formatError (NoSuchFragment name) = " No fragment named '" <> show name <> " '"
0 commit comments