Skip to content

Commit 16d817c

Browse files
authored
Merge pull request #194 from jamesdabbs/jcd/fix-generics
Add tests for generic fixes
2 parents 377c332 + c00b103 commit 16d817c

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

Diff for: src/GraphQL/Internal/Value/FromValue.hs

+6-9
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,12 @@ instance forall dataName consName records s l p.
100100
)) where
101101
genericFromValue o = M1 . M1 <$> genericFromValue @records o
102102

103-
instance forall wrappedType fieldName rest u s l.
104-
( KnownSymbol fieldName
105-
, FromValue wrappedType
106-
, GenericFromValue rest
107-
) => GenericFromValue (S1 ('MetaSel ('Just fieldName) u s l) (Rec0 wrappedType) :*: rest) where
108-
genericFromValue object = do
109-
l <- getValue @wrappedType @fieldName object
110-
r <- genericFromValue @rest object
111-
pure (l :*: r)
103+
104+
instance forall l r.
105+
( GenericFromValue l
106+
, GenericFromValue r
107+
) => GenericFromValue (l :*: r) where
108+
genericFromValue object = liftA2 (:*:) (genericFromValue @l object) (genericFromValue @r object)
112109

113110
-- | Look up a single record field element in the Object.
114111
getValue :: forall wrappedType fieldName u s l p. (FromValue wrappedType, KnownSymbol fieldName)

Diff for: tests/ValueTests.hs

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE DeriveGeneric #-}
12
module ValueTests (tests) where
23

34
import Protolude
@@ -11,15 +12,24 @@ import qualified GraphQL.Internal.Syntax.AST as AST
1112
import GraphQL.Internal.Arbitrary (arbitraryText, arbitraryNonEmpty)
1213
import GraphQL.Value
1314
( Object
15+
, Value'(ValueObject')
1416
, ObjectField'(..)
1517
, astToVariableValue
1618
, unionObjects
1719
, objectFields
1820
, objectFromList
1921
, toValue
2022
)
21-
import GraphQL.Internal.Value.FromValue (prop_roundtripValue)
23+
import GraphQL.Internal.Value.FromValue (FromValue(..), prop_roundtripValue)
2224

25+
data Resource = Resource
26+
{ resText :: Text
27+
, resInt :: Int32
28+
, resDouble :: Double
29+
, resBool :: Bool
30+
} deriving (Generic, Eq, Show)
31+
32+
instance FromValue Resource
2333

2434
tests :: IO TestTree
2535
tests = testSpec "Value" $ do
@@ -47,6 +57,23 @@ tests = testSpec "Value" $ do
4757
describe "Objects" $ do
4858
prop "have unique fields" $ do
4959
prop_fieldsUnique
60+
-- See https://github.com/haskell-graphql/graphql-api/pull/178 for background
61+
it "derives fromValue instances for objects with more than three fields" $ do
62+
let Just value = objectFromList
63+
[ ("resText", toValue @Text "text")
64+
, ("resBool", toValue @Bool False)
65+
, ("resDouble", toValue @Double 1.2)
66+
, ("resInt", toValue @Int32 32)
67+
]
68+
let Right observed = fromValue $ ValueObject' value
69+
let expected = Resource
70+
{ resText = "text"
71+
, resInt = 32
72+
, resDouble = 1.2
73+
, resBool = False
74+
}
75+
observed `shouldBe` expected
76+
5077
describe "ToValue / FromValue instances" $ do
5178
prop "Bool" $ prop_roundtripValue @Bool
5279
prop "Int32" $ prop_roundtripValue @Int32

0 commit comments

Comments
 (0)