Skip to content

Commit 6cb738d

Browse files
Victor NawothnigVictor Nawothnig
authored andcommitted
Properly recurse into (:*:) for HasAnnotatedInputType
1 parent 120c8b7 commit 6cb738d

File tree

4 files changed

+31
-27
lines changed

4 files changed

+31
-27
lines changed

src/GraphQL/Internal/API.hs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ module GraphQL.Internal.API
3535
, getAnnotatedInputType
3636
) where
3737

38-
import Protolude hiding (Enum, TypeError)
38+
import Protolude hiding (Enum, TypeError, (<>))
3939

40+
import Data.Semigroup ((<>))
4041
import qualified Data.List.NonEmpty as NonEmpty
4142
import GHC.Generics ((:*:)(..))
4243
import GHC.TypeLits (Symbol, KnownSymbol, TypeError, ErrorMessage(..))
@@ -358,12 +359,12 @@ class GenericAnnotatedInputType (f :: Type -> Type) where
358359
class GenericInputObjectFieldDefinitions (f :: Type -> Type) where
359360
genericGetInputObjectFieldDefinitions :: Either SchemaError (NonEmpty Schema.InputObjectFieldDefinition)
360361

361-
instance forall dataName consName records s l p.
362+
instance forall dataName consName records m p f.
362363
( KnownSymbol dataName
363364
, KnownSymbol consName
364365
, GenericInputObjectFieldDefinitions records
365-
) => GenericAnnotatedInputType (D1 ('MetaData dataName s l 'False)
366-
(C1 ('MetaCons consName p 'True) records
366+
) => GenericAnnotatedInputType (D1 ('MetaData dataName m p 'False)
367+
(C1 ('MetaCons consName f 'True) records
367368
)) where
368369
genericGetAnnotatedInputType = do
369370
name <- nameFromSymbol @dataName
@@ -374,17 +375,14 @@ instance forall dataName consName records s l p.
374375
. Schema.InputObjectTypeDefinition name
375376
) (genericGetInputObjectFieldDefinitions @records)
376377

377-
instance forall wrappedType fieldName rest u s l.
378-
( KnownSymbol fieldName
379-
, HasAnnotatedInputType wrappedType
380-
, GenericInputObjectFieldDefinitions rest
381-
) => GenericInputObjectFieldDefinitions (S1 ('MetaSel ('Just fieldName) u s l) (Rec0 wrappedType) :*: rest) where
378+
instance forall l r.
379+
( GenericInputObjectFieldDefinitions l
380+
, GenericInputObjectFieldDefinitions r
381+
) => GenericInputObjectFieldDefinitions (l :*: r) where
382382
genericGetInputObjectFieldDefinitions = do
383-
name <- nameFromSymbol @fieldName
384-
annotatedInputType <- getAnnotatedInputType @wrappedType
385-
let l = Schema.InputObjectFieldDefinition name annotatedInputType Nothing
386-
r <- genericGetInputObjectFieldDefinitions @rest
387-
pure (NonEmpty.cons l r)
383+
l <- genericGetInputObjectFieldDefinitions @l
384+
r <- genericGetInputObjectFieldDefinitions @r
385+
pure (l <> r)
388386

