aboutsummaryrefslogtreecommitdiff
path: root/src/Language/GraphQL/Validate/Rules.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Language/GraphQL/Validate/Rules.hs')
-rw-r--r--src/Language/GraphQL/Validate/Rules.hs21
1 files changed, 17 insertions, 4 deletions
diff --git a/src/Language/GraphQL/Validate/Rules.hs b/src/Language/GraphQL/Validate/Rules.hs
index 8c3156b..2d7adba 100644
--- a/src/Language/GraphQL/Validate/Rules.hs
+++ b/src/Language/GraphQL/Validate/Rules.hs
@@ -2,11 +2,13 @@
v. 2.0. If a copy of the MPL was not distributed with this file, You can
obtain one at https://mozilla.org/MPL/2.0/. -}
+{-# LANGUAGE DataKinds #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
+{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ViewPatterns #-}
-- | This module contains default rules defined in the GraphQL specification.
@@ -61,6 +63,7 @@ import Data.Sequence (Seq(..), (|>))
import qualified Data.Sequence as Seq
import Data.Text (Text)
import qualified Data.Text as Text
+import GHC.Records (HasField(..))
import qualified Language.GraphQL.AST.Document as Full
import qualified Language.GraphQL.Type.Definition as Definition
import qualified Language.GraphQL.Type.Internal as Type
@@ -618,6 +621,10 @@ noUndefinedVariablesRule =
, "\"."
]
+-- Used to find the difference between defined and used variables. The first
+-- argument are variables defined in the operation, the second argument are
+-- variables used in the query. It should return the difference between these
+-- 2 sets.
type UsageDifference
= HashMap Full.Name [Full.Location]
-> HashMap Full.Name [Full.Location]
@@ -664,11 +671,17 @@ variableUsageDifference difference errorMessage = OperationDefinitionRule $ \cas
= filterSelections' selections
>>= lift . mapReaderT (<> mapDirectives directives') . pure
findDirectiveVariables (Full.Directive _ arguments _) = mapArguments arguments
- mapArguments = Seq.fromList . mapMaybe findArgumentVariables
+ mapArguments = Seq.fromList . (>>= findArgumentVariables)
mapDirectives = foldMap findDirectiveVariables
- findArgumentVariables (Full.Argument _ Full.Node{ node = Full.Variable value', ..} _) =
- Just (value', [location])
- findArgumentVariables _ = Nothing
+
+ findArgumentVariables (Full.Argument _ value _) = findNodeVariables value
+ findNodeVariables Full.Node{ node = value, ..} = findValueVariables location value
+
+ findValueVariables location (Full.Variable value') = [(value', [location])]
+ findValueVariables _ (Full.List values) = values >>= findNodeVariables
+ findValueVariables _ (Full.Object fields) = fields
+ >>= findNodeVariables . getField @"value"
+ findValueVariables _ _ = []
makeError operationName (variableName, locations') = Error
{ message = errorMessage operationName variableName
, locations = locations'