diff options
| author | Eugen Wissner <belka@caraus.de> | 2019-12-17 09:03:18 +0100 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2019-12-18 09:03:18 +0100 |
| commit | 0cbe69736be1ce4e7e4d82ba70f97d5a2bdcdd0c (patch) | |
| tree | 6c13368061b75f417e1a2da4eb1957eccb5c1748 /src/Language/GraphQL/Type | |
| parent | 4c0d226030015c98ce76ebb4815dc9162ad10b11 (diff) | |
| download | graphql-0cbe69736be1ce4e7e4d82ba70f97d5a2bdcdd0c.tar.gz | |
Move Execute.Directive to Type.Directive
Just to roughly follow the structure of the reference implementation.
Diffstat (limited to 'src/Language/GraphQL/Type')
| -rw-r--r-- | src/Language/GraphQL/Type/Directive.hs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/Language/GraphQL/Type/Directive.hs b/src/Language/GraphQL/Type/Directive.hs new file mode 100644 index 0000000..afd97da --- /dev/null +++ b/src/Language/GraphQL/Type/Directive.hs @@ -0,0 +1,50 @@ +{-# LANGUAGE OverloadedStrings #-} + +module Language.GraphQL.Type.Directive + ( selection + ) where + +import qualified Data.HashMap.Strict as HashMap +import Language.GraphQL.AST.Core + +-- | Directive processing status. +data Status + = Skip -- ^ Skip the selection and stop directive processing + | Include Directive -- ^ The directive was processed, try other handlers + | Continue Directive -- ^ Directive handler mismatch, try other handlers + +-- | Takes a list of directives, handles supported directives and excludes them +-- from the result. If the selection should be skipped, returns 'Nothing'. +selection :: [Directive] -> Maybe [Directive] +selection = foldr go (Just []) + where + go directive' directives' = + case (skip . include) (Continue directive') of + (Include _) -> directives' + Skip -> Nothing + (Continue x) -> (x :) <$> directives' + +handle :: (Directive -> Status) -> Status -> Status +handle _ Skip = Skip +handle handler (Continue directive) = handler directive +handle handler (Include directive) = handler directive + +-- * Directive implementations + +skip :: Status -> Status +skip = handle skip' + where + skip' directive'@(Directive "skip" (Arguments arguments)) = + case HashMap.lookup "if" arguments of + (Just (Boolean True)) -> Skip + _ -> Include directive' + skip' directive' = Continue directive' + +include :: Status -> Status +include = handle include' + where + include' directive'@(Directive "include" (Arguments arguments)) = + case HashMap.lookup "if" arguments of + (Just (Boolean True)) -> Include directive' + _ -> Skip + include' directive' = Continue directive' |