389387
instance forall wrappedType fieldName u s l.
390388
( KnownSymbol fieldName

src/GraphQL/Internal/API/Enum.hs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ instance forall left right.
6464
genericEnumToValue (L1 gv) = genericEnumToValue gv
6565
genericEnumToValue (R1 gv) = genericEnumToValue gv
6666

67-
instance forall conName p b. (KnownSymbol conName) => GenericEnumValues (C1 ('MetaCons conName p b) U1) where
67+
instance forall conName f s.
68+
( KnownSymbol conName
69+
) => GenericEnumValues (C1 ('MetaCons conName f s) U1) where
6870
genericEnumValues = let name = nameFromSymbol @conName in [name]
6971
genericEnumFromValue vname =
7072
case nameFromSymbol @conName of
@@ -86,18 +88,18 @@ instance forall conName p b. (KnownSymbol conName) => GenericEnumValues (C1 ('Me
8688

8789
-- TODO(tom): better type errors using `n`. Also type errors for other
8890
-- invalid constructors.
89-
instance forall conName p b sa sb.
91+
instance forall conName f s sa sb.
9092
( TypeError ('Text "Constructor not unary: " ':<>: 'Text conName)
9193
, KnownSymbol conName
92-
) => GenericEnumValues (C1 ('MetaCons conName p b) (S1 sa sb)) where
94+
) => GenericEnumValues (C1 ('MetaCons conName f s) (S1 sa sb)) where
9395
genericEnumValues = nonUnaryConstructorError
9496
genericEnumFromValue = nonUnaryConstructorError
9597
genericEnumToValue = nonUnaryConstructorError
9698

97-
instance forall conName p b sa sb f.
99+
instance forall conName f s sa sb r.
98100
( TypeError ('Text "Constructor not unary: " ':<>: 'Text conName)
99101
, KnownSymbol conName
100-
) => GenericEnumValues (C1 ('MetaCons conName p b) (S1 sa sb) :+: f) where
102+
) => GenericEnumValues (C1 ('MetaCons conName f s) (S1 sa sb) :+: r) where
101103
genericEnumValues = nonUnaryConstructorError
102104
genericEnumFromValue = nonUnaryConstructorError
103105
genericEnumToValue = nonUnaryConstructorError

src/GraphQL/Internal/Value/FromValue.hs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ wrongType expected value = throwError ("Wrong type, should be: `" <> expected <>
9191
class GenericFromValue (f :: Type -> Type) where
9292
genericFromValue :: Object' ConstScalar -> Either Text (f p)
9393

94-
instance forall dataName consName records s l p.
94+
instance forall dataName consName records m p f.
9595
( KnownSymbol dataName
9696
, KnownSymbol consName
9797
, GenericFromValue records
98-
) => GenericFromValue (D1 ('MetaData dataName s l 'False)
99-
(C1 ('MetaCons consName p 'True) records
98+
) => GenericFromValue (D1 ('MetaData dataName m p 'False)
99+
(C1 ('MetaCons consName f 'True) records
100100
)) where
101101
genericFromValue o = M1 . M1 <$> genericFromValue @records o
102102

@@ -108,8 +108,10 @@ instance forall l r.
108108
genericFromValue object = liftA2 (:*:) (genericFromValue @l object) (genericFromValue @r object)
109109

110110
-- | Look up a single record field element in the Object.
111-
getValue :: forall wrappedType fieldName u s l p. (FromValue wrappedType, KnownSymbol fieldName)
112-
=> Object' ConstScalar -> Either Text ((S1 ('MetaSel ('Just fieldName) u s l) (Rec0 wrappedType)) p)
111+
getValue :: forall wrappedType fieldName u s l p.
112+
( FromValue wrappedType
113+
, KnownSymbol fieldName
114+
) => Object' ConstScalar -> Either Text ((S1 ('MetaSel ('Just fieldName) u s l) (Rec0 wrappedType)) p)
113115
getValue (Object' fieldMap) = do
114116
fieldName <- case nameFromSymbol @fieldName of
115117
Left err -> throwError ("invalid field name" <> show err)

tests/ValueTests.hs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Test.Tasty.Hspec (testSpec, describe, it, shouldBe, shouldSatisfy)
1010

1111
import qualified GraphQL.Internal.Syntax.AST as AST
1212
import GraphQL.Internal.Arbitrary (arbitraryText, arbitraryNonEmpty)
13+
import GraphQL.API (HasAnnotatedInputType)
1314
import GraphQL.Value
1415
( Object
1516
, Value'(ValueObject')
@@ -29,6 +30,7 @@ data Resource = Resource
2930
, resBool :: Bool
3031
} deriving (Generic, Eq, Show)
3132

33+
instance HasAnnotatedInputType
3234
instance FromValue Resource
3335

3436
tests :: IO TestTree
@@ -59,7 +61,7 @@ tests = testSpec "Value" $ do
5961
prop_fieldsUnique
6062
-- See https://github.com/haskell-graphql/graphql-api/pull/178 for background
6163
it "derives fromValue instances for objects with more than three fields" $ do
62-
let Just value = objectFromList
64+
let Just value = objectFromList
6365
[ ("resText", toValue @Text "text")
6466
, ("resBool", toValue @Bool False)
6567
, ("resDouble", toValue @Double 1.2)
@@ -70,10 +72,10 @@ tests = testSpec "Value" $ do
7072
{ resText = "text"
7173
, resInt = 32
7274
, resDouble = 1.2
73-
, resBool = False
75+
, resBool = False
7476
}
7577
observed `shouldBe` expected
76-
78+
7779
describe "ToValue / FromValue instances" $ do
7880
prop "Bool" $ prop_roundtripValue @Bool
7981
prop "Int32" $ prop_roundtripValue @Int32

0 commit comments

Comments
 (0)