Skip to content

Commit

Permalink
wip: graphql transactions over websockets
Browse files Browse the repository at this point in the history
  • Loading branch information
rakeshkky committed Dec 18, 2019
1 parent 6944f42 commit 9462484
Show file tree
Hide file tree
Showing 14 changed files with 1,054 additions and 532 deletions.
8 changes: 7 additions & 1 deletion server/graphql-engine.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,13 @@ library

, Hasura.GraphQL.Transport.HTTP.Protocol
, Hasura.GraphQL.Transport.HTTP
, Hasura.GraphQL.Transport.WebSocket.Protocol
, Hasura.GraphQL.Transport.WebSocket.Queries.Protocol
, Hasura.GraphQL.Transport.WebSocket.Queries.Types
, Hasura.GraphQL.Transport.WebSocket.Queries.Handlers
, Hasura.GraphQL.Transport.WebSocket.Transaction.Protocol
, Hasura.GraphQL.Transport.WebSocket.Transaction.Types
, Hasura.GraphQL.Transport.WebSocket.Transaction.Handlers
, Hasura.GraphQL.Transport.WebSocket.Common
, Hasura.GraphQL.Transport.WebSocket.Server
, Hasura.GraphQL.Transport.WebSocket
, Hasura.GraphQL.Schema.BoolExp
Expand Down
24 changes: 23 additions & 1 deletion server/src-lib/Hasura/Db.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ module Hasura.Db
, PGExecCtx(..)
, runLazyTx
, runLazyTx'
, runLazyTxWithConn
, beginTx
, abortTx
, commitTx
, withUserInfo

, RespTx
Expand All @@ -19,6 +23,7 @@ import Control.Lens
import Control.Monad.Validate

import qualified Data.Aeson.Extended as J
import qualified Data.Text as T
import qualified Database.PG.Query as Q
import qualified Database.PG.Query.Connection as Q

Expand Down Expand Up @@ -79,6 +84,23 @@ runLazyTx' (PGExecCtx pgPool _) = \case
LTNoTx a -> return a
LTTx tx -> Q.runTx' pgPool tx

runLazyTxWithConn :: MonadIO m => Q.PGConn -> LazyTx QErr a -> ExceptT QErr m a
runLazyTxWithConn pgConn = \case
LTErr e -> throwError e
LTNoTx a -> pure a
LTTx tx -> ExceptT <$> liftIO $ runExceptT $ Q.runTxWithConn pgConn tx

beginTx :: Q.TxIsolation -> LazyTx QErr ()
beginTx txIso = liftTx $ Q.unitQE defaultTxErrorHandler query () False
where
query = Q.fromText $ T.pack $ "BEGIN " <> show txIso

abortTx :: LazyTx QErr ()
abortTx = liftTx $ Q.unitQE defaultTxErrorHandler "ABORT" () False

commitTx :: LazyTx QErr ()
commitTx = liftTx $ Q.unitQE defaultTxErrorHandler "COMMIT" () False

type RespTx = Q.TxE QErr EncJSON
type LazyRespTx = LazyTx QErr EncJSON

Expand Down Expand Up @@ -118,7 +140,7 @@ mkTxErrorHandler isExpectedError txe = fromMaybe unexpectedError expectedError

PGDataException code -> case code of
Just (PGErrorSpecific PGInvalidEscapeSequence) -> (BadRequest, message)
_ -> (DataException, message)
_ -> (DataException, message)

PGSyntaxErrorOrAccessRuleViolation code -> (ConstraintError,) $ case code of
Just (PGErrorSpecific PGInvalidColumnReference) ->
Expand Down
Loading

0 comments on commit 9462484

Please sign in to comment.