mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-17 12:31:52 +03:00
caf9957aca
GraphQL types can refer to each other in a circular way. The PDV framework used to use values of type `Unique` to recognize two fragments of GraphQL schema as being the same instance. Internally, this is based on `Data.Unique` from the `base` package, which simply increases a counter on every creation of a `Unique` object. **NB**: The `Unique` values are _not_ used for knot tying the schema combinators themselves (i.e. `Parser`s). The knot tying for `Parser`s is purely based on keys provided to `memoizeOn`. The `Unique` values are _only_ used to recognize two pieces of GraphQL _schema_ as being identical. Originally, the idea was that this would help us with a perfectly correct identification of GraphQL types. But this fully correct equality checking of GraphQL types was never implemented, and does not seem to be necessary to prevent bugs. Specifically, these `Unique` values are stored as part of `data Definition a`, which specifies a part of our internal abstract syntax tree for the GraphQL types that we expose. The `Unique` values get initialized by the `SchemaT` effect. In #2894 and #2895, we are experimenting with how (parts of) the GraphQL types can be hidden behind certain permission predicates. This would allow a single GraphQL schema in memory to serve all roles, implementing #2711. The permission predicates get evaluated at query parsing time when we know what role is doing a certain request, thus outputting the correct GraphQL types for that role. If the approach of #2895 is followed, then the `Definition` objects, and thus the `Unique` values, would be hidden behind the permission predicates. Since the permission predicates are evaluated only after the schema is already supposed to be built, this means that the permission predicates would prevent us from initializing the `Unique` values, rendering them useless. The simplest remedy to this is to remove our usage of `Unique` altogether from the GraphQL schema and schema combinators. It doesn't serve a functional purpose, doesn't prevent bugs, and requires extra bookkeeping. PR-URL: https://github.com/hasura/graphql-engine-mono/pull/2980 GitOrigin-RevId: 50d3f9e0b9fbf578ac49c8fc773ba64a94b1f43d
199 lines
7.2 KiB
Haskell
199 lines
7.2 KiB
Haskell
-- | Helper functions for generating the schema of database tables
|
|
module Hasura.GraphQL.Schema.Table
|
|
( getTableGQLName,
|
|
tableSelectColumnsEnum,
|
|
tableUpdateColumnsEnum,
|
|
tablePermissions,
|
|
tableSelectPermissions,
|
|
tableSelectFields,
|
|
tableColumns,
|
|
tableSelectColumns,
|
|
tableUpdateColumns,
|
|
)
|
|
where
|
|
|
|
import Data.Has
|
|
import Data.HashMap.Strict qualified as Map
|
|
import Data.HashSet qualified as Set
|
|
import Data.Text.Extended
|
|
import Hasura.Base.Error (QErr)
|
|
import Hasura.GraphQL.Parser (Kind (..), Parser)
|
|
import Hasura.GraphQL.Parser qualified as P
|
|
import Hasura.GraphQL.Parser.Class
|
|
import Hasura.GraphQL.Schema.Backend
|
|
import Hasura.Prelude
|
|
import Hasura.RQL.DML.Internal (getRolePermInfo)
|
|
import Hasura.RQL.Types
|
|
import Language.GraphQL.Draft.Syntax qualified as G
|
|
|
|
-- | Helper function to get the table GraphQL name. A table may have a
|
|
-- custom name configured with it. When the custom name exists, the GraphQL nodes
|
|
-- that are generated according to the custom name. For example: Let's say,
|
|
-- we have a table called `users address`, the name of the table is not GraphQL
|
|
-- compliant so we configure the table with a GraphQL compliant name,
|
|
-- say `users_address`
|
|
-- The generated top-level nodes of this table will be like `users_address`,
|
|
-- `insert_users_address` etc
|
|
getTableGQLName ::
|
|
forall b m.
|
|
(Backend b, MonadError QErr m) =>
|
|
TableInfo b ->
|
|
m G.Name
|
|
getTableGQLName tableInfo = do
|
|
let coreInfo = _tiCoreInfo tableInfo
|
|
tableName = _tciName coreInfo
|
|
tableCustomName = _tcCustomName $ _tciCustomConfig coreInfo
|
|
tableCustomName
|
|
`onNothing` tableGraphQLName @b tableName
|
|
`onLeft` throwError
|
|
|
|
-- | Table select columns enum
|
|
--
|
|
-- Parser for an enum type that matches the columns of the given
|
|
-- table. Used as a parameter for "distinct", among others. Maps to
|
|
-- the table_select_column object.
|
|
--
|
|
-- Return Nothing if there's no column the current user has "select"
|
|
-- permissions for.
|
|
tableSelectColumnsEnum ::
|
|
forall m n r b.
|
|
(BackendSchema b, MonadSchema n m, MonadRole r m, MonadTableInfo r m, Has P.MkTypename r) =>
|
|
SourceName ->
|
|
TableInfo b ->
|
|
SelPermInfo b ->
|
|
m (Maybe (Parser 'Both n (Column b)))
|
|
tableSelectColumnsEnum sourceName tableInfo selectPermissions = do
|
|
tableGQLName <- getTableGQLName @b tableInfo
|
|
columns <- tableSelectColumns sourceName tableInfo selectPermissions
|
|
enumName <- P.mkTypename $ tableGQLName <> $$(G.litName "_select_column")
|
|
let description =
|
|
Just $
|
|
G.Description $
|
|
"select columns of table " <>> tableInfoName tableInfo
|
|
pure $
|
|
P.enum enumName description
|
|
<$> nonEmpty
|
|
[ ( define $ pgiName column,
|
|
pgiColumn column
|
|
)
|
|
| column <- columns
|
|
]
|
|
where
|
|
define name =
|
|
P.Definition name (Just $ G.Description "column name") P.EnumValueInfo
|
|
|
|
-- | Table update columns enum
|
|
--
|
|
-- Parser for an enum type that matches the columns of the given
|
|
-- table. Used for conflict resolution in "insert" mutations, among
|
|
-- others. Maps to the table_update_column object.
|
|
--
|
|
-- If there's no column for which the current user has "update"
|
|
-- permissions, this functions returns an enum that only contains a
|
|
-- placeholder, so as to still allow this type to exist in the schema.
|
|
tableUpdateColumnsEnum ::
|
|
forall m n r b.
|
|
(BackendSchema b, MonadSchema n m, MonadError QErr m, MonadReader r m, Has P.MkTypename r) =>
|
|
TableInfo b ->
|
|
UpdPermInfo b ->
|
|
m (Parser 'Both n (Maybe (Column b)))
|
|
tableUpdateColumnsEnum tableInfo updatePermissions = do
|
|
tableGQLName <- getTableGQLName tableInfo
|
|
columns <- tableUpdateColumns tableInfo updatePermissions
|
|
enumName <- P.mkTypename $ tableGQLName <> $$(G.litName "_update_column")
|
|
let tableName = tableInfoName tableInfo
|
|
enumDesc = Just $ G.Description $ "update columns of table " <>> tableName
|
|
altDesc = Just $ G.Description $ "placeholder for update columns of table " <> tableName <<> " (current role has no relevant permissions)"
|
|
enumValues = do
|
|
column <- columns
|
|
pure (define $ pgiName column, Just $ pgiColumn column)
|
|
pure $ case nonEmpty enumValues of
|
|
Just values -> P.enum enumName enumDesc values
|
|
Nothing -> P.enum enumName altDesc $ pure (placeholder, Nothing)
|
|
where
|
|
define name = P.Definition name (Just $ G.Description "column name") P.EnumValueInfo
|
|
placeholder = P.Definition @P.EnumValueInfo $$(G.litName "_PLACEHOLDER") (Just $ G.Description "placeholder (do not use)") P.EnumValueInfo
|
|
|
|
tablePermissions ::
|
|
forall m n r b.
|
|
(Backend b, MonadSchema n m, MonadRole r m) =>
|
|
TableInfo b ->
|
|
m (Maybe (RolePermInfo b))
|
|
tablePermissions tableInfo = do
|
|
roleName <- askRoleName
|
|
pure $ getRolePermInfo roleName tableInfo
|
|
|
|
tableSelectPermissions ::
|
|
forall b r m n.
|
|
(Backend b, MonadSchema n m, MonadRole r m) =>
|
|
TableInfo b ->
|
|
m (Maybe (SelPermInfo b))
|
|
tableSelectPermissions tableInfo = (_permSel =<<) <$> tablePermissions tableInfo
|
|
|
|
tableSelectFields ::
|
|
forall m n r b.
|
|
(Backend b, MonadSchema n m, MonadTableInfo r m, MonadRole r m) =>
|
|
SourceName ->
|
|
TableInfo b ->
|
|
SelPermInfo b ->
|
|
m [FieldInfo b]
|
|
tableSelectFields sourceName tableInfo permissions = do
|
|
let tableFields = _tciFieldInfoMap . _tiCoreInfo $ tableInfo
|
|
filterM canBeSelected $ Map.elems tableFields
|
|
where
|
|
canBeSelected (FIColumn columnInfo) =
|
|
pure $ Map.member (pgiColumn columnInfo) (spiCols permissions)
|
|
canBeSelected (FIRelationship relationshipInfo) = do
|
|
tableInfo' <- askTableInfo sourceName $ riRTable relationshipInfo
|
|
isJust <$> tableSelectPermissions @b tableInfo'
|
|
canBeSelected (FIComputedField computedFieldInfo) =
|
|
case _cfiReturnType computedFieldInfo of
|
|
CFRScalar _ ->
|
|
pure $ Map.member (_cfiName computedFieldInfo) $ spiScalarComputedFields permissions
|
|
CFRSetofTable tableName -> do
|
|
tableInfo' <- askTableInfo sourceName tableName
|
|
isJust <$> tableSelectPermissions @b tableInfo'
|
|
canBeSelected (FIRemoteRelationship _) = pure True
|
|
|
|
tableColumns ::
|
|
forall b. TableInfo b -> [ColumnInfo b]
|
|
tableColumns tableInfo =
|
|
mapMaybe columnInfo . Map.elems . _tciFieldInfoMap . _tiCoreInfo $ tableInfo
|
|
where
|
|
columnInfo (FIColumn ci) = Just ci
|
|
columnInfo _ = Nothing
|
|
|
|
-- | Get the columns of a table that my be selected under the given select
|
|
-- permissions.
|
|
tableSelectColumns ::
|
|
forall m n r b.
|
|
(Backend b, MonadSchema n m, MonadTableInfo r m, MonadRole r m) =>
|
|
SourceName ->
|
|
TableInfo b ->
|
|
SelPermInfo b ->
|
|
m [ColumnInfo b]
|
|
tableSelectColumns sourceName tableInfo permissions =
|
|
mapMaybe columnInfo <$> tableSelectFields sourceName tableInfo permissions
|
|
where
|
|
columnInfo (FIColumn ci) = Just ci
|
|
columnInfo _ = Nothing
|
|
|
|
-- | Get the columns of a table that my be updated under the given update
|
|
-- permissions.
|
|
tableUpdateColumns ::
|
|
forall m n b.
|
|
(Backend b, MonadSchema n m) =>
|
|
TableInfo b ->
|
|
UpdPermInfo b ->
|
|
m [ColumnInfo b]
|
|
tableUpdateColumns tableInfo permissions = do
|
|
let tableFields = _tciFieldInfoMap . _tiCoreInfo $ tableInfo
|
|
pure $ mapMaybe isUpdatable $ Map.elems tableFields
|
|
where
|
|
isUpdatable (FIColumn columnInfo) =
|
|
if Set.member (pgiColumn columnInfo) (upiCols permissions)
|
|
&& not (Map.member (pgiColumn columnInfo) (upiSet permissions))
|
|
then Just columnInfo
|
|
else Nothing
|
|
isUpdatable _ = Nothing
|