2021-08-24 19:25:12 +03:00
|
|
|
module Hasura.GraphQL.Transport.WSServerApp
|
|
|
|
( createWSServerApp,
|
|
|
|
stopWSServerApp,
|
|
|
|
createWSServerEnv,
|
|
|
|
)
|
|
|
|
where
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2021-08-24 19:25:12 +03:00
|
|
|
import Control.Concurrent.Async.Lifted.Safe qualified as LA
|
|
|
|
import Control.Concurrent.STM qualified as STM
|
|
|
|
import Control.Exception.Lifted
|
|
|
|
import Control.Monad.Trans.Control qualified as MC
|
2021-11-04 15:38:57 +03:00
|
|
|
import Data.Aeson (object, toJSON, (.=))
|
2021-08-24 19:25:12 +03:00
|
|
|
import Data.ByteString.Char8 qualified as B (pack)
|
2022-10-13 12:32:33 +03:00
|
|
|
import Data.Text (pack)
|
2023-03-17 13:29:07 +03:00
|
|
|
import Hasura.App.State
|
2023-04-05 11:57:19 +03:00
|
|
|
import Hasura.Backends.DataConnector.Agent.Client (AgentLicenseKey)
|
|
|
|
import Hasura.CredentialCache
|
2021-08-24 19:25:12 +03:00
|
|
|
import Hasura.GraphQL.Execute qualified as E
|
|
|
|
import Hasura.GraphQL.Logging
|
|
|
|
import Hasura.GraphQL.Transport.HTTP (MonadExecuteQuery)
|
|
|
|
import Hasura.GraphQL.Transport.Instances ()
|
|
|
|
import Hasura.GraphQL.Transport.WebSocket
|
|
|
|
import Hasura.GraphQL.Transport.WebSocket.Protocol
|
|
|
|
import Hasura.GraphQL.Transport.WebSocket.Server qualified as WS
|
|
|
|
import Hasura.GraphQL.Transport.WebSocket.Types
|
|
|
|
import Hasura.Logging qualified as L
|
|
|
|
import Hasura.Metadata.Class
|
|
|
|
import Hasura.Prelude
|
2023-03-31 00:18:11 +03:00
|
|
|
import Hasura.QueryTags
|
2022-04-27 16:57:28 +03:00
|
|
|
import Hasura.RQL.Types.SchemaCache
|
2023-03-17 13:29:07 +03:00
|
|
|
import Hasura.Server.AppStateRef
|
2023-03-30 19:31:50 +03:00
|
|
|
import Hasura.Server.Auth (UserAuthentication)
|
2021-08-24 19:25:12 +03:00
|
|
|
import Hasura.Server.Init.Config
|
2023-03-30 19:31:50 +03:00
|
|
|
( WSConnectionInitTimeout,
|
2021-08-24 19:25:12 +03:00
|
|
|
)
|
2021-09-29 19:20:06 +03:00
|
|
|
import Hasura.Server.Limits
|
2021-08-24 19:25:12 +03:00
|
|
|
import Hasura.Server.Metrics (ServerMetrics (..))
|
2022-07-24 00:18:01 +03:00
|
|
|
import Hasura.Server.Prometheus
|
|
|
|
( PrometheusMetrics (..),
|
|
|
|
decWebsocketConnections,
|
|
|
|
incWebsocketConnections,
|
|
|
|
)
|
2023-05-18 15:55:53 +03:00
|
|
|
import Hasura.Server.Types (MonadGetPolicies (..))
|
harmonize network manager handling
## Description
### I want to speak to the `Manager`
Oh boy. This PR is both fairly straightforward and overreaching, so let's break it down.
For most network access, we need a [`HTTP.Manager`](https://hackage.haskell.org/package/http-client-0.1.0.0/docs/Network-HTTP-Client-Manager.html). It is created only once, at the top level, when starting the engine, and is then threaded through the application to wherever we need to make a network call. As of main, the way we do this is not standardized: most of the GraphQL execution code passes it "manually" as a function argument throughout the code. We also have a custom monad constraint, `HasHttpManagerM`, that describes a monad's ability to provide a manager. And, finally, several parts of the code store the manager in some kind of argument structure, such as `RunT`'s `RunCtx`.
This PR's first goal is to harmonize all of this: we always create the manager at the root, and we already have it when we do our very first `runReaderT`. Wouldn't it make sense for the rest of the code to not manually pass it anywhere, to not store it anywhere, but to always rely on the current monad providing it? This is, in short, what this PR does: it implements a constraint on the base monads, so that they provide the manager, and removes most explicit passing from the code.
### First come, first served
One way this PR goes a tiny bit further than "just" doing the aforementioned harmonization is that it starts the process of implementing the "Services oriented architecture" roughly outlined in this [draft document](https://docs.google.com/document/d/1FAigqrST0juU1WcT4HIxJxe1iEBwTuBZodTaeUvsKqQ/edit?usp=sharing). Instead of using the existing `HasHTTPManagerM`, this PR revamps it into the `ProvidesNetwork` service.
The idea is, again, that we should make all "external" dependencies of the engine, all things that the core of the engine doesn't care about, a "service". This allows us to define clear APIs for features, to choose different implementations based on which version of the engine we're running, harmonizes our many scattered monadic constraints... Which is why this service is called "Network": we can refine it, moving forward, to be the constraint that defines how all network communication is to operate, instead of relying on disparate classes constraint or hardcoded decisions. A comment in the code clarifies this intent.
### Side-effects? In my Haskell?
This PR also unavoidably touches some other aspects of the codebase. One such example: it introduces `Hasura.App.AppContext`, named after `HasuraPro.Context.AppContext`: a name for the reader structure at the base level. It also transforms `Handler` from a type alias to a newtype, as `Handler` is where we actually enforce HTTP limits; but without `Handler` being a distinct type, any code path could simply do a `runExceptT $ runReader` and forget to enforce them.
(As a rule of thumb, i am starting to consider any straggling `runReaderT` or `runExceptT` as a code smell: we should not stack / unstack monads haphazardly, and every layer should be an opaque `newtype` with a corresponding run function.)
## Further work
In several places, i have left TODOs when i have encountered things that suggest that we should do further unrelated cleanups. I'll write down the follow-up steps, either in the aforementioned document or on slack. But, in short, at a glance, in approximate order, we could:
- delete `ExecutionCtx` as it is only a subset of `ServerCtx`, and remove one more `runReaderT` call
- delete `ServerConfigCtx` as it is only a subset of `ServerCtx`, and remove it from `RunCtx`
- remove `ServerCtx` from `HandlerCtx`, and make it part of `AppContext`, or even make it the `AppContext` altogether (since, at least for the OSS version, `AppContext` is there again only a subset)
- remove `CacheBuildParams` and `CacheBuild` altogether, as they're just a distinct stack that is a `ReaderT` on top of `IO` that contains, you guessed it, the same thing as `ServerCtx`
- move `RunT` out of `RQL.Types` and rename it, since after the previous cleanups **it only contains `UserInfo`**; it could be bundled with the authentication service, made a small implementation detail in `Hasura.Server.Auth`
- rename `PGMetadaStorageT` to something a bit more accurate, such as `App`, and enforce its IO base
This would significantly simply our complex stack. From there, or in parallel, we can start moving existing dependencies as Services. For the purpose of supporting read replicas entitlement, we could move `MonadResolveSource` to a `SourceResolver` service, as attempted in #7653, and transform `UserAuthenticationM` into a `Authentication` service.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7736
GitOrigin-RevId: 68cce710eb9e7d752bda1ba0c49541d24df8209f
2023-02-22 18:53:52 +03:00
|
|
|
import Hasura.Services.Network
|
2021-08-24 19:25:12 +03:00
|
|
|
import Hasura.Tracing qualified as Tracing
|
|
|
|
import Network.WebSockets qualified as WS
|
|
|
|
import System.Metrics.Gauge qualified as EKG.Gauge
|
|
|
|
|
|
|
|
createWSServerApp ::
|
2021-10-13 19:38:56 +03:00
|
|
|
( MonadIO m,
|
2021-08-24 19:25:12 +03:00
|
|
|
MC.MonadBaseControl IO m,
|
|
|
|
LA.Forall (LA.Pure m),
|
Rewrite `Tracing` to allow for only one `TraceT` in the entire stack.
This PR is on top of #7789.
### Description
This PR entirely rewrites the API of the Tracing library, to make `interpTraceT` a thing of the past. Before this change, we ran traces by sticking a `TraceT` on top of whatever we were doing. This had several major drawbacks:
- we were carrying a bunch of `TraceT` across the codebase, and the entire codebase had to know about it
- we needed to carry a second class constraint around (`HasReporterM`) to be able to run all of those traces
- we kept having to do stack rewriting with `interpTraceT`, which went from inconvenient to horrible
- we had to declare several behavioral instances on `TraceT m`
This PR rewrite all of `Tracing` using a more conventional model: there is ONE `TraceT` at the bottom of the stack, and there is an associated class constraint `MonadTrace`: any part of the code that happens to satisfy `MonadTrace` is able to create new traces. We NEVER have to do stack rewriting, `interpTraceT` is gone, and `TraceT` and `Reporter` become implementation details that 99% of the code is blissfully unaware of: code that needs to do tracing only needs to declare that the monad in which it operates implements `MonadTrace`.
In doing so, this PR revealed **several bugs in the codebase**: places where we were expecting to trace something, but due to the default instance of `HasReporterM IO` we would actually not do anything. This PR also splits the code of `Tracing` in more byte-sized modules, with the goal of potentially moving to `server/lib` down the line.
### Remaining work
This PR is a draft; what's left to do is:
- [x] make Pro compile; i haven't updated `HasuraPro/Main` yet
- [x] document Tracing by writing a note that explains how to use the library, and the meaning of "reporter", "trace" and "span", as well as the pitfalls
- [x] discuss some of the trade-offs in the implementation, which is why i'm opening this PR already despite it not fully building yet
- [x] it depends on #7789 being merged first
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7791
GitOrigin-RevId: cadd32d039134c93ddbf364599a2f4dd988adea8
2023-03-13 20:37:16 +03:00
|
|
|
UserAuthentication m,
|
2021-08-24 19:25:12 +03:00
|
|
|
E.MonadGQLExecutionCheck m,
|
|
|
|
WS.MonadWSLog m,
|
|
|
|
MonadQueryLog m,
|
2023-03-15 16:05:17 +03:00
|
|
|
MonadExecutionLog m,
|
2021-08-24 19:25:12 +03:00
|
|
|
MonadExecuteQuery m,
|
2023-02-03 04:03:23 +03:00
|
|
|
MonadMetadataStorage m,
|
2023-03-31 00:18:11 +03:00
|
|
|
MonadQueryTags m,
|
harmonize network manager handling
## Description
### I want to speak to the `Manager`
Oh boy. This PR is both fairly straightforward and overreaching, so let's break it down.
For most network access, we need a [`HTTP.Manager`](https://hackage.haskell.org/package/http-client-0.1.0.0/docs/Network-HTTP-Client-Manager.html). It is created only once, at the top level, when starting the engine, and is then threaded through the application to wherever we need to make a network call. As of main, the way we do this is not standardized: most of the GraphQL execution code passes it "manually" as a function argument throughout the code. We also have a custom monad constraint, `HasHttpManagerM`, that describes a monad's ability to provide a manager. And, finally, several parts of the code store the manager in some kind of argument structure, such as `RunT`'s `RunCtx`.
This PR's first goal is to harmonize all of this: we always create the manager at the root, and we already have it when we do our very first `runReaderT`. Wouldn't it make sense for the rest of the code to not manually pass it anywhere, to not store it anywhere, but to always rely on the current monad providing it? This is, in short, what this PR does: it implements a constraint on the base monads, so that they provide the manager, and removes most explicit passing from the code.
### First come, first served
One way this PR goes a tiny bit further than "just" doing the aforementioned harmonization is that it starts the process of implementing the "Services oriented architecture" roughly outlined in this [draft document](https://docs.google.com/document/d/1FAigqrST0juU1WcT4HIxJxe1iEBwTuBZodTaeUvsKqQ/edit?usp=sharing). Instead of using the existing `HasHTTPManagerM`, this PR revamps it into the `ProvidesNetwork` service.
The idea is, again, that we should make all "external" dependencies of the engine, all things that the core of the engine doesn't care about, a "service". This allows us to define clear APIs for features, to choose different implementations based on which version of the engine we're running, harmonizes our many scattered monadic constraints... Which is why this service is called "Network": we can refine it, moving forward, to be the constraint that defines how all network communication is to operate, instead of relying on disparate classes constraint or hardcoded decisions. A comment in the code clarifies this intent.
### Side-effects? In my Haskell?
This PR also unavoidably touches some other aspects of the codebase. One such example: it introduces `Hasura.App.AppContext`, named after `HasuraPro.Context.AppContext`: a name for the reader structure at the base level. It also transforms `Handler` from a type alias to a newtype, as `Handler` is where we actually enforce HTTP limits; but without `Handler` being a distinct type, any code path could simply do a `runExceptT $ runReader` and forget to enforce them.
(As a rule of thumb, i am starting to consider any straggling `runReaderT` or `runExceptT` as a code smell: we should not stack / unstack monads haphazardly, and every layer should be an opaque `newtype` with a corresponding run function.)
## Further work
In several places, i have left TODOs when i have encountered things that suggest that we should do further unrelated cleanups. I'll write down the follow-up steps, either in the aforementioned document or on slack. But, in short, at a glance, in approximate order, we could:
- delete `ExecutionCtx` as it is only a subset of `ServerCtx`, and remove one more `runReaderT` call
- delete `ServerConfigCtx` as it is only a subset of `ServerCtx`, and remove it from `RunCtx`
- remove `ServerCtx` from `HandlerCtx`, and make it part of `AppContext`, or even make it the `AppContext` altogether (since, at least for the OSS version, `AppContext` is there again only a subset)
- remove `CacheBuildParams` and `CacheBuild` altogether, as they're just a distinct stack that is a `ReaderT` on top of `IO` that contains, you guessed it, the same thing as `ServerCtx`
- move `RunT` out of `RQL.Types` and rename it, since after the previous cleanups **it only contains `UserInfo`**; it could be bundled with the authentication service, made a small implementation detail in `Hasura.Server.Auth`
- rename `PGMetadaStorageT` to something a bit more accurate, such as `App`, and enforce its IO base
This would significantly simply our complex stack. From there, or in parallel, we can start moving existing dependencies as Services. For the purpose of supporting read replicas entitlement, we could move `MonadResolveSource` to a `SourceResolver` service, as attempted in #7653, and transform `UserAuthenticationM` into a `Authentication` service.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7736
GitOrigin-RevId: 68cce710eb9e7d752bda1ba0c49541d24df8209f
2023-02-22 18:53:52 +03:00
|
|
|
HasResourceLimits m,
|
Rewrite `Tracing` to allow for only one `TraceT` in the entire stack.
This PR is on top of #7789.
### Description
This PR entirely rewrites the API of the Tracing library, to make `interpTraceT` a thing of the past. Before this change, we ran traces by sticking a `TraceT` on top of whatever we were doing. This had several major drawbacks:
- we were carrying a bunch of `TraceT` across the codebase, and the entire codebase had to know about it
- we needed to carry a second class constraint around (`HasReporterM`) to be able to run all of those traces
- we kept having to do stack rewriting with `interpTraceT`, which went from inconvenient to horrible
- we had to declare several behavioral instances on `TraceT m`
This PR rewrite all of `Tracing` using a more conventional model: there is ONE `TraceT` at the bottom of the stack, and there is an associated class constraint `MonadTrace`: any part of the code that happens to satisfy `MonadTrace` is able to create new traces. We NEVER have to do stack rewriting, `interpTraceT` is gone, and `TraceT` and `Reporter` become implementation details that 99% of the code is blissfully unaware of: code that needs to do tracing only needs to declare that the monad in which it operates implements `MonadTrace`.
In doing so, this PR revealed **several bugs in the codebase**: places where we were expecting to trace something, but due to the default instance of `HasReporterM IO` we would actually not do anything. This PR also splits the code of `Tracing` in more byte-sized modules, with the goal of potentially moving to `server/lib` down the line.
### Remaining work
This PR is a draft; what's left to do is:
- [x] make Pro compile; i haven't updated `HasuraPro/Main` yet
- [x] document Tracing by writing a note that explains how to use the library, and the meaning of "reporter", "trace" and "span", as well as the pitfalls
- [x] discuss some of the trade-offs in the implementation, which is why i'm opening this PR already despite it not fully building yet
- [x] it depends on #7789 being merged first
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7791
GitOrigin-RevId: cadd32d039134c93ddbf364599a2f4dd988adea8
2023-03-13 20:37:16 +03:00
|
|
|
ProvidesNetwork m,
|
2023-05-18 15:55:53 +03:00
|
|
|
Tracing.MonadTrace m,
|
|
|
|
MonadGetPolicies m
|
2021-08-24 19:25:12 +03:00
|
|
|
) =>
|
|
|
|
HashSet (L.EngineLogType L.Hasura) ->
|
2023-03-17 13:29:07 +03:00
|
|
|
WSServerEnv impl ->
|
2021-08-24 19:25:12 +03:00
|
|
|
WSConnectionInitTimeout ->
|
2023-04-05 11:57:19 +03:00
|
|
|
Maybe (CredentialCache AgentLicenseKey) ->
|
2023-03-30 19:31:50 +03:00
|
|
|
-- | aka generalized 'WS.ServerApp'
|
2021-08-24 19:25:12 +03:00
|
|
|
WS.HasuraServerApp m
|
2023-04-05 11:57:19 +03:00
|
|
|
createWSServerApp enabledLogTypes serverEnv connInitTimeout licenseKeyCache = \ !ipAddress !pendingConn -> do
|
2023-03-17 13:29:07 +03:00
|
|
|
let getMetricsConfig = scMetricsConfig <$> getSchemaCache (_wseAppStateRef serverEnv)
|
2022-12-28 06:47:42 +03:00
|
|
|
WS.createServerApp getMetricsConfig connInitTimeout (_wseServer serverEnv) prometheusMetrics handlers ipAddress pendingConn
|
2021-08-24 19:25:12 +03:00
|
|
|
where
|
|
|
|
handlers =
|
|
|
|
WS.WSHandlers
|
|
|
|
onConnHandler
|
|
|
|
onMessageHandler
|
|
|
|
onCloseHandler
|
|
|
|
|
|
|
|
logger = _wseLogger serverEnv
|
|
|
|
serverMetrics = _wseServerMetrics serverEnv
|
2022-07-24 00:18:01 +03:00
|
|
|
prometheusMetrics = _wsePrometheusMetrics serverEnv
|
2021-08-24 19:25:12 +03:00
|
|
|
|
2023-03-30 19:31:50 +03:00
|
|
|
getAuthMode = acAuthMode <$> getAppContext (_wseAppStateRef serverEnv)
|
2021-08-24 19:25:12 +03:00
|
|
|
wsActions = mkWSActions logger
|
|
|
|
|
|
|
|
-- Mask async exceptions during event processing to help maintain integrity of mutable vars:
|
|
|
|
-- here `sp` stands for sub-protocol
|
|
|
|
onConnHandler rid rh ip sp = mask_ do
|
|
|
|
liftIO $ EKG.Gauge.inc $ smWebsocketConnections serverMetrics
|
2022-07-24 00:18:01 +03:00
|
|
|
liftIO $ incWebsocketConnections $ pmConnections prometheusMetrics
|
2021-08-24 19:25:12 +03:00
|
|
|
flip runReaderT serverEnv $ onConn rid rh ip (wsActions sp)
|
|
|
|
|
|
|
|
onMessageHandler conn bs sp =
|
2023-05-24 16:51:56 +03:00
|
|
|
mask_
|
|
|
|
$ onMessage enabledLogTypes getAuthMode serverEnv conn bs (wsActions sp) licenseKeyCache
|
2021-08-24 19:25:12 +03:00
|
|
|
|
|
|
|
onCloseHandler conn = mask_ do
|
2023-05-18 15:55:53 +03:00
|
|
|
granularPrometheusMetricsState <- runGetPrometheusMetricsGranularity
|
2021-08-24 19:25:12 +03:00
|
|
|
liftIO $ EKG.Gauge.dec $ smWebsocketConnections serverMetrics
|
2022-07-24 00:18:01 +03:00
|
|
|
liftIO $ decWebsocketConnections $ pmConnections prometheusMetrics
|
2023-05-18 15:55:53 +03:00
|
|
|
onClose logger serverMetrics prometheusMetrics (_wseSubscriptionState serverEnv) conn granularPrometheusMetricsState
|
2021-08-24 19:25:12 +03:00
|
|
|
|
2023-03-17 13:29:07 +03:00
|
|
|
stopWSServerApp :: WSServerEnv impl -> IO ()
|
2021-08-24 19:25:12 +03:00
|
|
|
stopWSServerApp wsEnv = WS.shutdown (_wseServer wsEnv)
|
|
|
|
|
|
|
|
createWSServerEnv ::
|
2023-03-30 19:31:50 +03:00
|
|
|
( HasAppEnv m,
|
|
|
|
MonadIO m
|
|
|
|
) =>
|
2023-03-17 13:29:07 +03:00
|
|
|
AppStateRef impl ->
|
|
|
|
m (WSServerEnv impl)
|
2023-03-30 19:31:50 +03:00
|
|
|
createWSServerEnv appStateRef = do
|
|
|
|
AppEnv {..} <- askAppEnv
|
|
|
|
let getCorsPolicy = acCorsPolicy <$> getAppContext appStateRef
|
|
|
|
logger = _lsLogger appEnvLoggers
|
|
|
|
|
2023-04-12 13:26:09 +03:00
|
|
|
AppContext {acEnableAllowlist, acAuthMode, acSQLGenCtx, acExperimentalFeatures, acDefaultNamingConvention} <- liftIO $ getAppContext appStateRef
|
2023-03-30 19:31:50 +03:00
|
|
|
allowlist <- liftIO $ scAllowlist <$> getSchemaCache appStateRef
|
|
|
|
corsPolicy <- liftIO getCorsPolicy
|
|
|
|
|
2023-04-12 13:26:09 +03:00
|
|
|
wsServer <- liftIO $ STM.atomically $ WS.createWSServer acAuthMode acEnableAllowlist allowlist corsPolicy acSQLGenCtx acExperimentalFeatures acDefaultNamingConvention logger
|
2023-03-30 19:31:50 +03:00
|
|
|
|
2023-05-24 16:51:56 +03:00
|
|
|
pure
|
|
|
|
$ WSServerEnv
|
2023-03-30 19:31:50 +03:00
|
|
|
(_lsLogger appEnvLoggers)
|
|
|
|
appEnvSubscriptionState
|
|
|
|
appStateRef
|
|
|
|
appEnvManager
|
|
|
|
getCorsPolicy
|
|
|
|
appEnvEnableReadOnlyMode
|
|
|
|
wsServer
|
|
|
|
appEnvWebSocketKeepAlive
|
|
|
|
appEnvServerMetrics
|
|
|
|
appEnvPrometheusMetrics
|
|
|
|
appEnvTraceSamplingPolicy
|
2021-08-24 19:25:12 +03:00
|
|
|
|
|
|
|
mkWSActions :: L.Logger L.Hasura -> WSSubProtocol -> WS.WSActions WSConnData
|
|
|
|
mkWSActions logger subProtocol =
|
|
|
|
WS.WSActions
|
|
|
|
mkPostExecErrMessageAction
|
|
|
|
mkOnErrorMessageAction
|
|
|
|
mkConnectionCloseAction
|
|
|
|
keepAliveAction
|
|
|
|
getServerMsgType
|
|
|
|
mkAcceptRequest
|
2021-11-04 15:38:57 +03:00
|
|
|
fmtErrorMessage
|
2021-08-24 19:25:12 +03:00
|
|
|
where
|
|
|
|
mkPostExecErrMessageAction wsConn opId execErr =
|
|
|
|
sendMsg wsConn $ case subProtocol of
|
|
|
|
Apollo -> SMData $ DataMsg opId $ throwError execErr
|
|
|
|
GraphQLWS -> SMErr $ ErrorMsg opId $ toJSON execErr
|
|
|
|
|
2022-10-13 12:32:33 +03:00
|
|
|
mkOnErrorMessageAction wsConn err mErrMsg =
|
|
|
|
case subProtocol of
|
|
|
|
Apollo ->
|
|
|
|
case mErrMsg of
|
|
|
|
WS.ConnInitFailed -> sendCloseWithMsg logger wsConn (WS.mkWSServerErrorCode mErrMsg err) (Just $ SMConnErr err) Nothing
|
|
|
|
WS.ClientMessageParseFailed -> sendMsg wsConn $ SMConnErr err
|
|
|
|
GraphQLWS -> sendCloseWithMsg logger wsConn (WS.mkWSServerErrorCode mErrMsg err) (Just $ SMConnErr err) Nothing
|
2021-08-24 19:25:12 +03:00
|
|
|
|
|
|
|
mkConnectionCloseAction wsConn opId errMsg =
|
2023-05-24 16:51:56 +03:00
|
|
|
when (subProtocol == GraphQLWS)
|
|
|
|
$ sendCloseWithMsg logger wsConn (GenericError4400 errMsg) (Just . SMErr $ ErrorMsg opId $ toJSON (pack errMsg)) (Just 1000)
|
2021-08-24 19:25:12 +03:00
|
|
|
|
|
|
|
getServerMsgType = case subProtocol of
|
|
|
|
Apollo -> SMData
|
|
|
|
GraphQLWS -> SMNext
|
|
|
|
|
2023-05-24 16:51:56 +03:00
|
|
|
keepAliveAction wsConn = sendMsg wsConn
|
|
|
|
$ case subProtocol of
|
2021-08-24 19:25:12 +03:00
|
|
|
Apollo -> SMConnKeepAlive
|
|
|
|
GraphQLWS -> SMPing . Just $ keepAliveMessage
|
|
|
|
|
|
|
|
mkAcceptRequest =
|
|
|
|
WS.defaultAcceptRequest
|
|
|
|
{ WS.acceptSubprotocol = Just . B.pack . showSubProtocol $ subProtocol
|
|
|
|
}
|
2021-11-04 15:38:57 +03:00
|
|
|
|
|
|
|
fmtErrorMessage errMsgs = case subProtocol of
|
|
|
|
Apollo -> object ["errors" .= errMsgs]
|
|
|
|
GraphQLWS -> toJSON errMsgs
|