summaryrefslogtreecommitdiff
path: root/src/Data/Morpheus/Types/ID.hs
blob: 4c983a4052e5915327d30217c1309ba74dcbe466 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
{-# LANGUAGE DeriveGeneric     #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeFamilies      #-}

module Data.Morpheus.Types.ID
  ( ID(..)
  ) where

import           Data.Morpheus.Kind                 (SCALAR)
import           Data.Morpheus.Types.GQLScalar      (GQLScalar (..))
import           Data.Morpheus.Types.GQLType        (GQLType (KIND, __typeVisibility))
import           Data.Morpheus.Types.Internal.Value (ScalarValue (..))
import           Data.Text                          (Text, pack)
import           GHC.Generics                       (Generic)

-- | default GraphQL type,
-- parses only 'String' and 'Int' values,
-- serialized always as 'String'
newtype ID = ID
  { unpackID :: Text
  } deriving (Show, Generic)

instance GQLType ID where
  type KIND ID = SCALAR
  __typeVisibility _ = False

instance GQLScalar ID where
  parseValue (Int x)    = return (ID $ pack $ show x)
  parseValue (String x) = return (ID x)
  parseValue _          = Left ""
  serialize (ID x) = String x