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
|
|
|
ConfigHeader,
|
2022-06-02 08:22:44 +03:00
|
|
|
SourceNameHeader,
|
|
|
|
SourceName,
|
2022-05-05 08:18:43 +03:00
|
|
|
openApiSchemaJson,
|
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 Data.Aeson qualified as J
|
2022-04-01 04:20:23 +03:00
|
|
|
import Data.Data (Proxy (..))
|
2022-05-05 08:18:43 +03:00
|
|
|
import Data.OpenApi (OpenApi)
|
2022-06-02 08:22:44 +03:00
|
|
|
import Data.Text (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
|
|
|
|
|
2022-06-03 11:06:31 +03:00
|
|
|
type CapabilitiesApi =
|
|
|
|
"capabilities"
|
|
|
|
:> Get '[JSON] V0.CapabilitiesResponse
|
2022-05-05 08:18:43 +03:00
|
|
|
|
2022-04-01 04:20:23 +03:00
|
|
|
type SchemaApi =
|
|
|
|
"schema"
|
2022-06-02 08:22:44 +03:00
|
|
|
:> SourceNameHeader
|
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-06-02 08:22:44 +03:00
|
|
|
:> SourceNameHeader
|
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-05 08:18:43 +03:00
|
|
|
type ConfigHeader = Header' '[Required, Strict] "X-Hasura-DataConnector-Config" V0.Config
|
2022-05-02 02:01:11 +03:00
|
|
|
|
2022-06-02 08:22:44 +03:00
|
|
|
type SourceNameHeader = Header' '[Required, Strict] "X-Hasura-DataConnector-SourceName" SourceName
|
|
|
|
|
|
|
|
type SourceName = Text
|
|
|
|
|
2022-04-01 04:20:23 +03:00
|
|
|
data Routes mode = Routes
|
2022-06-03 11:06:31 +03:00
|
|
|
{ -- | 'GET /capabilities'
|
|
|
|
_capabilities :: mode :- CapabilitiesApi,
|
2022-05-05 08:18:43 +03:00
|
|
|
-- | 'GET /schema'
|
2022-04-01 04:20:23 +03:00
|
|
|
_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@
|
2022-06-03 11:06:31 +03:00
|
|
|
type Api = CapabilitiesApi :<|> SchemaApi :<|> QueryApi
|
2022-04-01 04:20:23 +03:00
|
|
|
|
|
|
|
-- | Provide an OpenApi 3.0 schema for the API
|
|
|
|
openApiSchema :: OpenApi
|
2022-06-03 11:06:31 +03:00
|
|
|
openApiSchema = toOpenApi (Proxy @Api)
|
2022-04-10 07:47:15 +03:00
|
|
|
|
2022-05-05 08:18:43 +03:00
|
|
|
-- | The OpenAPI 3.0 schema for the API
|
|
|
|
--
|
|
|
|
-- This is not exposed as the 'OpenApi' type because we need to do some hackery in
|
|
|
|
-- the serialized JSON to work around some limitations in the openapi3 library
|
|
|
|
openApiSchemaJson :: J.Value
|
|
|
|
openApiSchemaJson = V0.fixExternalSchemaRefsInComponentSchemas $ J.toJSON openApiSchema
|
|
|
|
|
2022-04-10 07:47:15 +03:00
|
|
|
apiClient :: Client ClientM (NamedRoutes Routes)
|
|
|
|
apiClient =
|
|
|
|
client (Proxy @(NamedRoutes Routes))
|