Add deriveToGraphQL
Some checks failed
Build / audit (push) Failing after 23s
Build / test (push) Failing after 22s
Build / doc (push) Failing after 24s

… for deriving `ToGraphQL` instances automatically.
This commit is contained in:
2024-07-07 12:55:42 +02:00
parent 6590cfaae8
commit 11ab7e18e1
4 changed files with 125 additions and 3 deletions

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