2022-09-21 21:01:48 +03:00
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
|
|
|
|
2022-03-21 13:39:49 +03:00
|
|
|
module Hasura.GraphQL.Execute.Subscription.Options
|
|
|
|
( SubscriptionsOptions (..),
|
|
|
|
LiveQueriesOptions,
|
2022-04-07 17:41:43 +03:00
|
|
|
StreamQueriesOptions,
|
2022-07-19 01:12:24 +03:00
|
|
|
BatchSize (..),
|
|
|
|
RefetchInterval (..),
|
2022-03-21 13:39:49 +03:00
|
|
|
mkSubscriptionsOptions,
|
2021-09-24 01:56:37 +03:00
|
|
|
mkBatchSize,
|
|
|
|
mkRefetchInterval,
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Data.Aeson qualified as J
|
2022-09-21 21:01:48 +03:00
|
|
|
import Hasura.Base.Instances ()
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.Prelude
|
2022-09-21 21:01:48 +03:00
|
|
|
import Refined (NonNegative, Refined, refineFail, refineTH)
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2022-03-21 13:39:49 +03:00
|
|
|
data SubscriptionsOptions = SubscriptionsOptions
|
2021-09-24 01:56:37 +03:00
|
|
|
{ _lqoBatchSize :: !BatchSize,
|
|
|
|
_lqoRefetchInterval :: !RefetchInterval
|
|
|
|
}
|
|
|
|
deriving (Show, Eq)
|
2019-08-28 15:19:21 +03:00
|
|
|
|
2022-03-21 13:39:49 +03:00
|
|
|
type LiveQueriesOptions = SubscriptionsOptions
|
|
|
|
|
2022-04-07 17:41:43 +03:00
|
|
|
type StreamQueriesOptions = SubscriptionsOptions
|
|
|
|
|
2022-03-21 13:39:49 +03:00
|
|
|
mkSubscriptionsOptions :: Maybe BatchSize -> Maybe RefetchInterval -> SubscriptionsOptions
|
|
|
|
mkSubscriptionsOptions batchSize refetchInterval =
|
|
|
|
SubscriptionsOptions
|
2022-09-21 21:01:48 +03:00
|
|
|
{ _lqoBatchSize = fromMaybe (BatchSize $$(refineTH 100)) batchSize,
|
|
|
|
_lqoRefetchInterval = fromMaybe (RefetchInterval $$(refineTH 1)) refetchInterval
|
2021-09-24 01:56:37 +03:00
|
|
|
}
|
2019-08-28 15:19:21 +03:00
|
|
|
|
2022-03-21 13:39:49 +03:00
|
|
|
instance J.ToJSON SubscriptionsOptions where
|
|
|
|
toJSON (SubscriptionsOptions batchSize refetchInterval) =
|
2021-09-24 01:56:37 +03:00
|
|
|
J.object
|
|
|
|
[ "batch_size" J..= batchSize,
|
|
|
|
"refetch_delay" J..= refetchInterval
|
|
|
|
]
|
2019-08-28 15:19:21 +03:00
|
|
|
|
2022-03-21 13:39:49 +03:00
|
|
|
instance J.FromJSON SubscriptionsOptions where
|
2020-06-03 02:31:50 +03:00
|
|
|
parseJSON = J.withObject "live query options" \o ->
|
2022-11-02 23:53:23 +03:00
|
|
|
SubscriptionsOptions
|
|
|
|
<$> o J..: "batch_size"
|
2021-09-24 01:56:37 +03:00
|
|
|
<*> o J..: "refetch_delay"
|
2020-06-03 02:31:50 +03:00
|
|
|
|
2022-09-21 21:01:48 +03:00
|
|
|
newtype BatchSize = BatchSize {unBatchSize :: Refined NonNegative Int}
|
2020-06-03 02:31:50 +03:00
|
|
|
deriving (Show, Eq, J.ToJSON, J.FromJSON)
|
2019-08-28 15:19:21 +03:00
|
|
|
|
2020-09-17 13:56:41 +03:00
|
|
|
mkBatchSize :: Int -> Maybe BatchSize
|
2022-09-21 21:01:48 +03:00
|
|
|
mkBatchSize x = BatchSize <$> refineFail x
|
2020-09-17 13:56:41 +03:00
|
|
|
|
2020-07-14 22:00:58 +03:00
|
|
|
-- TODO this is treated as milliseconds in fromEnv and as seconds in ToJSON.
|
2020-01-16 04:56:57 +03:00
|
|
|
-- ideally this would have e.g. ... unRefetchInterval :: Milliseconds
|
2022-09-21 21:01:48 +03:00
|
|
|
newtype RefetchInterval = RefetchInterval {unRefetchInterval :: Refined NonNegative DiffTime}
|
2020-06-03 02:31:50 +03:00
|
|
|
deriving (Show, Eq, J.ToJSON, J.FromJSON)
|
2020-09-17 13:56:41 +03:00
|
|
|
|
|
|
|
mkRefetchInterval :: DiffTime -> Maybe RefetchInterval
|
2022-09-21 21:01:48 +03:00
|
|
|
mkRefetchInterval x = RefetchInterval <$> refineFail x
|