2021-02-09 15:47:21 +03:00
|
|
|
-- | Top-level schema building function. Those are used to construct a schema for a given resource,
|
|
|
|
-- such as a table or a function. Those matches what the @BackendSchema@ class expects, and are the
|
|
|
|
-- default implementation a fully-fledged backend should support.
|
|
|
|
|
|
|
|
module Hasura.GraphQL.Schema.Build where
|
|
|
|
|
|
|
|
import Hasura.Prelude
|
|
|
|
|
|
|
|
import qualified Language.GraphQL.Draft.Syntax as G
|
|
|
|
|
|
|
|
import Data.Text.Extended
|
|
|
|
|
2021-03-15 16:02:58 +03:00
|
|
|
import qualified Hasura.SQL.AnyBackend as AB
|
|
|
|
|
2021-02-09 15:47:21 +03:00
|
|
|
import Hasura.GraphQL.Parser hiding (EnumValueInfo, field)
|
|
|
|
import Hasura.GraphQL.Schema.Backend (MonadBuildSchema)
|
|
|
|
import Hasura.GraphQL.Schema.Common
|
|
|
|
import Hasura.GraphQL.Schema.Mutation
|
|
|
|
import Hasura.GraphQL.Schema.Select
|
2021-06-11 06:26:50 +03:00
|
|
|
import Hasura.RQL.IR
|
2021-02-09 15:47:21 +03:00
|
|
|
import Hasura.RQL.Types
|
|
|
|
|
|
|
|
|
|
|
|
buildTableQueryFields
|
|
|
|
:: forall b r m n
|
|
|
|
. MonadBuildSchema b r m n
|
|
|
|
=> SourceName
|
|
|
|
-> SourceConfig b
|
|
|
|
-> TableName b
|
|
|
|
-> TableInfo b
|
|
|
|
-> G.Name
|
|
|
|
-> SelPermInfo b
|
server: IR for DB-DB joins
### Description
This PR adds the required IR for DB to DB joins, based on @paf31 and @0x777 's `feature/db-to-db` branch.
To do so, it also refactors the IR to introduce a new type parameter, `r`, which is used to recursively constructs the `v` parameter of remote QueryDBs. When collecting remote joins, we replace `r` with `Const Void`, indicating at the type level that there cannot be any leftover remote join.
Furthermore, this PR refactors IR.Select for readability, moves some code from IR.Root to IR.Select to avoid having to deal with circular dependencies, and makes it compile by adding `error` in all new cases in the execution pipeline.
The diff doesn't make it clear, but most of Select.hs is actually unchanged. Declarations have just been reordered by topic, in the following order:
- type declarations
- instance declarations
- type aliases
- constructor functions
- traverse functions
https://github.com/hasura/graphql-engine-mono/pull/1580
Co-authored-by: Phil Freeman <630306+paf31@users.noreply.github.com>
GitOrigin-RevId: bbdcb4119cec8bb3fc32f1294f91b8dea0728721
2021-06-18 02:12:11 +03:00
|
|
|
-> m [FieldParser n (QueryRootField UnpreparedValue UnpreparedValue)]
|
2021-02-09 15:47:21 +03:00
|
|
|
buildTableQueryFields sourceName sourceInfo tableName tableInfo gqlName selPerms = do
|
|
|
|
let
|
2021-03-15 16:02:58 +03:00
|
|
|
mkRF = RFDB sourceName
|
|
|
|
. AB.mkAnyBackend
|
|
|
|
. SourceConfigWith sourceInfo
|
|
|
|
. QDBR
|
2021-02-09 15:47:21 +03:00
|
|
|
customRootFields = _tcCustomRootFields $ _tciCustomConfig $ _tiCoreInfo tableInfo
|
|
|
|
-- select table
|
|
|
|
selectName = fromMaybe gqlName $ _tcrfSelect customRootFields
|
|
|
|
selectDesc = Just $ G.Description $ "fetch data from the table: " <>> tableName
|
|
|
|
-- select table by pk
|
|
|
|
selectPKName = fromMaybe (gqlName <> $$(G.litName "_by_pk")) $ _tcrfSelectByPk customRootFields
|
|
|
|
selectPKDesc = Just $ G.Description $ "fetch data from the table: " <> tableName <<> " using primary key columns"
|
|
|
|
-- select table aggregate
|
|
|
|
selectAggName = fromMaybe (gqlName <> $$(G.litName "_aggregate")) $ _tcrfSelectAggregate customRootFields
|
|
|
|
selectAggDesc = Just $ G.Description $ "fetch aggregated fields from the table: " <>> tableName
|
|
|
|
catMaybes <$> sequenceA
|
2021-05-18 16:06:42 +03:00
|
|
|
[ requiredFieldParser (mkRF . QDBMultipleRows) $ selectTable sourceName tableInfo selectName selectDesc selPerms
|
|
|
|
, optionalFieldParser (mkRF . QDBSingleRow) $ selectTableByPk sourceName tableInfo selectPKName selectPKDesc selPerms
|
|
|
|
, optionalFieldParser (mkRF . QDBAggregation) $ selectTableAggregate sourceName tableInfo selectAggName selectAggDesc selPerms
|
2021-02-09 15:47:21 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
buildTableInsertMutationFields
|
|
|
|
:: forall b r m n
|
|
|
|
. MonadBuildSchema b r m n
|
|
|
|
=> SourceName
|
|
|
|
-> SourceConfig b
|
|
|
|
-> TableName b
|
|
|
|
-> TableInfo b
|
|
|
|
-> G.Name
|
|
|
|
-> InsPermInfo b
|
|
|
|
-> Maybe (SelPermInfo b)
|
|
|
|
-> Maybe (UpdPermInfo b)
|
server: IR for DB-DB joins
### Description
This PR adds the required IR for DB to DB joins, based on @paf31 and @0x777 's `feature/db-to-db` branch.
To do so, it also refactors the IR to introduce a new type parameter, `r`, which is used to recursively constructs the `v` parameter of remote QueryDBs. When collecting remote joins, we replace `r` with `Const Void`, indicating at the type level that there cannot be any leftover remote join.
Furthermore, this PR refactors IR.Select for readability, moves some code from IR.Root to IR.Select to avoid having to deal with circular dependencies, and makes it compile by adding `error` in all new cases in the execution pipeline.
The diff doesn't make it clear, but most of Select.hs is actually unchanged. Declarations have just been reordered by topic, in the following order:
- type declarations
- instance declarations
- type aliases
- constructor functions
- traverse functions
https://github.com/hasura/graphql-engine-mono/pull/1580
Co-authored-by: Phil Freeman <630306+paf31@users.noreply.github.com>
GitOrigin-RevId: bbdcb4119cec8bb3fc32f1294f91b8dea0728721
2021-06-18 02:12:11 +03:00
|
|
|
-> m [FieldParser n (MutationRootField UnpreparedValue UnpreparedValue)]
|
2021-02-09 15:47:21 +03:00
|
|
|
buildTableInsertMutationFields sourceName sourceInfo tableName tableInfo gqlName insPerms mSelPerms mUpdPerms = do
|
|
|
|
let
|
2021-03-15 16:02:58 +03:00
|
|
|
mkRF = RFDB sourceName
|
|
|
|
. AB.mkAnyBackend
|
|
|
|
. SourceConfigWith sourceInfo
|
|
|
|
. MDBR
|
2021-02-09 15:47:21 +03:00
|
|
|
customRootFields = _tcCustomRootFields $ _tciCustomConfig $ _tiCoreInfo tableInfo
|
|
|
|
-- insert into table
|
|
|
|
insertName = fromMaybe ($$(G.litName "insert_") <> gqlName) $ _tcrfInsert customRootFields
|
|
|
|
insertDesc = Just $ G.Description $ "insert data into the table: " <>> tableName
|
|
|
|
-- insert one into table
|
|
|
|
insertOneName = fromMaybe ($$(G.litName "insert_") <> gqlName <> $$(G.litName "_one")) $ _tcrfInsertOne customRootFields
|
|
|
|
insertOneDesc = Just $ G.Description $ "insert a single row into the table: " <>> tableName
|
2021-05-18 16:06:42 +03:00
|
|
|
insert <- insertIntoTable sourceName tableInfo insertName insertDesc insPerms mSelPerms mUpdPerms
|
2021-02-09 15:47:21 +03:00
|
|
|
-- Select permissions are required for insertOne: the selection set is the
|
|
|
|
-- same as a select on that table, and it therefore can't be populated if the
|
|
|
|
-- user doesn't have select permissions.
|
|
|
|
insertOne <- for mSelPerms \selPerms ->
|
2021-05-18 16:06:42 +03:00
|
|
|
insertOneIntoTable sourceName tableInfo insertOneName insertOneDesc insPerms selPerms mUpdPerms
|
2021-02-09 15:47:21 +03:00
|
|
|
pure $ fmap (mkRF . MDBInsert) <$> insert : maybeToList insertOne
|
|
|
|
|
|
|
|
buildTableUpdateMutationFields
|
|
|
|
:: forall b r m n
|
|
|
|
. MonadBuildSchema b r m n
|
|
|
|
=> SourceName
|
|
|
|
-> SourceConfig b
|
|
|
|
-> TableName b
|
|
|
|
-> TableInfo b
|
|
|
|
-> G.Name
|
|
|
|
-> UpdPermInfo b
|
|
|
|
-> Maybe (SelPermInfo b)
|
server: IR for DB-DB joins
### Description
This PR adds the required IR for DB to DB joins, based on @paf31 and @0x777 's `feature/db-to-db` branch.
To do so, it also refactors the IR to introduce a new type parameter, `r`, which is used to recursively constructs the `v` parameter of remote QueryDBs. When collecting remote joins, we replace `r` with `Const Void`, indicating at the type level that there cannot be any leftover remote join.
Furthermore, this PR refactors IR.Select for readability, moves some code from IR.Root to IR.Select to avoid having to deal with circular dependencies, and makes it compile by adding `error` in all new cases in the execution pipeline.
The diff doesn't make it clear, but most of Select.hs is actually unchanged. Declarations have just been reordered by topic, in the following order:
- type declarations
- instance declarations
- type aliases
- constructor functions
- traverse functions
https://github.com/hasura/graphql-engine-mono/pull/1580
Co-authored-by: Phil Freeman <630306+paf31@users.noreply.github.com>
GitOrigin-RevId: bbdcb4119cec8bb3fc32f1294f91b8dea0728721
2021-06-18 02:12:11 +03:00
|
|
|
-> m [FieldParser n (MutationRootField UnpreparedValue UnpreparedValue)]
|
2021-02-09 15:47:21 +03:00
|
|
|
buildTableUpdateMutationFields sourceName sourceInfo tableName tableInfo gqlName updPerms mSelPerms = do
|
|
|
|
let
|
2021-03-15 16:02:58 +03:00
|
|
|
mkRF = RFDB sourceName
|
|
|
|
. AB.mkAnyBackend
|
|
|
|
. SourceConfigWith sourceInfo
|
|
|
|
. MDBR
|
2021-02-09 15:47:21 +03:00
|
|
|
customRootFields = _tcCustomRootFields $ _tciCustomConfig $ _tiCoreInfo tableInfo
|
|
|
|
-- update table
|
|
|
|
updateName = fromMaybe ($$(G.litName "update_") <> gqlName) $ _tcrfUpdate customRootFields
|
|
|
|
updateDesc = Just $ G.Description $ "update data of the table: " <>> tableName
|
|
|
|
-- update table by pk
|
|
|
|
updatePKName = fromMaybe ($$(G.litName "update_") <> gqlName <> $$(G.litName "_by_pk")) $ _tcrfUpdateByPk customRootFields
|
|
|
|
updatePKDesc = Just $ G.Description $ "update single row of the table: " <>> tableName
|
2021-05-18 16:06:42 +03:00
|
|
|
update <- updateTable sourceName tableInfo updateName updateDesc updPerms mSelPerms
|
2021-02-09 15:47:21 +03:00
|
|
|
-- Primary keys can only be tested in the `where` clause if the user has
|
|
|
|
-- select permissions for them, which at the very least requires select
|
|
|
|
-- permissions.
|
2021-05-18 16:06:42 +03:00
|
|
|
updateByPk <- fmap join $ for mSelPerms $ updateTableByPk sourceName tableInfo updatePKName updatePKDesc updPerms
|
2021-02-09 15:47:21 +03:00
|
|
|
pure $ fmap (mkRF . MDBUpdate) <$> catMaybes [update, updateByPk]
|
|
|
|
|
|
|
|
buildTableDeleteMutationFields
|
|
|
|
:: forall b r m n
|
|
|
|
. MonadBuildSchema b r m n
|
|
|
|
=> SourceName
|
|
|
|
-> SourceConfig b
|
|
|
|
-> TableName b
|
|
|
|
-> TableInfo b
|
|
|
|
-> G.Name
|
|
|
|
-> DelPermInfo b
|
|
|
|
-> Maybe (SelPermInfo b)
|
server: IR for DB-DB joins
### Description
This PR adds the required IR for DB to DB joins, based on @paf31 and @0x777 's `feature/db-to-db` branch.
To do so, it also refactors the IR to introduce a new type parameter, `r`, which is used to recursively constructs the `v` parameter of remote QueryDBs. When collecting remote joins, we replace `r` with `Const Void`, indicating at the type level that there cannot be any leftover remote join.
Furthermore, this PR refactors IR.Select for readability, moves some code from IR.Root to IR.Select to avoid having to deal with circular dependencies, and makes it compile by adding `error` in all new cases in the execution pipeline.
The diff doesn't make it clear, but most of Select.hs is actually unchanged. Declarations have just been reordered by topic, in the following order:
- type declarations
- instance declarations
- type aliases
- constructor functions
- traverse functions
https://github.com/hasura/graphql-engine-mono/pull/1580
Co-authored-by: Phil Freeman <630306+paf31@users.noreply.github.com>
GitOrigin-RevId: bbdcb4119cec8bb3fc32f1294f91b8dea0728721
2021-06-18 02:12:11 +03:00
|
|
|
-> m [FieldParser n (MutationRootField UnpreparedValue UnpreparedValue)]
|
2021-02-09 15:47:21 +03:00
|
|
|
buildTableDeleteMutationFields sourceName sourceInfo tableName tableInfo gqlName delPerms mSelPerms = do
|
|
|
|
let
|
2021-03-15 16:02:58 +03:00
|
|
|
mkRF = RFDB sourceName
|
|
|
|
. AB.mkAnyBackend
|
|
|
|
. SourceConfigWith sourceInfo
|
|
|
|
. MDBR
|
2021-02-09 15:47:21 +03:00
|
|
|
customRootFields = _tcCustomRootFields $ _tciCustomConfig $ _tiCoreInfo tableInfo
|
|
|
|
-- delete from table
|
|
|
|
deleteName = fromMaybe ($$(G.litName "delete_") <> gqlName) $ _tcrfDelete customRootFields
|
|
|
|
deleteDesc = Just $ G.Description $ "delete data from the table: " <>> tableName
|
|
|
|
-- delete from table by pk
|
|
|
|
deletePKName = fromMaybe ($$(G.litName "delete_") <> gqlName <> $$(G.litName "_by_pk")) $ _tcrfDeleteByPk customRootFields
|
|
|
|
deletePKDesc = Just $ G.Description $ "delete single row from the table: " <>> tableName
|
2021-05-18 16:06:42 +03:00
|
|
|
delete <- deleteFromTable sourceName tableInfo deleteName deleteDesc delPerms mSelPerms
|
2021-02-09 15:47:21 +03:00
|
|
|
-- Primary keys can only be tested in the `where` clause if the user has
|
|
|
|
-- select permissions for them, which at the very least requires select
|
|
|
|
-- permissions.
|
2021-05-18 16:06:42 +03:00
|
|
|
deleteByPk <- fmap join $ for mSelPerms $ deleteFromTableByPk sourceName tableInfo deletePKName deletePKDesc delPerms
|
2021-02-09 15:47:21 +03:00
|
|
|
pure $ fmap (mkRF . MDBDelete) <$> delete : maybeToList deleteByPk
|
|
|
|
|
|
|
|
buildFunctionQueryFields
|
|
|
|
:: forall b r m n
|
|
|
|
. MonadBuildSchema b r m n
|
|
|
|
=> SourceName
|
|
|
|
-> SourceConfig b
|
|
|
|
-> FunctionName b
|
|
|
|
-> FunctionInfo b
|
|
|
|
-> TableName b
|
|
|
|
-> SelPermInfo b
|
server: IR for DB-DB joins
### Description
This PR adds the required IR for DB to DB joins, based on @paf31 and @0x777 's `feature/db-to-db` branch.
To do so, it also refactors the IR to introduce a new type parameter, `r`, which is used to recursively constructs the `v` parameter of remote QueryDBs. When collecting remote joins, we replace `r` with `Const Void`, indicating at the type level that there cannot be any leftover remote join.
Furthermore, this PR refactors IR.Select for readability, moves some code from IR.Root to IR.Select to avoid having to deal with circular dependencies, and makes it compile by adding `error` in all new cases in the execution pipeline.
The diff doesn't make it clear, but most of Select.hs is actually unchanged. Declarations have just been reordered by topic, in the following order:
- type declarations
- instance declarations
- type aliases
- constructor functions
- traverse functions
https://github.com/hasura/graphql-engine-mono/pull/1580
Co-authored-by: Phil Freeman <630306+paf31@users.noreply.github.com>
GitOrigin-RevId: bbdcb4119cec8bb3fc32f1294f91b8dea0728721
2021-06-18 02:12:11 +03:00
|
|
|
-> m [FieldParser n (QueryRootField UnpreparedValue UnpreparedValue)]
|
2021-02-09 15:47:21 +03:00
|
|
|
buildFunctionQueryFields sourceName sourceInfo functionName functionInfo tableName selPerms = do
|
|
|
|
funcName <- functionGraphQLName @b functionName `onLeft` throwError
|
|
|
|
let
|
2021-03-15 16:02:58 +03:00
|
|
|
mkRF = RFDB sourceName
|
|
|
|
. AB.mkAnyBackend
|
|
|
|
. SourceConfigWith sourceInfo
|
|
|
|
. QDBR
|
2021-02-09 15:47:21 +03:00
|
|
|
-- select function
|
|
|
|
funcDesc = Just $ G.Description $ "execute function " <> functionName <<> " which returns " <>> tableName
|
|
|
|
-- select function agg
|
|
|
|
funcAggName = funcName <> $$(G.litName "_aggregate")
|
|
|
|
funcAggDesc = Just $ G.Description $ "execute function " <> functionName <<> " and query aggregates on result of table type " <>> tableName
|
|
|
|
queryResultType =
|
|
|
|
case _fiJsonAggSelect functionInfo of
|
|
|
|
JASMultipleRows -> QDBMultipleRows
|
|
|
|
JASSingleObject -> QDBSingleRow
|
|
|
|
catMaybes <$> sequenceA
|
2021-05-18 16:06:42 +03:00
|
|
|
[ requiredFieldParser (mkRF . queryResultType) $ selectFunction sourceName functionInfo funcName funcDesc selPerms
|
|
|
|
, optionalFieldParser (mkRF . QDBAggregation) $ selectFunctionAggregate sourceName functionInfo funcAggName funcAggDesc selPerms
|
2021-02-09 15:47:21 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
buildFunctionMutationFields
|
|
|
|
:: forall b r m n
|
|
|
|
. MonadBuildSchema b r m n
|
|
|
|
=> SourceName
|
|
|
|
-> SourceConfig b
|
|
|
|
-> FunctionName b
|
|
|
|
-> FunctionInfo b
|
|
|
|
-> TableName b
|
|
|
|
-> SelPermInfo b
|
server: IR for DB-DB joins
### Description
This PR adds the required IR for DB to DB joins, based on @paf31 and @0x777 's `feature/db-to-db` branch.
To do so, it also refactors the IR to introduce a new type parameter, `r`, which is used to recursively constructs the `v` parameter of remote QueryDBs. When collecting remote joins, we replace `r` with `Const Void`, indicating at the type level that there cannot be any leftover remote join.
Furthermore, this PR refactors IR.Select for readability, moves some code from IR.Root to IR.Select to avoid having to deal with circular dependencies, and makes it compile by adding `error` in all new cases in the execution pipeline.
The diff doesn't make it clear, but most of Select.hs is actually unchanged. Declarations have just been reordered by topic, in the following order:
- type declarations
- instance declarations
- type aliases
- constructor functions
- traverse functions
https://github.com/hasura/graphql-engine-mono/pull/1580
Co-authored-by: Phil Freeman <630306+paf31@users.noreply.github.com>
GitOrigin-RevId: bbdcb4119cec8bb3fc32f1294f91b8dea0728721
2021-06-18 02:12:11 +03:00
|
|
|
-> m [FieldParser n (MutationRootField UnpreparedValue UnpreparedValue)]
|
2021-02-09 15:47:21 +03:00
|
|
|
buildFunctionMutationFields sourceName sourceInfo functionName functionInfo tableName selPerms = do
|
|
|
|
funcName <- functionGraphQLName @b functionName `onLeft` throwError
|
|
|
|
let
|
2021-03-15 16:02:58 +03:00
|
|
|
mkRF = RFDB sourceName
|
|
|
|
. AB.mkAnyBackend
|
|
|
|
. SourceConfigWith sourceInfo
|
|
|
|
. MDBR
|
2021-02-09 15:47:21 +03:00
|
|
|
funcDesc = Just $ G.Description $ "execute VOLATILE function " <> functionName <<> " which returns " <>> tableName
|
|
|
|
jsonAggSelect = _fiJsonAggSelect functionInfo
|
|
|
|
catMaybes <$> sequenceA
|
2021-05-18 16:06:42 +03:00
|
|
|
[ requiredFieldParser (mkRF . MDBFunction jsonAggSelect) $ selectFunction sourceName functionInfo funcName funcDesc selPerms
|
2021-02-09 15:47:21 +03:00
|
|
|
-- TODO: do we want aggregate mutation functions?
|
|
|
|
]
|