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-07-20 21:29:12 +02:00
|
|
|
import Data.Foldable (foldrM)
|
|
|
|
import Data.Sequence (Seq(..), (><), (|>))
|
|
|
|
import qualified Data.Sequence as Seq
|
|
|
|
import Data.Text (Text)
|
|
|
|
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
|
|
|
-- | If an error can be associated to a particular field in the GraphQL result,
|
|
|
|
-- it must contain an entry with the key path that details the path of the
|
|
|
|
-- response field which experienced the error. This allows clients to identify
|
|
|
|
-- whether a null result is intentional or caused by a runtime error.
|
2020-07-20 21:29:12 +02:00
|
|
|
data Path
|
2020-07-24 21:34:31 +02:00
|
|
|
= Segment Text -- ^ Field name.
|
|
|
|
| Index Int -- ^ List index if a field returned a list.
|
2020-07-20 21:29:12 +02:00
|
|
|
deriving (Eq, Show)
|
|
|
|
|
2020-07-24 21:34:31 +02:00
|
|
|
-- | Validation error.
|
2020-07-20 21:29:12 +02:00
|
|
|
data Error = Error
|
|
|
|
{ message :: String
|
|
|
|
, locations :: [Location]
|
|
|
|
, path :: [Path]
|
|
|
|
} deriving (Eq, Show)
|
|
|
|
|
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) =
|
|
|
|
flip mapReaderT (rule definition') $ \case
|
|
|
|
Just message' ->
|
|
|
|
pure $ accumulator |> Error
|
|
|
|
{ message = message'
|
|
|
|
, locations = [definitionLocation definition']
|
|
|
|
, path = []
|
|
|
|
}
|
|
|
|
Nothing -> pure accumulator
|
|
|
|
ruleFilter _ accumulator _ = pure accumulator
|
|
|
|
definitionLocation (ExecutableDefinition executableDefinition')
|
|
|
|
| DefinitionOperation definitionOperation <- executableDefinition'
|
|
|
|
, SelectionSet _ location <- definitionOperation = location
|
|
|
|
| DefinitionOperation definitionOperation <- executableDefinition'
|
|
|
|
, OperationDefinition _ _ _ _ _ location <- definitionOperation =
|
|
|
|
location
|
|
|
|
| DefinitionFragment fragmentDefinition' <- executableDefinition'
|
|
|
|
, FragmentDefinition _ _ _ _ location <- fragmentDefinition' = location
|
2020-07-20 21:29:12 +02:00
|
|
|
definitionLocation (TypeSystemDefinition _ location) = location
|
|
|
|
definitionLocation (TypeSystemExtension _ location) = location
|
|
|
|
|
|
|
|
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 =
|
|
|
|
asks rules >>= foldM (ruleFilter operation) Seq.empty
|
|
|
|
where
|
|
|
|
ruleFilter definition' accumulator (OperationDefinitionRule rule) =
|
|
|
|
flip mapReaderT (rule definition') $ \case
|
|
|
|
Just message' ->
|
|
|
|
pure $ accumulator |> Error
|
|
|
|
{ message = message'
|
|
|
|
, locations = [definitionLocation operation]
|
|
|
|
, path = []
|
|
|
|
}
|
|
|
|
Nothing -> pure accumulator
|
|
|
|
ruleFilter _ accumulator _ = pure accumulator
|
|
|
|
definitionLocation (SelectionSet _ location) = location
|
|
|
|
definitionLocation (OperationDefinition _ _ _ _ _ location) = location
|
2020-07-20 21:29:12 +02:00
|
|
|
|
|
|
|
fragmentDefinition :: forall m. FragmentDefinition -> ValidateT m
|
|
|
|
fragmentDefinition _fragment = pure Seq.empty
|