Skip to content

Commit

Permalink
simplify SQL generated for _eq and _neq operators in GraphQL API (#1466)
Browse files Browse the repository at this point in the history
  • Loading branch information
rakeshkky authored and 0x777 committed Jan 24, 2019
1 parent 8342a96 commit ae63ed9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
8 changes: 4 additions & 4 deletions server/src-lib/Hasura/GraphQL/Resolve/BoolExp.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ parseOpExps
parseOpExps annVal = do
opExpsM <- flip withObjectM annVal $ \nt objM -> forM objM $ \obj ->
forM (OMap.toList obj) $ \(k, v) -> case k of
"_eq" -> fmap AEQ <$> asPGColValM v
"_ne" -> fmap ANE <$> asPGColValM v
"_neq" -> fmap ANE <$> asPGColValM v
"_eq" -> fmap (AEQ True) <$> asPGColValM v
"_ne" -> fmap (ANE True) <$> asPGColValM v
"_neq" -> fmap (ANE True) <$> asPGColValM v
"_is_null" -> resolveIsNull v

"_in" -> fmap (AIN . catMaybes) <$> parseMany asPGColValM v
Expand Down Expand Up @@ -92,7 +92,7 @@ parseAsEqOp
:: (MonadError QErr m)
=> AnnGValue -> m [OpExp]
parseAsEqOp annVal = do
annValOpExp <- AEQ <$> asPGColVal annVal
annValOpExp <- AEQ True <$> asPGColVal annVal
return [annValOpExp]

parseColExp
Expand Down
2 changes: 1 addition & 1 deletion server/src-lib/Hasura/GraphQL/Resolve/Insert.hs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ mkBoolExp tn colInfoVals =
mapM (fmap BoolFld . uncurry f) colInfoVals
where
f ci@(PGColInfo _ colTy _) colVal =
AVCol ci . pure . AEQ <$> prepare (colTy, colVal)
AVCol ci . pure . AEQ True <$> prepare (colTy, colVal)

mkSelQ :: MonadError QErr m => QualifiedTable
-> [PGColInfo] -> [PGColWithValue] -> m InsWithExp
Expand Down
12 changes: 7 additions & 5 deletions server/src-lib/Hasura/RQL/GBoolExp.hs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ parseOpExp parser fim (PGColInfo cn colTy _) (opStr, val) = case opStr of

x -> throw400 UnexpectedPayload $ "Unknown operator : " <> x
where
parseEq = AEQ <$> parseOne -- equals
parseNe = ANE <$> parseOne -- <>
parseEq = AEQ False <$> parseOne -- equals
parseNe = ANE False <$> parseOne -- <>
parseIn = AIN <$> parseMany -- in an array
parseNin = ANIN <$> parseMany -- not in an array
parseGt = AGT <$> parseOne -- >
Expand Down Expand Up @@ -142,7 +142,7 @@ parseOpExps
-> m [OpExpG a]
parseOpExps valParser cim colInfo = \case
(Object o) -> mapM (parseOpExp valParser cim colInfo)(M.toList o)
val -> pure . AEQ <$> valParser (pgiType colInfo) val
val -> pure . AEQ False <$> valParser (pgiType colInfo) val

type ValueParser m a = PGColType -> Value -> m a

Expand Down Expand Up @@ -257,8 +257,10 @@ txtRHSBuilder ty val =
mkColCompExp
:: S.Qual -> PGCol -> OpExpG S.SQLExp -> S.BoolExp
mkColCompExp qual lhsCol = \case
AEQ val -> equalsBoolExpBuilder lhs val
ANE val -> notEqualsBoolExpBuilder lhs val
AEQ False val -> equalsBoolExpBuilder lhs val
AEQ True val -> S.BECompare S.SEQ lhs val
ANE False val -> notEqualsBoolExpBuilder lhs val
ANE True val -> S.BECompare S.SNE lhs val
AIN vals -> handleEmptyIn vals
ANIN vals -> S.BENot $ handleEmptyIn vals
AGT val -> S.BECompare S.SGT lhs val
Expand Down
8 changes: 4 additions & 4 deletions server/src-lib/Hasura/RQL/Types/BoolExp.hs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ foldBoolExp f (BoolFld ce) =
f ce

data OpExpG a
= AEQ !a
| ANE !a
= AEQ !Bool !a
| ANE !Bool !a

| AIN ![a]
| ANIN ![a]
Expand Down Expand Up @@ -140,8 +140,8 @@ data OpExpG a

opExpToJPair :: (a -> Value) -> OpExpG a -> (Text, Value)
opExpToJPair f = \case
AEQ a -> ("_eq", f a)
ANE a -> ("_ne", f a)
AEQ _ a -> ("_eq", f a)
ANE _ a -> ("_ne", f a)

AIN a -> ("_in", toJSON $ map f a)
ANIN a -> ("_nin", toJSON $ map f a)
Expand Down

0 comments on commit ae63ed9

Please sign in to comment.