{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE NoImplicitPrelude #-}
module Data.Morpheus.Types.SelectionTree where
import Data.Bool (Bool (..))
import Data.Foldable (concatMap)
import Data.Monoid (mempty)
import Data.Morpheus.Internal.Utils (elems, keyOf)
import Data.Morpheus.Types.Internal.AST
( FieldName (..),
Selection (..),
Selection (selectionContent),
SelectionContent (SelectionField, SelectionSet, UnionSelection),
UnionTag (..),
VALID,
)
import Data.String
( IsString (..),
)
import Data.Text (unpack)
import Prelude ((.))
class SelectionTree nodeType where
isLeaf :: nodeType -> Bool
getChildrenList :: nodeType -> [nodeType]
getName :: IsString name => nodeType -> name
instance SelectionTree (Selection VALID) where
isLeaf node = case selectionContent node of
SelectionField -> True
_ -> False
getChildrenList node = case selectionContent node of
SelectionField -> mempty
(SelectionSet deeperSel) -> elems deeperSel
(UnionSelection sel) ->
concatMap
(elems . unionTagSelection)
(elems sel)
getName =
fromString
. unpack
. readName
. keyOf