2020-07-20 21:29:12 +02:00
|
|
|
{- This Source Code Form is subject to the terms of the Mozilla Public License,
|
|
|
|
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 ExplicitForAll #-}
|
|
|
|
{-# LANGUAGE LambdaCase #-}
|
|
|
|
|
2020-07-24 21:34:31 +02:00
|
|
|
-- | GraphQL validator.
|
2020-07-20 21:29:12 +02:00
|
|
|
module Language.GraphQL.Validate
|
|
|
|
( Error(..)
|
|
|
|
, Path(..)
|
|
|
|
, document
|
|
|
|
, module Language.GraphQL.Validate.Rules
|
|
|
|
) where
|
|
|
|
|
2020-08-25 21:03:42 +02:00
|
|
|
import Control.Monad (foldM)
|
|
|
|
import Control.Monad.Trans.Reader (Reader, asks, mapReaderT, runReader)
|
2020-08-31 11:06:27 +02:00
|
|
|
import Data.Foldable (fold, foldrM)
|
2020-07-20 21:29:12 +02:00
|
|
|
import Data.Sequence (Seq(..), (><), (|>))
|
|
|
|
import qualified Data.Sequence as Seq
|
|
|
|
import Language.GraphQL.AST.Document
|
2020-08-25 21:03:42 +02:00
|
|
|
import Language.GraphQL.Type.Internal
|
|
|
|
import Language.GraphQL.Type.Schema (Schema(..))
|
2020-07-20 21:29:12 +02:00
|
|
|
import Language.GraphQL.Validate.Rules
|
2020-08-25 21:03:42 +02:00
|
|
|
import Language.GraphQL.Validate.Validation
|
2020-07-20 21:29:12 +02:00
|
|
|
|
2020-08-25 21:03:42 +02:00
|
|
|
type ValidateT m = Reader (Validation m) (Seq Error)
|
2020-07-20 21:29:12 +02:00
|
|
|
|
2020-07-24 21:34:31 +02:00
|
|
|
-- | Validates a document and returns a list of found errors. If the returned
|
|
|
|
-- list is empty, the document is valid.
|
2020-08-25 21:03:42 +02:00
|
|
|
document :: forall m. Schema m -> [Rule m] -> Document -> Seq Error
|
2020-07-20 21:29:12 +02:00
|
|
|
document schema' rules' document' =
|
|
|
|
runReader (foldrM go Seq.empty document') context
|
|
|
|
where
|
2020-08-25 21:03:42 +02:00
|
|
|
context = Validation
|
2020-07-20 21:29:12 +02:00
|
|
|
{ ast = document'
|
|
|
|
, schema = schema'
|
2020-08-25 21:03:42 +02:00
|
|
|
, types = collectReferencedTypes schema'
|
2020-07-20 21:29:12 +02:00
|
|
|
, rules = rules'
|
|
|
|
}
|
|
|
|
go definition' accumulator = (accumulator ><) <$> definition definition'
|
|
|
|
|
|
|
|
definition :: forall m. Definition -> ValidateT m
|
|
|
|
definition = \case
|
2020-08-25 21:03:42 +02:00
|
|
|
definition'@(ExecutableDefinition executableDefinition') -> do
|
2020-07-20 21:29:12 +02:00
|
|
|
applied <- applyRules definition'
|
|
|
|
children <- executableDefinition executableDefinition'
|
|
|
|
pure $ children >< applied
|
|
|
|
definition' -> applyRules definition'
|
|
|
|
where
|
2020-08-25 21:03:42 +02:00
|
|
|
applyRules definition' =
|
|
|
|
asks rules >>= foldM (ruleFilter definition') Seq.empty
|
|
|
|
ruleFilter definition' accumulator (DefinitionRule rule) =
|
2020-08-27 09:04:31 +02:00
|
|
|
mapReaderT (runRule accumulator) $ rule definition'
|
2020-08-25 21:03:42 +02:00
|
|
|
ruleFilter _ accumulator _ = pure accumulator
|
2020-08-27 09:04:31 +02:00
|
|
|
|
|
|
|
runRule :: Applicative f => Seq Error -> Maybe Error -> f (Seq Error)
|
|
|
|
runRule accumulator (Just error') = pure $ accumulator |> error'
|
|
|
|
runRule accumulator Nothing = pure accumulator
|
2020-07-20 21:29:12 +02:00
|
|
|
|
|
|
|
executableDefinition :: forall m. ExecutableDefinition -> ValidateT m
|
|
|
|
executableDefinition (DefinitionOperation definition') =
|
|
|
|
operationDefinition definition'
|
|
|
|
executableDefinition (DefinitionFragment definition') =
|
|
|
|
fragmentDefinition definition'
|
|
|
|
|
|
|
|
operationDefinition :: forall m. OperationDefinition -> ValidateT m
|
2020-08-25 21:03:42 +02:00
|
|
|
operationDefinition operation =
|
2020-08-31 11:06:27 +02:00
|
|
|
let selectionSet = getSelectionSet operation
|
|
|
|
in visitChildSelections ruleFilter selectionSet
|
2020-08-25 21:03:42 +02:00
|
|
|
where
|
2020-08-28 08:32:21 +02:00
|
|
|
ruleFilter accumulator (OperationDefinitionRule rule) =
|
|
|
|
mapReaderT (runRule accumulator) $ rule operation
|
|
|
|
ruleFilter accumulator _ = pure accumulator
|
2020-08-31 11:06:27 +02:00
|
|
|
getSelectionSet (SelectionSet selectionSet _) = selectionSet
|
|
|
|
getSelectionSet (OperationDefinition _ _ _ _ selectionSet _) = selectionSet
|
2020-07-20 21:29:12 +02:00
|
|
|
|
2020-08-31 11:06:27 +02:00
|
|
|
selection :: forall m. Selection -> ValidateT m
|
2020-09-05 10:00:58 +02:00
|
|
|
selection selection'
|
|
|
|
| FragmentSpread{} <- selection' =
|
|
|
|
asks rules >>= foldM ruleFilter Seq.empty
|
|
|
|
| Field _ _ _ _ selectionSet _ <- selection' =
|
|
|
|
visitChildSelections ruleFilter selectionSet
|
|
|
|
| InlineFragment _ _ selectionSet _ <- selection' =
|
|
|
|
visitChildSelections ruleFilter selectionSet
|
2020-08-31 11:06:27 +02:00
|
|
|
where
|
|
|
|
ruleFilter accumulator (SelectionRule rule) =
|
|
|
|
mapReaderT (runRule accumulator) $ rule selection'
|
|
|
|
ruleFilter accumulator _ = pure accumulator
|
|
|
|
|
|
|
|
traverseSelectionSet :: Traversable t => forall m. t Selection -> ValidateT m
|
|
|
|
traverseSelectionSet = fmap fold . traverse selection
|
|
|
|
|
|
|
|
visitChildSelections :: Traversable t
|
|
|
|
=> (Seq Error -> Rule m -> ValidateT m)
|
|
|
|
-> t Selection
|
|
|
|
-> ValidateT m
|
|
|
|
visitChildSelections ruleFilter selectionSet = do
|
|
|
|
rules' <- asks rules
|
|
|
|
applied <- foldM ruleFilter Seq.empty rules'
|
|
|
|
children <- traverseSelectionSet selectionSet
|
|
|
|
pure $ children >< applied
|
|
|
|
|
|
|
|
fragmentDefinition :: forall m. FragmentDefinition -> ValidateT m
|
|
|
|
fragmentDefinition fragment@(FragmentDefinition _ _ _ selectionSet _) =
|
|
|
|
visitChildSelections ruleFilter selectionSet
|
2020-08-28 08:32:21 +02:00
|
|
|
where
|
|
|
|
ruleFilter accumulator (FragmentDefinitionRule rule) =
|
|
|
|
mapReaderT (runRule accumulator) $ rule fragment
|
|
|
|
ruleFilter accumulator _ = pure accumulator
|