Skip to content

Commit

Permalink
Dmoverton/schemaless capability
Browse files Browse the repository at this point in the history
PR-URL: hasura/graphql-engine-mono#9667
GitOrigin-RevId: 41b3928e976703633437ef14624ec00fb0a8498d
  • Loading branch information
dmoverton authored and hasura-bot committed Jun 27, 2023
1 parent b11203f commit 37560dd
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 17 deletions.
2 changes: 1 addition & 1 deletion dc-agents/dc-api-types/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hasura/dc-api-types",
"version": "0.33.0",
"version": "0.34.0",
"description": "Hasura GraphQL Engine Data Connector Agent API types",
"author": "Hasura (https://github.com/hasura/graphql-engine)",
"license": "Apache-2.0",
Expand Down
5 changes: 5 additions & 0 deletions dc-agents/dc-api-types/src/agent.openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,11 @@
"default": false,
"description": "Whether tables can have primary keys",
"type": "boolean"
},
"supports_schemaless_tables": {
"default": false,
"description": "Whether the database supports tables with no defined schema",
"type": "boolean"
}
},
"type": "object"
Expand Down
4 changes: 4 additions & 0 deletions dc-agents/dc-api-types/src/models/DataSchemaCapabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@ export type DataSchemaCapabilities = {
* Whether tables can have primary keys
*/
supports_primary_keys?: boolean;
/**
* Whether the database supports tables with no defined schema
*/
supports_schemaless_tables?: boolean;
};

