@@ -21,10 +21,12 @@ module Data.Morpheus.Server.Deriving.Decode
2121 )
2222where
2323
24- import Control.Applicative ((<*>) , pure )
24+ import Control.Applicative (pure , (<*>) )
2525import Control.Monad ((>>=) )
26- import Data.Functor ((<$>) , Functor (.. ))
26+ import Data.Functor (Functor (.. ), (<$> ) )
2727import Data.List (elem )
28+ import Data.List.NonEmpty (NonEmpty )
29+ import qualified Data.List.NonEmpty as NonEmpty
2830import Data.Maybe (Maybe (.. ))
2931import Data.Morpheus.Internal.Utils
3032 ( elems ,
@@ -41,19 +43,12 @@ import Data.Morpheus.Server.Deriving.Utils
4143 datatypeNameProxy ,
4244 selNameProxy ,
4345 )
44- import Data.Morpheus.Server.Internal.TH.Decode
45- ( decodeFieldWith ,
46- withInputObject ,
47- withInputUnion ,
48- withList ,
49- withMaybe ,
50- withScalar ,
51- )
46+ import Data.Morpheus.Server.Internal.TH.Decode (decodeFieldWith , withInputObject , withInputUnion , withList , withMaybe , withRefinedList , withScalar )
5247import Data.Morpheus.Server.Types.GQLType
5348 ( GQLType
5449 ( KIND ,
55- __type ,
56- typeOptions
50+ typeOptions ,
51+ __type
5752 ),
5853 GQLTypeOptions (.. ),
5954 TypeData (.. ),
@@ -79,14 +74,15 @@ import Data.Morpheus.Types.Internal.Resolving
7974 )
8075import Data.Proxy (Proxy (.. ))
8176import Data.Semigroup (Semigroup (.. ))
77+ import Data.Sequence (Seq )
78+ import qualified Data.Sequence as Seq
79+ import Data.Set (Set )
80+ import qualified Data.Set as Set
81+ import Data.String (IsString (fromString ))
82+ import Data.Vector (Vector )
83+ import qualified Data.Vector as Vector
8284import GHC.Generics
83- import Prelude
84- ( ($) ,
85- (.) ,
86- Eq (.. ),
87- Ord ,
88- otherwise ,
89- )
85+ import Prelude (Either (Left , Right ), Eq (.. ), Foldable (length ), Ord , maybe , otherwise , show , ($) , (-) , (.) )
9086
9187type DecodeConstraint a =
9288 ( Generic a ,
@@ -113,6 +109,26 @@ instance Decode a => Decode (Maybe a) where
113109instance Decode a => Decode [a ] where
114110 decode = withList decode
115111
112+ instance Decode a => Decode (NonEmpty a ) where
113+ decode = withRefinedList (maybe (Left " Expected a NonEmpty list" ) Right . NonEmpty. nonEmpty) decode
114+
115+ -- | Should this instance dedupe silently or fail on dupes ?
116+ instance (Ord a , Decode a ) => Decode (Set a ) where
117+ decode val = do
118+ listVal <- withList (decode @ a ) val
119+ let setVal = Set. fromList listVal
120+ let setLength = length setVal
121+ let listLength = length setVal
122+ if listLength == setLength
123+ then pure setVal
124+ else failure (fromString (" Expected a List without duplicates, found " <> show (setLength - listLength) <> " duplicates" ) :: InternalError )
125+
126+ instance (Decode a ) => Decode (Seq a ) where
127+ decode = fmap Seq. fromList . withList decode
128+
129+ instance (Decode a ) => Decode (Vector a ) where
130+ decode = fmap Vector. fromList . withList decode
131+
116132-- | Decode GraphQL type with Specific Kind
117133class DecodeKind (kind :: GQL_KIND ) a where
118134 decodeKind :: Proxy kind -> ValidValue -> ResolverState a
0 commit comments