graphql-engine/server/src-lib/Hasura/Backends/MySQL/Meta.hs
David Overton 346804fc67 Support nested object fields in DC API and use this to implement nest…
## 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
2023-04-11 01:30:37 +00:00

228 lines
8.5 KiB
Haskell

{-# LANGUAGE TemplateHaskell #-}
module Hasura.Backends.MySQL.Meta
( getMetadata,
)
where
import Control.Exception (throw)
import Data.ByteString.Char8 qualified as B8
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.Sequence.NonEmpty qualified as NESeq
import Data.String (fromString)
import Database.MySQL.Base (Connection)
import Database.MySQL.Base.Types (Field (..))
import Database.MySQL.Simple (Only (Only), query)
import Database.MySQL.Simple.QueryResults (QueryResults (..), convertError)
import Database.MySQL.Simple.Result (Result, ResultError (..), convert)
import Hasura.Backends.MySQL.Instances.Types ()
import Hasura.Backends.MySQL.Types
import Hasura.Prelude
import Hasura.RQL.Types.Column
import Hasura.RQL.Types.Common
import Hasura.RQL.Types.Table
import Hasura.SQL.Backend
import Language.GraphQL.Draft.Syntax qualified as G
getMetadata :: ConnSourceConfig -> Connection -> IO (DBTablesMetadata 'MySQL)
getMetadata ConnSourceConfig {_cscDatabase} scConnection = do
let sql = $(makeRelativeToProject "src-rsr/mysql_table_metadata.sql" >>= embedFile)
results :: [InformationSchema] <- query scConnection (fromString . B8.unpack $ sql) (Only _cscDatabase)
pure (mkMetadata results)
mkMetadata :: [InformationSchema] -> DBTablesMetadata 'MySQL
mkMetadata = foldr mergeMetadata HM.empty
mergeMetadata :: InformationSchema -> DBTablesMetadata 'MySQL -> DBTablesMetadata 'MySQL
mergeMetadata InformationSchema {..} =
HM.insertWith
mergeDBTableMetadata
(TableName {name = isTableName, schema = pure isTableSchema})
$ DBTableMetadata
{ _ptmiOid = OID 0,
_ptmiColumns =
[ RawColumnInfo
{ rciName = Column isColumnName,
rciPosition = fromIntegral isOrdinalPosition,
rciType = parseMySQLScalarType isColumnType, -- TODO: This needs to become more precise by considering Field length and character-set
rciIsNullable = isIsNullable == "YES", -- ref: https://dev.mysql.com/doc/refman/8.0/en/information-schema-columns-table.html
rciDescription = Just $ G.Description isColumnComment,
rciMutability = ColumnMutability {_cmIsInsertable = True, _cmIsUpdatable = True}
}
],
_ptmiPrimaryKey =
if isColumnKey == PRI
then
Just $
PrimaryKey
( Constraint
(ConstraintName $ fromMaybe "" isConstraintName)
(OID $ fromIntegral $ fromMaybe 0 isConstraintOrdinalPosition)
)
(NESeq.singleton (Column isColumnName))
else Nothing,
_ptmiUniqueConstraints =
if isColumnKey == UNI
then
HS.singleton
( UniqueConstraint
{ _ucConstraint =
Constraint
(ConstraintName $ fromMaybe "" isConstraintName)
(OID $ fromIntegral $ fromMaybe 0 isConstraintOrdinalPosition),
_ucColumns = HS.singleton (Column isColumnName)
}
)
else HS.empty,
_ptmiForeignKeys =
if isColumnKey == MUL
then
HS.singleton
( ForeignKeyMetadata
( ForeignKey
( Constraint
(ConstraintName $ fromMaybe "" isConstraintName)
(OID $ fromIntegral $ fromMaybe 0 isConstraintOrdinalPosition)
)
( TableName
{ name = (fromMaybe "" isReferencedTableName),
schema = isReferencedTableSchema
}
)
( NEHashMap.singleton
(Column isColumnName)
(Column $ fromMaybe "" isReferencedColumnName)
)
)
)
else HS.empty,
_ptmiViewInfo = Nothing,
_ptmiDescription = Nothing,
_ptmiExtraTableMetadata = (),
_ptmiCustomObjectTypes = mempty
}
mergeDBTableMetadata :: DBTableMetadata 'MySQL -> DBTableMetadata 'MySQL -> DBTableMetadata 'MySQL
mergeDBTableMetadata new existing =
DBTableMetadata
{ _ptmiOid = OID 0,
_ptmiColumns = _ptmiColumns existing <> _ptmiColumns new,
_ptmiPrimaryKey = _ptmiPrimaryKey existing <|> _ptmiPrimaryKey new, -- Only one column can be a PRIMARY KEY, so this is just a courtesy choice.
_ptmiUniqueConstraints = _ptmiUniqueConstraints existing <> _ptmiUniqueConstraints new, -- union
_ptmiForeignKeys = _ptmiForeignKeys existing <> _ptmiForeignKeys new, -- union
_ptmiViewInfo = _ptmiViewInfo existing <|> _ptmiViewInfo new,
_ptmiDescription = _ptmiDescription existing <|> _ptmiDescription new,
_ptmiExtraTableMetadata = (),
_ptmiCustomObjectTypes = mempty
}
data InformationSchema = InformationSchema
{ isTableSchema :: Text,
isTableName :: Text,
isColumnName :: Text,
isOrdinalPosition :: Word,
isColumnDefault :: Maybe Text,
isIsNullable :: Text,
isDataType :: Maybe Text,
isColumnType :: Text,
isColumnKey :: InformationSchemaColumnKey,
isColumnComment :: Text,
isConstraintName :: Maybe Text,
isConstraintOrdinalPosition :: Maybe Word,
isPositionInUniqueConstraint :: Maybe Word,
isReferencedTableSchema :: Maybe Text,
isReferencedTableName :: Maybe Text,
isReferencedColumnName :: Maybe Text
}
deriving (Show, Eq, Generic)
instance QueryResults InformationSchema where
convertResults
[ fisTableSchema,
fisTableName,
fisColumnName,
fisOrdinalPosition,
fisColumnDefault,
fisIsNullable,
fisDataType,
fisColumnType,
fisColumnKey,
fisColumnComment,
fisConstraintName,
fisConstraintOrdinalPosition,
fisPositionInUniqueConstraint,
fisReferencedTableSchema,
fisReferencedTableName,
fisReferencedColumnName
]
[ visTableSchema,
visTableName,
visColumnName,
visOrdinalPosition,
visColumnDefault,
visIsNullable,
visDataType,
visColumnType,
visColumnKey,
visColumnComment,
visConstraintName,
visConstraintOrdinalPosition,
visPositionInUniqueConstraint,
visReferencedTableSchema,
visReferencedTableName,
visReferencedColumnName
] =
InformationSchema
(convert fisTableSchema visTableSchema)
(convert fisTableName visTableName)
(convert fisColumnName visColumnName)
(convert fisOrdinalPosition visOrdinalPosition)
(convert fisColumnDefault visColumnDefault)
(convert fisIsNullable visIsNullable)
(convert fisDataType visDataType)
(convert fisColumnType visColumnType)
(convert fisColumnKey visColumnKey)
(convert fisColumnComment visColumnComment)
(convert fisConstraintName visConstraintName)
(convert fisConstraintOrdinalPosition visConstraintOrdinalPosition)
(convert fisPositionInUniqueConstraint visPositionInUniqueConstraint)
(convert fisReferencedTableSchema visReferencedTableSchema)
(convert fisReferencedTableName visReferencedTableName)
(convert fisReferencedColumnName visReferencedColumnName)
convertResults fs vs = convertError fs vs 16
data InformationSchemaColumnKey
= PRI
| UNI
| MUL
| -- | This field isn't NULLable and uses empty strings, by the looks of it.
BLANK
deriving (Show, Read, Eq, Generic)
instance Result InformationSchemaColumnKey where
convert f mbs =
case mbs of
Nothing ->
throw $
UnexpectedNull
(show $ fieldType f)
"InformationSchemaColumnKey"
(B8.unpack $ fieldName f)
"COLUMN_KEY in INFORMATION_SCHEMA cannot be NULL"
Just bs -> case bs of
-- Could have used 'readMaybe' here, but we need the specific errors.
"PRI" -> PRI -- primary key
"UNI" -> UNI -- unique key
"MUL" -> MUL -- foreign key (`MUL`tiple allowed, non-unique key)
"" -> BLANK
x ->
throw $
ConversionFailed
(show $ fieldType f)
"InformationSchemaColumnKey"
(B8.unpack $ fieldName f)
("COLUMN_KEY in INFORMATION_SCHEMA has value extraneous to the expected ENUM: " <> B8.unpack x)