10 changes: 5 additions & 5 deletions dc-agents/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dc-agents/reference/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dc-agents/reference/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"dependencies": {
"@fastify/cors": "^8.1.0",
"@hasura/dc-api-types": "0.33.0",
"@hasura/dc-api-types": "0.34.0",
"fastify": "^4.13.0",
"mathjs": "^11.0.0",
"pino-pretty": "^8.0.0",
Expand Down
4 changes: 2 additions & 2 deletions dc-agents/sqlite/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dc-agents/sqlite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"dependencies": {
"@fastify/cors": "^8.1.0",
"@hasura/dc-api-types": "0.33.0",
"@hasura/dc-api-types": "0.34.0",
"fastify-metrics": "^9.2.1",
"fastify": "^4.13.0",
"nanoid": "^3.3.4",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module Hasura.Backends.DataConnector.API.V0.Capabilities
dscSupportsPrimaryKeys,
dscSupportsForeignKeys,
dscColumnNullability,
dscSupportsSchemalessTables,
defaultDataSchemaCapabilities,
ColumnNullability (..),
QueryCapabilities (..),
Expand Down Expand Up @@ -136,15 +137,16 @@ instance HasCodec Capabilities where
data DataSchemaCapabilities = DataSchemaCapabilities
{ _dscSupportsPrimaryKeys :: Bool,
_dscSupportsForeignKeys :: Bool,
_dscColumnNullability :: ColumnNullability
_dscColumnNullability :: ColumnNullability,
_dscSupportsSchemalessTables :: Bool
}
deriving stock (Eq, Ord, Show, Generic, Data)
deriving anyclass (NFData, Hashable)
deriving (FromJSON, ToJSON, ToSchema) via Autodocodec DataSchemaCapabilities

defaultDataSchemaCapabilities :: DataSchemaCapabilities
defaultDataSchemaCapabilities =
DataSchemaCapabilities False False NullableAndNonNullableColumns
DataSchemaCapabilities False False NullableAndNonNullableColumns False

instance HasCodec DataSchemaCapabilities where
codec =
Expand All @@ -153,6 +155,7 @@ instance HasCodec DataSchemaCapabilities where
<$> optionalFieldWithOmittedDefault "supports_primary_keys" (_dscSupportsPrimaryKeys defaultDataSchemaCapabilities) "Whether tables can have primary keys" .= _dscSupportsPrimaryKeys
<*> optionalFieldWithOmittedDefault "supports_foreign_keys" (_dscSupportsForeignKeys defaultDataSchemaCapabilities) "Whether tables can have foreign keys" .= _dscSupportsForeignKeys
<*> optionalFieldWithOmittedDefault "column_nullability" (_dscColumnNullability defaultDataSchemaCapabilities) "The sort of column nullability that is supported" .= _dscColumnNullability
<*> optionalFieldWithOmittedDefault "supports_schemaless_tables" (_dscSupportsSchemalessTables defaultDataSchemaCapabilities) "Whether the database supports tables with no defined schema" .= _dscSupportsSchemalessTables

data ColumnNullability
= OnlyNullableColumns
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Hasura.Backends.DataConnector.Adapter.Backend
)
where

import Control.Lens (view)
import Data.Aeson qualified as J
import Data.Aeson.Extended (ToJSONKeyValue (..))
import Data.Aeson.Key (fromText)
Expand Down Expand Up @@ -167,6 +168,9 @@ instance Backend 'DataConnector where

backendSupportsNestedObjects = pure ()

sourceSupportsSchemalessTables =
view $ DC.scCapabilities . API.cDataSchema . API.dscSupportsSchemalessTables

instance HasSourceConfiguration 'DataConnector where
type SourceConfig 'DataConnector = DC.SourceConfig
type SourceConnConfiguration 'DataConnector = DC.ConnSourceConfig
Expand Down
3 changes: 3 additions & 0 deletions server/src-lib/Hasura/RQL/Types/Backend.hs
Original file line number Diff line number Diff line change
Expand Up @@ -425,5 +425,8 @@ class
default backendSupportsNestedObjects :: (XNestedObjects b ~ XDisable) => Either QErr (XNestedObjects b)
backendSupportsNestedObjects = throw400 InvalidConfiguration "Nested objects not supported"

sourceSupportsSchemalessTables :: SourceConfig b -> Bool
sourceSupportsSchemalessTables = const False

-- Prisms
$(makePrisms ''ComputedFieldReturnType)
15 changes: 12 additions & 3 deletions server/src-lib/Hasura/Table/API.hs
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ buildTableCache = Inc.cache proc (source, sourceConfig, dbTablesMeta, tableBuild
-<
err400 NotExists $ "no such table/view exists in source: " <>> _tbiName table
Just metadataTable ->
buildRawTableInfo -< (table, metadataTable, sourceConfig, reloadMetadataInvalidationKey, logicalModels)
buildRawTableInfo -< (source, table, metadataTable, sourceConfig, reloadMetadataInvalidationKey, logicalModels)
)
|)
(mkTableMetadataObject source tableName)
Expand Down Expand Up @@ -761,14 +761,15 @@ buildTableCache = Inc.cache proc (source, sourceConfig, dbTablesMeta, tableBuild
ErrorA
QErr
arr
( TableBuildInput b,
( SourceName,
TableBuildInput b,
DBTableMetadata b,
SourceConfig b,
Inc.Dependency Inc.InvalidationKey,
LogicalModels b
)
(TableCoreInfoG b (RawColumnInfo b) (Column b))
buildRawTableInfo = Inc.cache proc (tableBuildInput, metadataTable, sourceConfig, reloadMetadataInvalidationKey, logicalModels) -> do
buildRawTableInfo = Inc.cache proc (sourceName, tableBuildInput, metadataTable, sourceConfig, reloadMetadataInvalidationKey, logicalModels) -> do
let TableBuildInput name isEnum config apolloFedConfig mLogicalModelName = tableBuildInput
columns <-
liftEitherA
Expand All @@ -778,6 +779,14 @@ buildTableCache = Inc.cache proc (source, sourceConfig, dbTablesMeta, tableBuild
pure $ _ptmiColumns metadataTable
Just logicalModelName -> do
-- A logical model was specified: use columns from the logical model
--
-- If the source does not support schemaless tables then we throw an error.
-- This is not strictly necessary - the logical model could be used even with sources
-- that always provide a table schema. For now, we want to limit this functionality to
-- databases such as MongoDB that don't always provide a schema. In future we may want
-- to relax this.
unless (sourceSupportsSchemalessTables @b sourceConfig)
$ throw400 InvalidConfiguration ("The source " <> sourceName <<> " does not support schemaless tables")
logicalModel <-
InsOrdHashMap.lookup logicalModelName logicalModels
`onNothing` throw400 InvalidConfiguration ("The logical mode " <> logicalModelName <<> " could not be found")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ genDataSchemaCapabilities =
<$> Gen.bool
<*> Gen.bool
<*> genColumnNullability
<*> Gen.bool

genColumnNullability :: (MonadGen m) => m ColumnNullability
genColumnNullability =
Expand Down

0 comments on commit 37560dd

Please sign in to comment.