2017-02-19 19:29:58 +01:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2019-07-14 05:58:05 +02:00
|
|
|
module Language.GraphQL.AST.Transform
|
|
|
|
( document
|
|
|
|
) where
|
2017-01-29 22:44:03 +01:00
|
|
|
|
|
|
|
import Control.Applicative (empty)
|
2017-02-04 01:48:26 +01:00
|
|
|
import Control.Monad ((<=<))
|
2017-01-29 22:44:03 +01:00
|
|
|
import Data.Bifunctor (first)
|
|
|
|
import Data.Either (partitionEithers)
|
2017-02-12 19:19:13 +01:00
|
|
|
import Data.Foldable (fold, foldMap)
|
2017-01-29 22:44:03 +01:00
|
|
|
import qualified Data.List.NonEmpty as NonEmpty
|
2017-02-25 20:46:51 +01:00
|
|
|
import Data.Monoid (Alt(Alt,getAlt), (<>))
|
2017-01-29 22:44:03 +01:00
|
|
|
import Data.Text (Text)
|
2019-07-07 06:31:53 +02:00
|
|
|
import qualified Language.GraphQL.AST as Full
|
|
|
|
import qualified Language.GraphQL.AST.Core as Core
|
|
|
|
import qualified Language.GraphQL.Schema as Schema
|
2017-01-29 22:44:03 +01:00
|
|
|
|
|
|
|
type Name = Text
|
|
|
|
|
|
|
|
-- | Replaces a fragment name by a list of 'Field'. If the name doesn't match an
|
|
|
|
-- empty list is returned.
|
|
|
|
type Fragmenter = Name -> [Core.Field]
|
|
|
|
|
2017-02-25 20:46:51 +01:00
|
|
|
-- TODO: Replace Maybe by MonadThrow with CustomError
|
2017-02-04 01:48:26 +01:00
|
|
|
document :: Schema.Subs -> Full.Document -> Maybe Core.Document
|
2017-02-19 19:29:58 +01:00
|
|
|
document subs doc = operations subs fr ops
|
2017-01-29 22:44:03 +01:00
|
|
|
where
|
2017-02-12 19:19:13 +01:00
|
|
|
(fr, ops) = first foldFrags
|
|
|
|
. partitionEithers
|
|
|
|
. NonEmpty.toList
|
|
|
|
$ defrag subs
|
2017-02-19 19:29:58 +01:00
|
|
|
<$> doc
|
2017-01-29 22:44:03 +01:00
|
|
|
|
|
|
|
foldFrags :: [Fragmenter] -> Fragmenter
|
|
|
|
foldFrags fs n = getAlt $ foldMap (Alt . ($ n)) fs
|
|
|
|
|
2017-02-04 01:48:26 +01:00
|
|
|
-- * Operation
|
|
|
|
|
2017-02-25 20:46:51 +01:00
|
|
|
-- TODO: Replace Maybe by MonadThrow CustomError
|
2017-02-04 01:48:26 +01:00
|
|
|
operations
|
|
|
|
:: Schema.Subs
|
|
|
|
-> Fragmenter
|
|
|
|
-> [Full.OperationDefinition]
|
|
|
|
-> Maybe Core.Document
|
|
|
|
operations subs fr = NonEmpty.nonEmpty <=< traverse (operation subs fr)
|
|
|
|
|
2017-02-25 20:46:51 +01:00
|
|
|
-- TODO: Replace Maybe by MonadThrow CustomError
|
2017-02-04 01:48:26 +01:00
|
|
|
operation
|
|
|
|
:: Schema.Subs
|
|
|
|
-> Fragmenter
|
|
|
|
-> Full.OperationDefinition
|
|
|
|
-> Maybe Core.Operation
|
|
|
|
operation subs fr (Full.OperationSelectionSet sels) =
|
|
|
|
operation subs fr $ Full.OperationDefinition Full.Query empty empty empty sels
|
2017-02-12 19:19:13 +01:00
|
|
|
-- TODO: Validate Variable definitions with substituter
|
|
|
|
operation subs fr (Full.OperationDefinition ot _n _vars _dirs sels) =
|
2017-02-04 01:48:26 +01:00
|
|
|
case ot of
|
|
|
|
Full.Query -> Core.Query <$> node
|
|
|
|
Full.Mutation -> Core.Mutation <$> node
|
|
|
|
where
|
2017-02-25 20:46:51 +01:00
|
|
|
node = traverse (hush . selection subs fr) sels
|
2017-02-04 01:48:26 +01:00
|
|
|
|
2017-02-12 19:19:13 +01:00
|
|
|
selection
|
|
|
|
:: Schema.Subs
|
|
|
|
-> Fragmenter
|
|
|
|
-> Full.Selection
|
2017-02-25 20:46:51 +01:00
|
|
|
-> Either [Core.Field] Core.Field
|
2017-02-12 19:19:13 +01:00
|
|
|
selection subs fr (Full.SelectionField fld) =
|
2017-02-25 20:46:51 +01:00
|
|
|
Right $ field subs fr fld
|
2017-02-12 19:19:13 +01:00
|
|
|
selection _ fr (Full.SelectionFragmentSpread (Full.FragmentSpread n _dirs)) =
|
2017-02-25 20:46:51 +01:00
|
|
|
Left $ fr n
|
2017-02-12 19:19:13 +01:00
|
|
|
selection _ _ (Full.SelectionInlineFragment _) =
|
|
|
|
error "Inline fragments not supported yet"
|
2017-02-04 01:48:26 +01:00
|
|
|
|
2017-01-29 22:44:03 +01:00
|
|
|
-- * Fragment replacement
|
|
|
|
|
|
|
|
-- | Extract Fragments into a single Fragmenter function and a Operation
|
|
|
|
-- Definition.
|
2017-02-12 19:19:13 +01:00
|
|
|
defrag
|
|
|
|
:: Schema.Subs
|
|
|
|
-> Full.Definition
|
|
|
|
-> Either Fragmenter Full.OperationDefinition
|
|
|
|
defrag _ (Full.DefinitionOperation op) =
|
|
|
|
Right op
|
|
|
|
defrag subs (Full.DefinitionFragment fragDef) =
|
|
|
|
Left $ fragmentDefinition subs fragDef
|
|
|
|
|
|
|
|
fragmentDefinition :: Schema.Subs -> Full.FragmentDefinition -> Fragmenter
|
|
|
|
fragmentDefinition subs (Full.FragmentDefinition name _tc _dirs sels) name' =
|
2017-02-04 01:48:26 +01:00
|
|
|
-- TODO: Support fragments within fragments. Fold instead of map.
|
|
|
|
if name == name'
|
2017-02-25 20:46:51 +01:00
|
|
|
then either id pure =<< NonEmpty.toList (selection subs mempty <$> sels)
|
2017-02-04 01:48:26 +01:00
|
|
|
else empty
|
2017-01-29 22:44:03 +01:00
|
|
|
|
2017-02-25 20:46:51 +01:00
|
|
|
field :: Schema.Subs -> Fragmenter -> Full.Field -> Core.Field
|
2017-02-12 19:19:13 +01:00
|
|
|
field subs fr (Full.Field a n args _dirs sels) =
|
2017-02-25 20:46:51 +01:00
|
|
|
Core.Field a n (fold $ argument subs `traverse` args) (foldr go empty sels)
|
2017-02-19 19:29:58 +01:00
|
|
|
where
|
2017-02-25 20:46:51 +01:00
|
|
|
go :: Full.Selection -> [Core.Field] -> [Core.Field]
|
|
|
|
go (Full.SelectionFragmentSpread (Full.FragmentSpread name _dirs)) = (fr name <>)
|
|
|
|
go sel = (either id pure (selection subs fr sel) <>)
|
2017-02-12 19:19:13 +01:00
|
|
|
|
|
|
|
argument :: Schema.Subs -> Full.Argument -> Maybe Core.Argument
|
|
|
|
argument subs (Full.Argument n v) = Core.Argument n <$> value subs v
|
|
|
|
|
|
|
|
value :: Schema.Subs -> Full.Value -> Maybe Core.Value
|
|
|
|
value subs (Full.ValueVariable n) = subs n
|
|
|
|
value _ (Full.ValueInt i) = pure $ Core.ValueInt i
|
|
|
|
value _ (Full.ValueFloat f) = pure $ Core.ValueFloat f
|
|
|
|
value _ (Full.ValueString x) = pure $ Core.ValueString x
|
|
|
|
value _ (Full.ValueBoolean b) = pure $ Core.ValueBoolean b
|
|
|
|
value _ Full.ValueNull = pure Core.ValueNull
|
|
|
|
value _ (Full.ValueEnum e) = pure $ Core.ValueEnum e
|
|
|
|
value subs (Full.ValueList l) =
|
|
|
|
Core.ValueList <$> traverse (value subs) l
|
|
|
|
value subs (Full.ValueObject o) =
|
|
|
|
Core.ValueObject <$> traverse (objectField subs) o
|
|
|
|
|
|
|
|
objectField :: Schema.Subs -> Full.ObjectField -> Maybe Core.ObjectField
|
|
|
|
objectField subs (Full.ObjectField n v) = Core.ObjectField n <$> value subs v
|
2017-01-29 22:44:03 +01:00
|
|
|
|
2017-02-04 01:48:26 +01:00
|
|
|
hush :: Either a b -> Maybe b
|
|
|
|
hush = either (const Nothing) Just
|