Update error messages for enum and union support

This commit is contained in:
Eugen Wissner 2024-07-17 09:37:29 +02:00
parent aa28bdd7fe
commit 16bcdca066
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0

View File

@ -322,7 +322,7 @@ deriveFromGraphQL typeName = do
NewtypeD _ _ _ _ cons' _ NewtypeD _ _ _ _ cons' _
| RecC dataConName varBangTypes <- cons' -> | RecC dataConName varBangTypes <- cons' ->
withRecordConstructor dataConName varBangTypes withRecordConstructor dataConName varBangTypes
_ -> error "Only records with a single data constructor are supported" _ -> error "Only input objects and enums are supported if all member types have a FromGraphQL instance"
where where
enumMemberPattern (NormalC normalName []) = enumMemberPattern (NormalC normalName []) =
let fromGraphQLF = conP (mkName "Type.Enum") [litP $ stringL $ nameBase normalName] let fromGraphQLF = conP (mkName "Type.Enum") [litP $ stringL $ nameBase normalName]
@ -376,7 +376,7 @@ deriveToGraphQL typeName = do
NewtypeD _ _ _ _ cons' _ NewtypeD _ _ _ _ cons' _
| RecC dataConName varBangTypes <- cons' -> | RecC dataConName varBangTypes <- cons' ->
withRecordConstructor dataConName varBangTypes withRecordConstructor dataConName varBangTypes
_ -> error "Only records with a single data constructor are supported" _ -> error "Only objects, unions and enums are supported if all member types have a ToGraphQL instance"
where where
conTName = conT typeName conTName = conT typeName
collectEnumMemberNames (NormalC normalName []) = Just normalName collectEnumMemberNames (NormalC normalName []) = Just normalName
@ -397,7 +397,7 @@ deriveToGraphQL typeName = do
enumMemberPattern <$> enumMemberNames enumMemberPattern <$> enumMemberNames
| Just unionMembers <- traverse collectUnionMembers cons' = | Just unionMembers <- traverse collectUnionMembers cons' =
unionMemberPattern <$> unionMembers unionMemberPattern <$> unionMembers
| otherwise = error "Enum member should be a normal constructor without parameters" | otherwise = error "All data constructors should have either no parameters (Enum) or one parameter (Union)"
withRecordConstructor dataConName varBangTypes = do withRecordConstructor dataConName varBangTypes = do
fieldAliases <- traverse newFieldAliases varBangTypes fieldAliases <- traverse newFieldAliases varBangTypes
let fBody = let fBody =