1
Fork 0

Add deriveToGraphQL

… for deriving `ToGraphQL` instances automatically.
This commit is contained in:
Eugen Wissner 2024-07-07 12:55:42 +02:00
parent 6590cfaae8
commit 11ab7e18e1
No known key found for this signature in database
GPG key ID: A27FDC1E8EE902C0
4 changed files with 125 additions and 3 deletions
tests/Language/GraphQL

View file

@ -3,6 +3,7 @@
obtain one at https://mozilla.org/MPL/2.0/. -}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
module Language.GraphQL.ClassSpec
( spec
) where
@ -11,8 +12,16 @@ import Data.Text (Text)
import Data.Time (UTCTime(..))
import Data.Time.Calendar.OrdinalDate (fromOrdinalDate)
import qualified Language.GraphQL.Type as Type
import Language.GraphQL.Class (FromGraphQL(..), ToGraphQL(..))
import Language.GraphQL.Class (FromGraphQL(..), ToGraphQL(..), deriveToGraphQL)
import Test.Hspec (Spec, describe, it, shouldBe)
import qualified Data.HashMap.Strict as HashMap
data TwoFieldRecord = TwoFieldRecord
{ x :: Int
, y :: Bool
}
$(deriveToGraphQL ''TwoFieldRecord)
spec :: Spec
spec = do
@ -65,3 +74,16 @@ spec = do
}
actual = fromGraphQL given
in actual `shouldBe` expected
describe "deriveToGraphQL" $ do
it "derives ToGraphQL for a record" $ do
let expected = Type.Object $ HashMap.fromList
[ ("x", Type.Int 1)
, ("y", Type.Boolean True)
, ("__typename", Type.String "TwoFieldRecord")
]
given = TwoFieldRecord
{ x = 1
, y = True
}
in toGraphQL given `shouldBe` expected