Repository: hasura/graphql-engine
File: server/src-lib/Hasura/GraphQL/Transport/WebSocket.hs (Haskell)

Code before:
      logOpEv ODStarted (Just requestId)

  -- case execPlan of
  --   E.GExPHasura resolvedOp ->
  --     runHasuraGQ timerTot telemCacheHit requestId q userInfo resolvedOp
  --   E.GExPRemote rsi opDef  ->
  --     runRemoteGQ timerTot telemCacheHit execCtx requestId userInfo reqHdrs opDef rsi
  where
    telemTransport = Telem.WebSocket
    execQueryOrMut
      :: ExceptT () m DiffTime
      -> Telem.QueryType
      -> Telem.CacheHit
      -> Maybe EQ.GeneratedSqlMap
      -> RequestId
      -> ExceptT QErr (ExceptT () m) EncJSON
      -> ExceptT () m EncJSON
    execQueryOrMut timerTot telemQueryType telemCacheHit genSql requestId action = do
      let telemLocality = Telem.Local
      logOpEv ODStarted (Just requestId)
      -- log the generated SQL and the graphql query
      logQueryLog logger q genSql requestId
      result <- withElapsedTime (runExceptT action) >>= \case
        (_,      Left err) -> do
          postExecErr requestId err
          throwError ()
        (telemTimeIO_DT, Right encJson) -> do
          -- Telemetry. NOTE: don't time network IO:
          telemTimeTot <- Seconds <$> timerTot
          sendSuccResp encJson $ LQ.LiveQueryMetadata telemTimeIO_DT
          let telemTimeIO = convertDuration telemTimeIO_DT
          Telem.recordTimingMetric Telem.RequestDimensions{..} Telem.RequestTimings{..}
          pure encJson
      sendCompleted (Just requestId)
      pure result

{-
    runHasuraGQ :: ExceptT () m DiffTime
                -> Telem.CacheHit -> RequestId -> GQLReqUnparsed -> UserInfo -> E.ExecOp
                -> ExceptT () m ()
    runHasuraGQ timerTot telemCacheHit reqId query userInfo = \case
      E.ExOpQuery opTx genSql _asts ->
        execQueryOrMut Telem.Query genSql $ runQueryTx pgExecCtx opTx
    E.GExPHasura resolvedOp ->
      runHasuraGQ timerTot telemCacheHit requestId q reqParsed userInfo resolvedOp
    E.GExPRemote rsi opDef  ->
      runRemoteGQ timerTot telemCacheHit execCtx requestId userInfo reqHdrs opDef rsi
  where
    telemTransport = Telem.WebSocket
    runHasuraGQ
      :: ExceptT () m DiffTime

Review comment (performance):
The `completed` message has to be sent even when there is an error. So `throwError` shouldn't be called, Instead the type of this function can be `ExceptT () m (Maybe EncJSON)`? i.e, only the successful response is cached. 

Code after:
      liftIO $ $assertNFHere $! (lqId, opName)  -- so we don't write thunks to mutable vars
#endif
      liftIO $ STM.atomically $
        -- NOTE: see crucial `lookup` check above, ensuring this doesn't clobber:
        STMMap.insert (lqId, opName) opId opMap
      logOpEv ODStarted (Just requestId)

  -- case execPlan of
  --   E.GExPHasura resolvedOp ->
  --     runHasuraGQ timerTot telemCacheHit requestId q userInfo resolvedOp
  --   E.GExPRemote rsi opDef  ->
  --     runRemoteGQ timerTot telemCacheHit execCtx requestId userInfo reqHdrs opDef rsi
  where
    telemTransport = Telem.WebSocket
    execQueryOrMut
      :: ExceptT () m DiffTime
      -> Telem.QueryType
      -> Telem.CacheHit
      -> Maybe EQ.GeneratedSqlMap
      -> RequestId
      -> ExceptT QErr (ExceptT () m) EncJSON
      -> ExceptT () m (Maybe EncJSON)
    execQueryOrMut timerTot telemQueryType telemCacheHit genSql requestId action = do
      let telemLocality = Telem.Local
      logOpEv ODStarted (Just requestId)
      -- log the generated SQL and the graphql query
      logQueryLog logger q genSql requestId
      result <- withElapsedTime (runExceptT action) >>= \case
        (_,      Left err) -> do
          postExecErr requestId err
          pure Nothing
        (telemTimeIO_DT, Right encJson) -> do
          -- Telemetry. NOTE: don't time network IO:
          telemTimeTot <- Seconds <$> timerTot
          sendSuccResp encJson $ LQ.LiveQueryMetadata telemTimeIO_DT
          let telemTimeIO = convertDuration telemTimeIO_DT
          Telem.recordTimingMetric Telem.RequestDimensions{..} Telem.RequestTimings{..}
          pure $ Just encJson
      sendCompleted (Just requestId)
      pure result

{-
    runHasuraGQ :: ExceptT () m DiffTime
                -> Telem.CacheHit -> RequestId -> GQLReqUnparsed -> UserInfo -> E.ExecOp
                -> ExceptT () m ()
    runHasuraGQ timerTot telemCacheHit reqId query userInfo = \case
      E.ExOpQuery opTx genSql _asts ->
        execQueryOrMut Telem.Query genSql $ runQueryTx pgExecCtx opTx
    E.GExPHasura resolvedOp ->
      runHasuraGQ timerTot telemCacheHit requestId q reqParsed userInfo resolvedOp
    E.GExPRemote rsi opDef  ->