2022-04-01 04:20:23 +03:00
|
|
|
--
|
2022-05-02 08:03:12 +03:00
|
|
|
module Hasura.Backends.DataConnector.API
|
2022-04-01 04:20:23 +03:00
|
|
|
( module V0,
|
|
|
|
Api,
|
|
|
|
SchemaApi,
|
|
|
|
QueryApi,
|
2022-05-02 02:01:11 +03:00
|
|
|
Config (..),
|
|
|
|
ConfigHeader,
|
2022-04-01 04:20:23 +03:00
|
|
|
openApiSchema,
|
2022-04-10 07:47:15 +03:00
|
|
|
Routes (..),
|
|
|
|
apiClient,
|
2022-04-01 04:20:23 +03:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
2022-05-02 02:01:11 +03:00
|
|
|
import Control.DeepSeq (NFData)
|
|
|
|
import Data.Aeson (FromJSON, ToJSON)
|
|
|
|
import Data.Aeson qualified as J
|
|
|
|
import Data.ByteString.Lazy qualified as BSL
|
2022-04-01 04:20:23 +03:00
|
|
|
import Data.Data (Proxy (..))
|
2022-05-02 02:01:11 +03:00
|
|
|
import Data.Hashable (Hashable)
|
|
|
|
import Data.OpenApi (AdditionalProperties (..), OpenApi, OpenApiType (..), Schema (..), ToParamSchema (..))
|
|
|
|
import Data.Text.Encoding qualified as Text
|
2022-05-02 08:03:12 +03:00
|
|
|
import Hasura.Backends.DataConnector.API.V0.API as V0
|
2022-04-01 04:20:23 +03:00
|
|
|
import Servant.API
|
|
|
|
import Servant.API.Generic
|
2022-04-10 07:47:15 +03:00
|
|
|
import Servant.Client (Client, ClientM, client)
|
2022-04-01 04:20:23 +03:00
|
|
|
import Servant.OpenApi
|
2022-05-02 02:01:11 +03:00
|
|
|
import Prelude
|
2022-04-01 04:20:23 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Servant Routes
|
|
|
|
|
|
|
|
type SchemaApi =
|
|
|
|
"schema"
|
2022-05-02 02:01:11 +03:00
|
|
|
:> ConfigHeader
|
2022-04-01 04:20:23 +03:00
|
|
|
:> Get '[JSON] V0.SchemaResponse
|
|
|
|
|
|
|
|
type QueryApi =
|
|
|
|
"query"
|
2022-05-02 02:01:11 +03:00
|
|
|
:> ConfigHeader
|
2022-04-01 04:20:23 +03:00
|
|
|
:> ReqBody '[JSON] V0.Query
|
|
|
|
:> Post '[JSON] V0.QueryResponse
|
|
|
|
|
2022-05-02 02:01:11 +03:00
|
|
|
newtype Config = Config {unConfig :: J.Object}
|
|
|
|
deriving stock (Eq, Show, Ord)
|
|
|
|
deriving newtype (Hashable, NFData, ToJSON, FromJSON)
|
|
|
|
|
|
|
|
instance ToHttpApiData Config where
|
|
|
|
toUrlPiece (Config val) = Text.decodeUtf8 . BSL.toStrict $ J.encode val
|
|
|
|
toHeader (Config val) = BSL.toStrict $ J.encode val
|
|
|
|
|
|
|
|
instance ToParamSchema Config where
|
|
|
|
toParamSchema _ =
|
|
|
|
mempty
|
|
|
|
{ _schemaType = Just OpenApiObject,
|
|
|
|
_schemaNullable = Just False,
|
|
|
|
_schemaAdditionalProperties = Just (AdditionalPropertiesAllowed True)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ConfigHeader = Header' '[Required, Strict] "X-Hasura-DataConnector-Config" Config
|
|
|
|
|
2022-04-01 04:20:23 +03:00
|
|
|
data Routes mode = Routes
|
|
|
|
{ -- | 'GET /schema'
|
|
|
|
_schema :: mode :- SchemaApi,
|
|
|
|
-- | 'POST /query'
|
|
|
|
_query :: mode :- QueryApi
|
|
|
|
}
|
|
|
|
deriving stock (Generic)
|
|
|
|
|
|
|
|
-- | servant-openapi3 does not (yet) support NamedRoutes so we need to compose the
|
|
|
|
-- API the old-fashioned way using :<|> for use by @toOpenApi@
|
|
|
|
type Api = SchemaApi :<|> QueryApi
|
|
|
|
|
|
|
|
-- | Provide an OpenApi 3.0 schema for the API
|
|
|
|
openApiSchema :: OpenApi
|
|
|
|
openApiSchema = toOpenApi (Proxy :: Proxy Api)
|
2022-04-10 07:47:15 +03:00
|
|
|
|
|
|
|
apiClient :: Client ClientM (NamedRoutes Routes)
|
|
|
|
apiClient =
|
|
|
|
client (Proxy @(NamedRoutes Routes))
|