mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-19 13:31:43 +03:00
346804fc67
## Description This change adds support for nested object fields in HGE IR and Schema Cache, the Data Connectors backend and API, and the MongoDB agent. ### Data Connector API changes - The `/schema` endpoint response now includes an optional set of GraphQL type definitions. Table column types can refer to these definitions by name. - Queries can now include a new field type `object` which contains a column name and a nested query. This allows querying into a nested object within a field. ### MongoDB agent changes - Add support for querying into nested documents using the new `object` field type. ### HGE changes - The `Backend` type class has a new type family `XNestedObjects b` which controls whether or not a backend supports querying into nested objects. This is currently enabled only for the `DataConnector` backend. - For backends that support nested objects, the `FieldInfo` type gets a new constructor `FINestedObject`, and the `AnnFieldG` type gets a new constructor `AFNestedObject`. - If the DC `/schema` endpoint returns any custom GraphQL type definitions they are stored in the `TableInfo` for each table in the source. - During schema cache building, the function `addNonColumnFields` will check whether any column types match custom GraphQL object types stored in the `TableInfo`. If so, they are converted into `FINestedObject` instead of `FIColumn` in the `FieldInfoMap`. - When building the `FieldParser`s from `FieldInfo` (function `fieldSelection`) any `FINestedObject` fields are converted into nested object parsers returning `AFNestedObject`. - The `DataConnector` query planner converts `AFNestedObject` fields into `object` field types in the query sent to the agent. ## Limitations ### HGE not yet implemented: - Support for nested arrays - Support for nested objects/arrays in mutations - Support for nested objects/arrays in order-by - Support for filters (`where`) in nested objects/arrays - Support for adding custom GraphQL types via track table metadata API - Support for interface and union types - Tests for nested objects ### Mongo agent not yet implemented: - Generate nested object types from validation schema - Support for aggregates - Support for order-by - Configure agent port - Build agent in CI - Agent tests for nested objects and MongoDB agent PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7844 GitOrigin-RevId: aec9ec1e4216293286a68f9b1af6f3f5317db423
199 lines
6.7 KiB
Haskell
199 lines
6.7 KiB
Haskell
{-# LANGUAGE TemplateHaskell #-}
|
|
|
|
-- | Metadata related types, functions and helpers.
|
|
--
|
|
-- Provides a single function which loads the MSSQL database metadata.
|
|
-- See the file at src-rsr/mssql/mssql_table_metadata.sql for the SQL we use to build
|
|
-- this metadata.
|
|
-- See 'Hasura.RQL.Types.Table.DBTableMetadata' for the Haskell type we use forall
|
|
-- storing this metadata.
|
|
module Hasura.Backends.MSSQL.Meta
|
|
( loadDBMetadata,
|
|
)
|
|
where
|
|
|
|
import Data.Aeson as Aeson
|
|
import Data.ByteString.UTF8 qualified as BSUTF8
|
|
import Data.FileEmbed (embedFile, makeRelativeToProject)
|
|
import Data.HashMap.Strict qualified as HM
|
|
import Data.HashMap.Strict.NonEmpty qualified as NEHashMap
|
|
import Data.HashSet qualified as HS
|
|
import Data.String
|
|
import Data.Text qualified as T
|
|
import Data.Text.Encoding qualified as T
|
|
import Database.MSSQL.Transaction qualified as Tx
|
|
import Database.ODBC.SQLServer qualified as ODBC
|
|
import Hasura.Backends.MSSQL.Instances.Types ()
|
|
import Hasura.Backends.MSSQL.SQL.Error
|
|
import Hasura.Backends.MSSQL.Types.Internal
|
|
import Hasura.Base.Error
|
|
import Hasura.Prelude
|
|
import Hasura.RQL.Types.Column
|
|
import Hasura.RQL.Types.Common (OID (..))
|
|
import Hasura.RQL.Types.Table
|
|
import Hasura.SQL.Backend
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- * Loader
|
|
|
|
loadDBMetadata :: (MonadIO m) => Tx.TxET QErr m (DBTablesMetadata 'MSSQL)
|
|
loadDBMetadata = do
|
|
let queryBytes = $(makeRelativeToProject "src-rsr/mssql/mssql_table_metadata.sql" >>= embedFile)
|
|
odbcQuery :: ODBC.Query = fromString . BSUTF8.toString $ queryBytes
|
|
sysTablesText <- runIdentity <$> Tx.singleRowQueryE defaultMSSQLTxErrorHandler odbcQuery
|
|
case Aeson.eitherDecodeStrict (T.encodeUtf8 sysTablesText) of
|
|
Left e -> throw500 $ T.pack $ "error loading sql server database schema: " <> e
|
|
Right sysTables -> pure $ HM.fromList $ map transformTable sysTables
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- * Local types
|
|
|
|
data SysTable = SysTable
|
|
{ staName :: Text,
|
|
staObjectId :: Int,
|
|
staJoinedSysColumn :: [SysColumn],
|
|
staJoinedSysSchema :: SysSchema,
|
|
staJoinedSysPrimaryKey :: Maybe SysPrimaryKey
|
|
}
|
|
deriving (Show, Generic)
|
|
|
|
instance FromJSON SysTable where
|
|
parseJSON = genericParseJSON hasuraJSON
|
|
|
|
newtype SysPrimaryKeyColumn = SysPrimaryKeyColumn
|
|
{spkcName :: Text}
|
|
deriving (Show, Generic)
|
|
|
|
instance FromJSON SysPrimaryKeyColumn where
|
|
parseJSON = genericParseJSON hasuraJSON
|
|
|
|
data SysPrimaryKey = SysPrimaryKey
|
|
{ spkName :: Text,
|
|
spkIndexId :: Int,
|
|
spkColumns :: NESeq SysPrimaryKeyColumn
|
|
}
|
|
deriving (Show, Generic)
|
|
|
|
instance FromJSON SysPrimaryKey where
|
|
parseJSON = genericParseJSON hasuraJSON
|
|
|
|
data SysSchema = SysSchema
|
|
{ ssName :: Text,
|
|
ssSchemaId :: Int
|
|
}
|
|
deriving (Show, Generic)
|
|
|
|
instance FromJSON SysSchema where
|
|
parseJSON = genericParseJSON hasuraJSON
|
|
|
|
data SysColumn = SysColumn
|
|
{ scName :: Text,
|
|
scColumnId :: Int,
|
|
scUserTypeId :: Int,
|
|
scIsNullable :: Bool,
|
|
scIsIdentity :: Bool,
|
|
scIsComputed :: Bool,
|
|
scJoinedSysType :: SysType,
|
|
scJoinedForeignKeyColumns :: [SysForeignKeyColumn]
|
|
}
|
|
deriving (Show, Generic)
|
|
|
|
instance FromJSON SysColumn where
|
|
parseJSON = genericParseJSON hasuraJSON
|
|
|
|
data SysType = SysType
|
|
{ styName :: Text,
|
|
stySchemaId :: Int,
|
|
styUserTypeId :: Int
|
|
}
|
|
deriving (Show, Generic)
|
|
|
|
instance FromJSON SysType where
|
|
parseJSON = genericParseJSON hasuraJSON
|
|
|
|
data SysForeignKeyColumn = SysForeignKeyColumn
|
|
{ sfkcConstraintObjectId :: Int,
|
|
sfkcConstraintColumnId :: Int,
|
|
sfkcParentObjectId :: Int,
|
|
sfkcParentColumnId :: Int,
|
|
sfkcReferencedObjectId :: Int,
|
|
sfkcReferencedColumnId :: Int,
|
|
sfkcJoinedReferencedTableName :: Text,
|
|
sfkcJoinedReferencedColumnName :: Text,
|
|
sfkcJoinedReferencedSysSchema :: SysSchema
|
|
}
|
|
deriving (Show, Generic)
|
|
|
|
instance FromJSON SysForeignKeyColumn where
|
|
parseJSON = genericParseJSON hasuraJSON
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- * Transform
|
|
|
|
transformTable :: SysTable -> (TableName, DBTableMetadata 'MSSQL)
|
|
transformTable tableInfo =
|
|
let schemaName = SchemaName $ ssName $ staJoinedSysSchema tableInfo
|
|
tableName = TableName (staName tableInfo) schemaName
|
|
tableOID = OID $ staObjectId tableInfo
|
|
(columns, foreignKeys) = unzip $ transformColumn <$> staJoinedSysColumn tableInfo
|
|
foreignKeysMetadata = HS.fromList $ map ForeignKeyMetadata $ coalesceKeys $ concat foreignKeys
|
|
primaryKey = transformPrimaryKey <$> staJoinedSysPrimaryKey tableInfo
|
|
identityColumns =
|
|
map (ColumnName . scName) $
|
|
filter scIsIdentity $
|
|
staJoinedSysColumn tableInfo
|
|
in ( tableName,
|
|
DBTableMetadata
|
|
tableOID
|
|
columns
|
|
primaryKey
|
|
HS.empty -- no unique constraints?
|
|
foreignKeysMetadata
|
|
Nothing -- no views, only tables
|
|
Nothing -- no description
|
|
identityColumns
|
|
mempty
|
|
)
|
|
|
|
transformColumn ::
|
|
SysColumn ->
|
|
(RawColumnInfo 'MSSQL, [ForeignKey 'MSSQL])
|
|
transformColumn sysCol =
|
|
let rciName = ColumnName $ scName sysCol
|
|
rciPosition = scColumnId sysCol
|
|
|
|
rciIsNullable = scIsNullable sysCol
|
|
rciDescription = Nothing
|
|
rciType = parseScalarType $ styName $ scJoinedSysType sysCol
|
|
foreignKeys =
|
|
scJoinedForeignKeyColumns sysCol <&> \foreignKeyColumn ->
|
|
let _fkConstraint = Constraint (ConstraintName "fk_mssql") $ OID $ sfkcConstraintObjectId foreignKeyColumn
|
|
|
|
schemaName = SchemaName $ ssName $ sfkcJoinedReferencedSysSchema foreignKeyColumn
|
|
_fkForeignTable = TableName (sfkcJoinedReferencedTableName foreignKeyColumn) schemaName
|
|
_fkColumnMapping = NEHashMap.singleton rciName $ ColumnName $ sfkcJoinedReferencedColumnName foreignKeyColumn
|
|
in ForeignKey {..}
|
|
|
|
colIsImmutable = scIsComputed sysCol || scIsIdentity sysCol
|
|
rciMutability = ColumnMutability {_cmIsInsertable = not colIsImmutable, _cmIsUpdatable = not colIsImmutable}
|
|
in (RawColumnInfo {..}, foreignKeys)
|
|
|
|
transformPrimaryKey :: SysPrimaryKey -> PrimaryKey 'MSSQL (Column 'MSSQL)
|
|
transformPrimaryKey (SysPrimaryKey {..}) =
|
|
let constraint = Constraint (ConstraintName spkName) $ OID spkIndexId
|
|
columns = (ColumnName . spkcName) <$> spkColumns
|
|
in PrimaryKey constraint columns
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- * Helpers
|
|
|
|
coalesceKeys :: [ForeignKey 'MSSQL] -> [ForeignKey 'MSSQL]
|
|
coalesceKeys = HM.elems . foldl' coalesce HM.empty
|
|
where
|
|
coalesce mapping fk@(ForeignKey constraint tableName _) = HM.insertWith combine (constraint, tableName) fk mapping
|
|
combine oldFK newFK = oldFK {_fkColumnMapping = (NEHashMap.union `on` _fkColumnMapping) oldFK newFK}
|