2019-08-28 15:19:21 +03:00
|
|
|
module Hasura.GraphQL.Execute.LiveQuery.Options
|
2021-09-24 01:56:37 +03:00
|
|
|
( LiveQueriesOptions (..),
|
|
|
|
BatchSize,
|
|
|
|
unBatchSize,
|
|
|
|
RefetchInterval,
|
|
|
|
unRefetchInterval,
|
|
|
|
mkLiveQueriesOptions,
|
|
|
|
mkBatchSize,
|
|
|
|
mkRefetchInterval,
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Data.Aeson qualified as J
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.Types.Common
|
|
|
|
|
|
|
|
data LiveQueriesOptions = LiveQueriesOptions
|
|
|
|
{ _lqoBatchSize :: !BatchSize,
|
|
|
|
_lqoRefetchInterval :: !RefetchInterval
|
|
|
|
}
|
|
|
|
deriving (Show, Eq)
|
2019-08-28 15:19:21 +03:00
|
|
|
|
|
|
|
mkLiveQueriesOptions :: Maybe BatchSize -> Maybe RefetchInterval -> LiveQueriesOptions
|
2021-09-24 01:56:37 +03:00
|
|
|
mkLiveQueriesOptions batchSize refetchInterval =
|
|
|
|
LiveQueriesOptions
|
|
|
|
{ _lqoBatchSize = fromMaybe (BatchSize 100) batchSize,
|
|
|
|
_lqoRefetchInterval = fromMaybe (RefetchInterval 1) refetchInterval
|
|
|
|
}
|
2019-08-28 15:19:21 +03:00
|
|
|
|
|
|
|
instance J.ToJSON LiveQueriesOptions where
|
|
|
|
toJSON (LiveQueriesOptions 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
|
|
|
|
2020-06-03 02:31:50 +03:00
|
|
|
instance J.FromJSON LiveQueriesOptions where
|
|
|
|
parseJSON = J.withObject "live query options" \o ->
|
|
|
|
LiveQueriesOptions <$> o J..: "batch_size"
|
2021-09-24 01:56:37 +03:00
|
|
|
<*> o J..: "refetch_delay"
|
2020-06-03 02:31:50 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
newtype BatchSize = BatchSize {unBatchSize :: NonNegativeInt}
|
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
|
|
|
|
mkBatchSize x = BatchSize <$> mkNonNegativeInt x
|
|
|
|
|
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
|
2021-09-24 01:56:37 +03:00
|
|
|
newtype RefetchInterval = RefetchInterval {unRefetchInterval :: NonNegativeDiffTime}
|
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
|
|
|
|
mkRefetchInterval x = RefetchInterval <$> mkNonNegativeDiffTime x
|