haskell-graphql / graphql-api

Write type-safe GraphQL services in Haskell
BSD 3-Clause "New" or "Revised" License
406 stars 35 forks source link

Simplify Value related function #208

Closed zhujinxuan closed 5 years ago

zhujinxuan commented 5 years ago

I have not yet go through all codes, but it seems boilerpolate like

value :: AST.Value -> Text
value (AST.ValueVariable x _) = variable x
-- TODO: This will be replaced with `decimal` Buidler
value (AST.ValueInt      x _) = pack $ show x
-- TODO: This will be replaced with `decimal` Buidler
value (AST.ValueFloat    x _) = pack $ show x
value (AST.ValueBoolean  x _) = booleanValue x
value (AST.ValueString   x _) = stringValue x

can be simplified via

converter :: (Int -> r ) -> (Float -> r) -> (Boolean -> r) -> (value -> r) -> value -> r
converter f _ _ .. (ValueInt v ..) = ValueInt (f v) ....
value = converter ....

It seems we can use this converter in many places related to AST.Value or Internal.Value