forked from OSS/graphql
Validate variable names are unique
This commit is contained in:
@ -22,6 +22,7 @@ module Language.GraphQL.Validate.Rules
|
||||
, uniqueDirectiveNamesRule
|
||||
, uniqueFragmentNamesRule
|
||||
, uniqueOperationNamesRule
|
||||
, uniqueVariableNamesRule
|
||||
) where
|
||||
|
||||
import Control.Monad (foldM)
|
||||
@ -64,6 +65,8 @@ specifiedRules =
|
||||
, noFragmentCyclesRule
|
||||
-- Directives.
|
||||
, uniqueDirectiveNamesRule
|
||||
-- Variables.
|
||||
, uniqueVariableNamesRule
|
||||
]
|
||||
|
||||
-- | Definition must be OperationDefinition or FragmentDefinition.
|
||||
@ -492,3 +495,13 @@ filterDuplicates extract nodeType = lift
|
||||
, Text.unpack $ fst $ extract directive
|
||||
, "\"."
|
||||
]
|
||||
|
||||
-- | If any operation defines more than one variable with the same name, it is
|
||||
-- ambiguous and invalid. It is invalid even if the type of the duplicate
|
||||
-- variable is the same.
|
||||
uniqueVariableNamesRule :: forall m. Rule m
|
||||
uniqueVariableNamesRule = VariablesRule
|
||||
$ filterDuplicates extract "variable"
|
||||
where
|
||||
extract (VariableDefinition variableName _ _ location) =
|
||||
(variableName, location)
|
||||
|
@ -43,6 +43,7 @@ data Rule m
|
||||
| FieldRule (Field -> RuleT m)
|
||||
| ArgumentsRule (Field -> RuleT m) (Directive -> RuleT m)
|
||||
| DirectivesRule ([Directive] -> RuleT m)
|
||||
| VariablesRule ([VariableDefinition] -> RuleT m)
|
||||
|
||||
-- | Monad transformer used by the rules.
|
||||
type RuleT m = ReaderT (Validation m) Seq Error
|
||||
|
Reference in New Issue
Block a user