graphql-engine/server/src-lib/Hasura/GraphQL/Transport/HTTP/Protocol.hs
Vamshi Surabhi ce243f5899
multiplexed subscriptions (#1934)
* add types to represent unparsed http gql requests

This will help when we add caching of frequently used ASTs

* query plan caching

* move livequery to execute

* add multiplexed module

* session variable can be customised depending on the context

Previously the value was always "current_setting('hasura.user')"

* get rid of typemap requirement in reusable plan

* subscriptions are multiplexed when possible

* use lazytx for introspection to avoid acquiring a pg connection

* refactor to make execute a completely decoupled module

* don't issue a transaction for a query

* don't use current setting for explained sql

* move postgres related types to a different module

* validate variableValues on postgres before multiplexing subs

* don't user current_setting for queries over ws

* plan_cache is only visible when developer flag is enabled

* introduce 'batch size' when multiplexing subscriptions

* bump stackage to 13.16

* fix schema_stitching test case error code

* store hashes instead of actual responses for subscriptions

* internal api to dump subscriptions state

* remove PlanCache from SchemaCacheRef

* allow live query options to be configured on server startup

* capture metrics for multiplexed subscriptions

* more metrics captured for multiplexed subs

* switch to tvar based hashmap for faster snapshotting

* livequery modules do not expose internal details

* fix typo in live query env vars

* switch to hasura's pg-client-hs
2019-04-17 15:18:41 +05:30

101 lines
2.9 KiB
Haskell

module Hasura.GraphQL.Transport.HTTP.Protocol
( GQLReq(..)
, GQLReqUnparsed
, GQLReqParsed
, toParsed
, GQLQueryText
, GQLExecDoc(..)
, OperationName(..)
, VariableValues
, encodeGQErr
, encodeGQResp
, GQResp(..)
, isExecError
) where
import Hasura.EncJSON
import Hasura.GraphQL.Utils
import Hasura.Prelude
import Hasura.RQL.Types
import qualified Data.Aeson as J
import qualified Data.Aeson.Casing as J
import qualified Data.Aeson.TH as J
import qualified Data.ByteString.Lazy as BL
import qualified Data.HashMap.Strict as Map
import qualified Language.GraphQL.Draft.Parser as G
import qualified Language.GraphQL.Draft.Syntax as G
newtype GQLExecDoc
= GQLExecDoc { unGQLExecDoc :: [G.ExecutableDefinition] }
deriving (Ord, Show, Eq, Hashable)
instance J.FromJSON GQLExecDoc where
parseJSON = J.withText "GQLExecDoc" $ \t ->
case G.parseExecutableDoc t of
Left _ -> fail "parsing the graphql query failed"
Right q -> return $ GQLExecDoc $ G.getExecutableDefinitions q
instance J.ToJSON GQLExecDoc where
-- TODO, add pretty printer in graphql-parser
toJSON _ = J.String "toJSON not implemented for GQLExecDoc"
newtype OperationName
= OperationName { _unOperationName :: G.Name }
deriving (Ord, Show, Eq, Hashable, J.ToJSON)
instance J.FromJSON OperationName where
parseJSON v = OperationName . G.Name <$> J.parseJSON v
type VariableValues = Map.HashMap G.Variable J.Value
data GQLReq a
= GQLReq
{ _grOperationName :: !(Maybe OperationName)
, _grQuery :: !a
, _grVariables :: !(Maybe VariableValues)
} deriving (Show, Eq, Generic)
$(J.deriveJSON (J.aesonDrop 3 J.camelCase){J.omitNothingFields=True}
''GQLReq
)
instance (Hashable a) => Hashable (GQLReq a)
newtype GQLQueryText
= GQLQueryText
{ _unGQLQueryText :: Text
} deriving (Show, Eq, J.FromJSON, J.ToJSON, Hashable)
type GQLReqUnparsed = GQLReq GQLQueryText
type GQLReqParsed = GQLReq GQLExecDoc
toParsed :: (MonadError QErr m ) => GQLReqUnparsed -> m GQLReqParsed
toParsed req = case G.parseExecutableDoc gqlText of
Left _ -> withPathK "query" $ throwVE "not a valid graphql query"
Right a -> return $ req { _grQuery = GQLExecDoc $ G.getExecutableDefinitions a }
where
gqlText = _unGQLQueryText $ _grQuery req
encodeGQErr :: Bool -> QErr -> J.Value
encodeGQErr includeInternal qErr =
J.object [ "errors" J..= [encodeGQLErr includeInternal qErr]]
data GQResp
= GQSuccess !BL.ByteString
| GQPreExecError ![J.Value]
| GQExecError ![J.Value]
deriving (Show, Eq)
isExecError :: GQResp -> Bool
isExecError = \case
GQExecError _ -> True
_ -> False
encodeGQResp :: GQResp -> EncJSON
encodeGQResp gqResp =
encJFromAssocList $ case gqResp of
GQSuccess r -> [("data", encJFromLBS r)]
GQPreExecError e -> [("errors", encJFromJValue e)]
GQExecError e -> [("data", "null"), ("errors", encJFromJValue e)]