graphql-engine/server/src-lib/Hasura/GraphQL/Execute/Subscription/Options.hs
Samir Talwar 9c4792c417 server: Shorten the init specs.
Makes the init specs shorter by using `shouldBe` instead of `shouldSatisfy` wherever possible. This also makes test failures more expressive.

It also simplifies boolean logic in most places, following HLint warnings. These changes brought to you by `hlint --refactor`, which is basically magic.

I have left some redundancy in the boolean logic for clarity, along with the appropriate HLint suppressions.

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5087
GitOrigin-RevId: 52bf3626be2615e6a32a0fc0e8be19cca31ee4ad
2022-07-18 22:13:31 +00:00

59 lines
1.8 KiB
Haskell

module Hasura.GraphQL.Execute.Subscription.Options
( SubscriptionsOptions (..),
LiveQueriesOptions,
StreamQueriesOptions,
BatchSize (..),
RefetchInterval (..),
mkSubscriptionsOptions,
mkBatchSize,
mkRefetchInterval,
)
where
import Data.Aeson qualified as J
import Hasura.Prelude
import Hasura.RQL.Types.Common
data SubscriptionsOptions = SubscriptionsOptions
{ _lqoBatchSize :: !BatchSize,
_lqoRefetchInterval :: !RefetchInterval
}
deriving (Show, Eq)
type LiveQueriesOptions = SubscriptionsOptions
type StreamQueriesOptions = SubscriptionsOptions
mkSubscriptionsOptions :: Maybe BatchSize -> Maybe RefetchInterval -> SubscriptionsOptions
mkSubscriptionsOptions batchSize refetchInterval =
SubscriptionsOptions
{ _lqoBatchSize = fromMaybe (BatchSize 100) batchSize,
_lqoRefetchInterval = fromMaybe (RefetchInterval 1) refetchInterval
}
instance J.ToJSON SubscriptionsOptions where
toJSON (SubscriptionsOptions batchSize refetchInterval) =
J.object
[ "batch_size" J..= batchSize,
"refetch_delay" J..= refetchInterval
]
instance J.FromJSON SubscriptionsOptions where
parseJSON = J.withObject "live query options" \o ->
SubscriptionsOptions <$> o J..: "batch_size"
<*> o J..: "refetch_delay"
newtype BatchSize = BatchSize {unBatchSize :: NonNegativeInt}
deriving (Show, Eq, J.ToJSON, J.FromJSON)
mkBatchSize :: Int -> Maybe BatchSize
mkBatchSize x = BatchSize <$> mkNonNegativeInt x
-- TODO this is treated as milliseconds in fromEnv and as seconds in ToJSON.
-- ideally this would have e.g. ... unRefetchInterval :: Milliseconds
newtype RefetchInterval = RefetchInterval {unRefetchInterval :: NonNegativeDiffTime}
deriving (Show, Eq, J.ToJSON, J.FromJSON)
mkRefetchInterval :: DiffTime -> Maybe RefetchInterval
mkRefetchInterval x = RefetchInterval <$> mkNonNegativeDiffTime x