2021-07-07 04:43:42 +03:00
|
|
|
{-# LANGUAGE ViewPatterns #-}
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
-- | The RQL metadata query ('/v1/metadata')
|
2021-11-04 19:08:33 +03:00
|
|
|
module Hasura.Server.API.Metadata
|
|
|
|
( RQLMetadata,
|
|
|
|
RQLMetadataV1 (..),
|
|
|
|
runMetadataQuery,
|
|
|
|
)
|
|
|
|
where
|
2021-01-07 12:04:22 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
import Control.Monad.Trans.Control (MonadBaseControl)
|
|
|
|
import Data.Aeson
|
|
|
|
import Data.Aeson.Casing
|
|
|
|
import Data.Aeson.Types qualified as A
|
|
|
|
import Data.Environment qualified as Env
|
|
|
|
import Data.Has (Has)
|
|
|
|
import Data.Text qualified as T
|
|
|
|
import Data.Text.Extended qualified as T
|
|
|
|
import Hasura.Base.Error
|
|
|
|
import Hasura.EncJSON
|
|
|
|
import Hasura.Logging qualified as L
|
|
|
|
import Hasura.Metadata.Class
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.DDL.Action
|
|
|
|
import Hasura.RQL.DDL.ApiLimit
|
|
|
|
import Hasura.RQL.DDL.ComputedField
|
|
|
|
import Hasura.RQL.DDL.CustomTypes
|
|
|
|
import Hasura.RQL.DDL.Endpoint
|
|
|
|
import Hasura.RQL.DDL.EventTrigger
|
|
|
|
import Hasura.RQL.DDL.GraphqlSchemaIntrospection
|
|
|
|
import Hasura.RQL.DDL.InheritedRoles
|
|
|
|
import Hasura.RQL.DDL.Metadata
|
|
|
|
import Hasura.RQL.DDL.Network
|
|
|
|
import Hasura.RQL.DDL.Permission
|
|
|
|
import Hasura.RQL.DDL.QueryCollection
|
|
|
|
import Hasura.RQL.DDL.QueryTags
|
|
|
|
import Hasura.RQL.DDL.Relationship
|
|
|
|
import Hasura.RQL.DDL.Relationship.Rename
|
|
|
|
import Hasura.RQL.DDL.RemoteRelationship
|
|
|
|
import Hasura.RQL.DDL.RemoteSchema
|
|
|
|
import Hasura.RQL.DDL.ScheduledTrigger
|
|
|
|
import Hasura.RQL.DDL.Schema
|
|
|
|
import Hasura.RQL.DDL.Schema.Source
|
|
|
|
import Hasura.RQL.Types
|
|
|
|
import Hasura.RQL.Types.Eventing.Backend
|
|
|
|
import Hasura.RQL.Types.Run
|
|
|
|
import Hasura.SQL.AnyBackend
|
|
|
|
import Hasura.SQL.Tag
|
|
|
|
import Hasura.Server.API.Backend
|
|
|
|
import Hasura.Server.API.Instances ()
|
|
|
|
import Hasura.Server.Types (InstanceId (..), MaintenanceMode (..))
|
|
|
|
import Hasura.Server.Utils (APIVersion (..))
|
|
|
|
import Hasura.Session
|
|
|
|
import Hasura.Tracing qualified as Tracing
|
|
|
|
import Network.HTTP.Client.Manager qualified as HTTP
|
2021-01-07 12:04:22 +03:00
|
|
|
|
2021-02-16 11:08:19 +03:00
|
|
|
data RQLMetadataV1
|
2021-09-24 01:56:37 +03:00
|
|
|
= -- Sources
|
|
|
|
RMAddSource !(AnyBackend AddSource)
|
2021-07-07 04:43:42 +03:00
|
|
|
| RMDropSource DropSource
|
|
|
|
| RMRenameSource !RenameSource
|
2021-09-24 01:56:37 +03:00
|
|
|
| -- Tables
|
|
|
|
RMTrackTable !(AnyBackend TrackTableV2)
|
|
|
|
| RMUntrackTable !(AnyBackend UntrackTable)
|
2021-07-07 04:43:42 +03:00
|
|
|
| RMSetTableCustomization !(AnyBackend SetTableCustomization)
|
2021-09-24 01:56:37 +03:00
|
|
|
| -- Tables (PG-specific)
|
|
|
|
RMPgSetTableIsEnum !SetTableIsEnum
|
|
|
|
| -- Tables permissions
|
|
|
|
RMCreateInsertPermission !(AnyBackend (CreatePerm InsPerm))
|
2021-07-07 04:43:42 +03:00
|
|
|
| RMCreateSelectPermission !(AnyBackend (CreatePerm SelPerm))
|
|
|
|
| RMCreateUpdatePermission !(AnyBackend (CreatePerm UpdPerm))
|
|
|
|
| RMCreateDeletePermission !(AnyBackend (CreatePerm DelPerm))
|
2021-09-24 01:56:37 +03:00
|
|
|
| RMDropInsertPermission !(AnyBackend (DropPerm InsPerm))
|
|
|
|
| RMDropSelectPermission !(AnyBackend (DropPerm SelPerm))
|
|
|
|
| RMDropUpdatePermission !(AnyBackend (DropPerm UpdPerm))
|
|
|
|
| RMDropDeletePermission !(AnyBackend (DropPerm DelPerm))
|
|
|
|
| RMSetPermissionComment !(AnyBackend SetPermComment)
|
|
|
|
| -- Tables relationships
|
|
|
|
RMCreateObjectRelationship !(AnyBackend CreateObjRel)
|
|
|
|
| RMCreateArrayRelationship !(AnyBackend CreateArrRel)
|
|
|
|
| RMDropRelationship !(AnyBackend DropRel)
|
|
|
|
| RMSetRelationshipComment !(AnyBackend SetRelComment)
|
|
|
|
| RMRenameRelationship !(AnyBackend RenameRel)
|
|
|
|
| -- Tables remote relationships
|
2021-12-01 07:53:34 +03:00
|
|
|
RMCreateRemoteRelationship !(AnyBackend CreateFromSourceRelationship)
|
|
|
|
| RMUpdateRemoteRelationship !(AnyBackend CreateFromSourceRelationship)
|
|
|
|
| RMDeleteRemoteRelationship !(DeleteFromSourceRelationship ('Postgres 'Vanilla))
|
2021-09-24 01:56:37 +03:00
|
|
|
| -- Functions
|
|
|
|
RMTrackFunction !(AnyBackend TrackFunctionV2)
|
2021-07-07 04:43:42 +03:00
|
|
|
| RMUntrackFunction !(AnyBackend UnTrackFunction)
|
2021-10-07 16:02:19 +03:00
|
|
|
| RMSetFunctionCustomization (AnyBackend SetFunctionCustomization)
|
2021-09-24 01:56:37 +03:00
|
|
|
| -- Functions permissions
|
|
|
|
RMCreateFunctionPermission !(AnyBackend FunctionPermissionArgument)
|
|
|
|
| RMDropFunctionPermission !(AnyBackend FunctionPermissionArgument)
|
|
|
|
| -- Computed fields (PG-specific)
|
|
|
|
RMAddComputedField !(AddComputedField ('Postgres 'Vanilla))
|
2021-07-07 04:43:42 +03:00
|
|
|
| RMDropComputedField !(DropComputedField ('Postgres 'Vanilla))
|
2021-09-24 01:56:37 +03:00
|
|
|
| -- Tables event triggers
|
|
|
|
RMCreateEventTrigger !(AnyBackend CreateEventTriggerQuery)
|
|
|
|
| RMDeleteEventTrigger !(AnyBackend DeleteEventTriggerQuery)
|
|
|
|
| RMRedeliverEvent !(AnyBackend RedeliverEventQuery)
|
|
|
|
| RMInvokeEventTrigger !(AnyBackend InvokeEventTriggerQuery)
|
|
|
|
| -- Remote schemas
|
|
|
|
RMAddRemoteSchema !AddRemoteSchemaQuery
|
|
|
|
| RMUpdateRemoteSchema !AddRemoteSchemaQuery
|
|
|
|
| RMRemoveRemoteSchema !RemoteSchemaNameQuery
|
|
|
|
| RMReloadRemoteSchema !RemoteSchemaNameQuery
|
2021-01-07 12:04:22 +03:00
|
|
|
| RMIntrospectRemoteSchema !RemoteSchemaNameQuery
|
2021-09-24 01:56:37 +03:00
|
|
|
| -- Remote schemas permissions
|
|
|
|
RMAddRemoteSchemaPermissions !AddRemoteSchemaPermission
|
2021-01-18 13:38:34 +03:00
|
|
|
| RMDropRemoteSchemaPermissions !DropRemoteSchemaPermissions
|
2021-09-24 01:56:37 +03:00
|
|
|
| -- Scheduled triggers
|
|
|
|
RMCreateCronTrigger !CreateCronTrigger
|
|
|
|
| RMDeleteCronTrigger !ScheduledTriggerName
|
2021-01-07 12:04:22 +03:00
|
|
|
| RMCreateScheduledEvent !CreateScheduledEvent
|
|
|
|
| RMDeleteScheduledEvent !DeleteScheduledEvent
|
2021-09-24 01:56:37 +03:00
|
|
|
| RMGetScheduledEvents !GetScheduledEvents
|
|
|
|
| RMGetEventInvocations !GetEventInvocations
|
|
|
|
| -- Actions
|
|
|
|
RMCreateAction !CreateAction
|
|
|
|
| RMDropAction !DropAction
|
|
|
|
| RMUpdateAction !UpdateAction
|
2021-01-07 12:04:22 +03:00
|
|
|
| RMCreateActionPermission !CreateActionPermission
|
2021-09-24 01:56:37 +03:00
|
|
|
| RMDropActionPermission !DropActionPermission
|
|
|
|
| -- Query collections, allow list related
|
|
|
|
RMCreateQueryCollection !CreateCollection
|
|
|
|
| RMDropQueryCollection !DropCollection
|
|
|
|
| RMAddQueryToCollection !AddQueryToCollection
|
|
|
|
| RMDropQueryFromCollection !DropQueryFromCollection
|
|
|
|
| RMAddCollectionToAllowlist !CollectionReq
|
2021-07-07 04:43:42 +03:00
|
|
|
| RMDropCollectionFromAllowlist !CollectionReq
|
2021-09-24 01:56:37 +03:00
|
|
|
| -- Rest endpoints
|
|
|
|
RMCreateRestEndpoint !CreateEndpoint
|
|
|
|
| RMDropRestEndpoint !DropEndpoint
|
|
|
|
| -- Custom types
|
|
|
|
RMSetCustomTypes !CustomTypes
|
|
|
|
| -- Api limits
|
|
|
|
RMSetApiLimits !ApiLimit
|
2021-02-11 20:54:25 +03:00
|
|
|
| RMRemoveApiLimits
|
2021-09-24 01:56:37 +03:00
|
|
|
| -- Metrics config
|
|
|
|
RMSetMetricsConfig !MetricsConfig
|
2021-02-11 20:54:25 +03:00
|
|
|
| RMRemoveMetricsConfig
|
2021-09-24 01:56:37 +03:00
|
|
|
| -- Inherited roles
|
|
|
|
RMAddInheritedRole !InheritedRole
|
[Preview] Inherited roles for postgres read queries
fixes #3868
docker image - `hasura/graphql-engine:inherited-roles-preview-48b73a2de`
Note:
To be able to use the inherited roles feature, the graphql-engine should be started with the env variable `HASURA_GRAPHQL_EXPERIMENTAL_FEATURES` set to `inherited_roles`.
Introduction
------------
This PR implements the idea of multiple roles as presented in this [paper](https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/FGALanguageICDE07.pdf). The multiple roles feature in this PR can be used via inherited roles. An inherited role is a role which can be created by combining multiple singular roles. For example, if there are two roles `author` and `editor` configured in the graphql-engine, then we can create a inherited role with the name of `combined_author_editor` role which will combine the select permissions of the `author` and `editor` roles and then make GraphQL queries using the `combined_author_editor`.
How are select permissions of different roles are combined?
------------------------------------------------------------
A select permission includes 5 things:
1. Columns accessible to the role
2. Row selection filter
3. Limit
4. Allow aggregation
5. Scalar computed fields accessible to the role
Suppose there are two roles, `role1` gives access to the `address` column with row filter `P1` and `role2` gives access to both the `address` and the `phone` column with row filter `P2` and we create a new role `combined_roles` which combines `role1` and `role2`.
Let's say the following GraphQL query is queried with the `combined_roles` role.
```graphql
query {
employees {
address
phone
}
}
```
This will translate to the following SQL query:
```sql
select
(case when (P1 or P2) then address else null end) as address,
(case when P2 then phone else null end) as phone
from employee
where (P1 or P2)
```
The other parameters of the select permission will be combined in the following manner:
1. Limit - Minimum of the limits will be the limit of the inherited role
2. Allow aggregations - If any of the role allows aggregation, then the inherited role will allow aggregation
3. Scalar computed fields - same as table column fields, as in the above example
APIs for inherited roles:
----------------------
1. `add_inherited_role`
`add_inherited_role` is the [metadata API](https://hasura.io/docs/1.0/graphql/core/api-reference/index.html#schema-metadata-api) to create a new inherited role. It accepts two arguments
`role_name`: the name of the inherited role to be added (String)
`role_set`: list of roles that need to be combined (Array of Strings)
Example:
```json
{
"type": "add_inherited_role",
"args": {
"role_name":"combined_user",
"role_set":[
"user",
"user1"
]
}
}
```
After adding the inherited role, the inherited role can be used like single roles like earlier
Note:
An inherited role can only be created with non-inherited/singular roles.
2. `drop_inherited_role`
The `drop_inherited_role` API accepts the name of the inherited role and drops it from the metadata. It accepts a single argument:
`role_name`: name of the inherited role to be dropped
Example:
```json
{
"type": "drop_inherited_role",
"args": {
"role_name":"combined_user"
}
}
```
Metadata
---------
The derived roles metadata will be included under the `experimental_features` key while exporting the metadata.
```json
{
"experimental_features": {
"derived_roles": [
{
"role_name": "manager_is_employee_too",
"role_set": [
"employee",
"manager"
]
}
]
}
}
```
Scope
------
Only postgres queries and subscriptions are supported in this PR.
Important points:
-----------------
1. All columns exposed to an inherited role will be marked as `nullable`, this is done so that cell value nullification can be done.
TODOs
-------
- [ ] Tests
- [ ] Test a GraphQL query running with a inherited role without enabling inherited roles in experimental features
- [] Tests for aggregate queries, limit, computed fields, functions, subscriptions (?)
- [ ] Introspection test with a inherited role (nullability changes in a inherited role)
- [ ] Docs
- [ ] Changelog
Co-authored-by: Vamshi Surabhi <6562944+0x777@users.noreply.github.com>
GitOrigin-RevId: 3b8ee1e11f5ceca80fe294f8c074d42fbccfec63
2021-03-08 14:14:13 +03:00
|
|
|
| RMDropInheritedRole !DropInheritedRole
|
2021-09-24 01:56:37 +03:00
|
|
|
| -- Metadata management
|
|
|
|
RMReplaceMetadata !ReplaceMetadata
|
|
|
|
| RMExportMetadata !ExportMetadata
|
|
|
|
| RMClearMetadata !ClearMetadata
|
|
|
|
| RMReloadMetadata !ReloadMetadata
|
|
|
|
| RMGetInconsistentMetadata !GetInconsistentMetadata
|
2021-07-07 04:43:42 +03:00
|
|
|
| RMDropInconsistentMetadata !DropInconsistentMetadata
|
2021-09-24 01:56:37 +03:00
|
|
|
| -- Introspection options
|
|
|
|
RMSetGraphqlSchemaIntrospectionOptions !SetGraphqlIntrospectionOptions
|
|
|
|
| -- Network
|
|
|
|
RMAddHostToTLSAllowlist !AddHostToTLSAllowlist
|
2021-08-24 10:36:32 +03:00
|
|
|
| RMDropHostFromTLSAllowlist !DropHostFromTLSAllowlist
|
2021-09-24 01:56:37 +03:00
|
|
|
| -- QueryTags
|
|
|
|
RMSetQueryTagsConfig !SetQueryTagsConfig
|
|
|
|
| -- Debug
|
|
|
|
RMDumpInternalState !DumpInternalState
|
|
|
|
| RMGetCatalogState !GetCatalogState
|
|
|
|
| RMSetCatalogState !SetCatalogState
|
2021-09-29 11:13:30 +03:00
|
|
|
| RMTestWebhookTransform !TestWebhookTransform
|
2021-09-24 01:56:37 +03:00
|
|
|
| -- Bulk metadata queries
|
|
|
|
RMBulk [RQLMetadataRequest]
|
2021-01-07 12:04:22 +03:00
|
|
|
|
2021-07-07 04:43:42 +03:00
|
|
|
instance FromJSON RQLMetadataV1 where
|
2021-09-24 01:56:37 +03:00
|
|
|
parseJSON = withObject "RQLMetadataV1" \o -> do
|
2021-07-07 04:43:42 +03:00
|
|
|
queryType <- o .: "type"
|
2021-09-24 01:56:37 +03:00
|
|
|
let args :: forall a. FromJSON a => A.Parser a
|
|
|
|
args = o .: "args"
|
2021-07-07 04:43:42 +03:00
|
|
|
case queryType of
|
|
|
|
-- backend agnostic
|
2021-09-24 01:56:37 +03:00
|
|
|
"rename_source" -> RMRenameSource <$> args
|
|
|
|
"add_remote_schema" -> RMAddRemoteSchema <$> args
|
|
|
|
"update_remote_schema" -> RMUpdateRemoteSchema <$> args
|
|
|
|
"remove_remote_schema" -> RMRemoveRemoteSchema <$> args
|
|
|
|
"reload_remote_schema" -> RMReloadRemoteSchema <$> args
|
|
|
|
"introspect_remote_schema" -> RMIntrospectRemoteSchema <$> args
|
|
|
|
"add_remote_schema_permissions" -> RMAddRemoteSchemaPermissions <$> args
|
|
|
|
"drop_remote_schema_permissions" -> RMDropRemoteSchemaPermissions <$> args
|
|
|
|
"create_cron_trigger" -> RMCreateCronTrigger <$> args
|
|
|
|
"delete_cron_trigger" -> RMDeleteCronTrigger <$> args
|
|
|
|
"create_scheduled_event" -> RMCreateScheduledEvent <$> args
|
|
|
|
"delete_scheduled_event" -> RMDeleteScheduledEvent <$> args
|
|
|
|
"get_scheduled_events" -> RMGetScheduledEvents <$> args
|
|
|
|
"get_event_invocations" -> RMGetEventInvocations <$> args
|
|
|
|
"create_action" -> RMCreateAction <$> args
|
|
|
|
"drop_action" -> RMDropAction <$> args
|
|
|
|
"update_action" -> RMUpdateAction <$> args
|
|
|
|
"create_action_permission" -> RMCreateActionPermission <$> args
|
|
|
|
"drop_action_permission" -> RMDropActionPermission <$> args
|
|
|
|
"create_query_collection" -> RMCreateQueryCollection <$> args
|
|
|
|
"drop_query_collection" -> RMDropQueryCollection <$> args
|
|
|
|
"add_query_to_collection" -> RMAddQueryToCollection <$> args
|
|
|
|
"drop_query_from_collection" -> RMDropQueryFromCollection <$> args
|
|
|
|
"add_collection_to_allowlist" -> RMAddCollectionToAllowlist <$> args
|
|
|
|
"drop_collection_from_allowlist" -> RMDropCollectionFromAllowlist <$> args
|
|
|
|
"create_rest_endpoint" -> RMCreateRestEndpoint <$> args
|
|
|
|
"drop_rest_endpoint" -> RMDropRestEndpoint <$> args
|
|
|
|
"set_custom_types" -> RMSetCustomTypes <$> args
|
|
|
|
"set_api_limits" -> RMSetApiLimits <$> args
|
|
|
|
"remove_api_limits" -> pure RMRemoveApiLimits
|
|
|
|
"set_metrics_config" -> RMSetMetricsConfig <$> args
|
|
|
|
"remove_metrics_config" -> pure RMRemoveMetricsConfig
|
|
|
|
"add_inherited_role" -> RMAddInheritedRole <$> args
|
|
|
|
"drop_inherited_role" -> RMDropInheritedRole <$> args
|
|
|
|
"replace_metadata" -> RMReplaceMetadata <$> args
|
|
|
|
"export_metadata" -> RMExportMetadata <$> args
|
|
|
|
"clear_metadata" -> RMClearMetadata <$> args
|
|
|
|
"reload_metadata" -> RMReloadMetadata <$> args
|
|
|
|
"get_inconsistent_metadata" -> RMGetInconsistentMetadata <$> args
|
|
|
|
"drop_inconsistent_metadata" -> RMDropInconsistentMetadata <$> args
|
|
|
|
"add_host_to_tls_allowlist" -> RMAddHostToTLSAllowlist <$> args
|
|
|
|
"drop_host_from_tls_allowlist" -> RMDropHostFromTLSAllowlist <$> args
|
|
|
|
"dump_internal_state" -> RMDumpInternalState <$> args
|
|
|
|
"get_catalog_state" -> RMGetCatalogState <$> args
|
|
|
|
"set_catalog_state" -> RMSetCatalogState <$> args
|
2021-07-07 04:43:42 +03:00
|
|
|
"set_graphql_schema_introspection_options" -> RMSetGraphqlSchemaIntrospectionOptions <$> args
|
2021-09-29 11:13:30 +03:00
|
|
|
"test_webhook_transform" -> RMTestWebhookTransform <$> args
|
2021-09-24 01:56:37 +03:00
|
|
|
"set_query_tags" -> RMSetQueryTagsConfig <$> args
|
|
|
|
"bulk" -> RMBulk <$> args
|
2021-07-07 04:43:42 +03:00
|
|
|
-- backend specific
|
|
|
|
_ -> do
|
|
|
|
let (prefix, T.drop 1 -> cmd) = T.breakOn "_" queryType
|
2021-09-24 01:56:37 +03:00
|
|
|
backendType <-
|
|
|
|
runAesonParser parseJSON (String prefix)
|
|
|
|
`onLeft` \_ ->
|
|
|
|
fail
|
|
|
|
( "unknown metadata command \"" <> T.unpack queryType
|
|
|
|
<> "\"; \""
|
|
|
|
<> T.unpack prefix
|
|
|
|
<> "\" was not recognized as a valid backend name"
|
|
|
|
)
|
2021-07-07 04:43:42 +03:00
|
|
|
dispatchAnyBackend @BackendAPI (liftTag backendType) \(_ :: BackendTag b) -> do
|
|
|
|
argValue <- args
|
2021-09-24 01:56:37 +03:00
|
|
|
command <- choice <$> sequenceA [p cmd argValue | p <- metadataV1CommandParsers @b]
|
|
|
|
onNothing command $
|
|
|
|
fail $
|
|
|
|
"unknown metadata command \"" <> T.unpack cmd
|
|
|
|
<> "\" for backend "
|
|
|
|
<> T.unpack (T.toTxt backendType)
|
2021-07-07 04:43:42 +03:00
|
|
|
|
2021-02-16 11:08:19 +03:00
|
|
|
data RQLMetadataV2
|
|
|
|
= RMV2ReplaceMetadata !ReplaceMetadataV2
|
2021-09-24 01:56:37 +03:00
|
|
|
| RMV2ExportMetadata !ExportMetadata
|
Clean metadata arguments
## Description
Thanks to #1664, the Metadata API types no longer require a `ToJSON` instance. This PR follows up with a cleanup of the types of the arguments to the metadata API:
- whenever possible, it moves those argument types to where they're used (RQL.DDL.*)
- it removes all unrequired instances (mostly `ToJSON`)
This PR does not attempt to do it for _all_ such argument types. For some of the metadata operations, the type used to describe the argument to the API and used to represent the value in the metadata are one and the same (like for `CreateEndpoint`). Sometimes, the two types are intertwined in complex ways (`RemoteRelationship` and `RemoteRelationshipDef`). In the spirit of only doing uncontroversial cleaning work, this PR only moves types that are not used outside of RQL.DDL.
Furthermore, this is a small step towards separating the different types all jumbled together in RQL.Types.
## Notes
This PR also improves several `FromJSON` instances to make use of `withObject`, and to use a human readable string instead of a type name in error messages whenever possible. For instance:
- before: `expected Object for Object, but encountered X`
after: `expected Object for add computed field, but encountered X`
- before: `Expecting an object for update query`
after: `expected Object for update query, but encountered X`
This PR also renames `CreateFunctionPermission` to `FunctionPermissionArgument`, to remove the quite surprising `type DropFunctionPermission = CreateFunctionPermission`.
This PR also deletes some dead code, mostly in RQL.DML.
This PR also moves a PG-specific source resolving function from DDL.Schema.Source to the only place where it is used: App.hs.
https://github.com/hasura/graphql-engine-mono/pull/1844
GitOrigin-RevId: a594521194bb7fe6a111b02a9e099896f9fed59c
2021-07-27 13:41:42 +03:00
|
|
|
deriving (Generic)
|
2021-07-07 04:43:42 +03:00
|
|
|
|
|
|
|
instance FromJSON RQLMetadataV2 where
|
2021-09-24 01:56:37 +03:00
|
|
|
parseJSON =
|
|
|
|
genericParseJSON $
|
|
|
|
defaultOptions
|
|
|
|
{ constructorTagModifier = snakeCase . drop 4,
|
|
|
|
sumEncoding = TaggedObject "type" "args"
|
|
|
|
}
|
2021-02-16 11:08:19 +03:00
|
|
|
|
2021-02-19 05:39:30 +03:00
|
|
|
data RQLMetadataRequest
|
2021-02-16 11:08:19 +03:00
|
|
|
= RMV1 !RQLMetadataV1
|
|
|
|
| RMV2 !RQLMetadataV2
|
|
|
|
|
2021-02-19 05:39:30 +03:00
|
|
|
instance FromJSON RQLMetadataRequest where
|
|
|
|
parseJSON = withObject "RQLMetadataRequest" $ \o -> do
|
2021-02-16 11:08:19 +03:00
|
|
|
version <- o .:? "version" .!= VIVersion1
|
|
|
|
let val = Object o
|
|
|
|
case version of
|
|
|
|
VIVersion1 -> RMV1 <$> parseJSON val
|
|
|
|
VIVersion2 -> RMV2 <$> parseJSON val
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
data RQLMetadata = RQLMetadata
|
|
|
|
{ _rqlMetadataResourceVersion :: !(Maybe MetadataResourceVersion),
|
|
|
|
_rqlMetadata :: !RQLMetadataRequest
|
Clean metadata arguments
## Description
Thanks to #1664, the Metadata API types no longer require a `ToJSON` instance. This PR follows up with a cleanup of the types of the arguments to the metadata API:
- whenever possible, it moves those argument types to where they're used (RQL.DDL.*)
- it removes all unrequired instances (mostly `ToJSON`)
This PR does not attempt to do it for _all_ such argument types. For some of the metadata operations, the type used to describe the argument to the API and used to represent the value in the metadata are one and the same (like for `CreateEndpoint`). Sometimes, the two types are intertwined in complex ways (`RemoteRelationship` and `RemoteRelationshipDef`). In the spirit of only doing uncontroversial cleaning work, this PR only moves types that are not used outside of RQL.DDL.
Furthermore, this is a small step towards separating the different types all jumbled together in RQL.Types.
## Notes
This PR also improves several `FromJSON` instances to make use of `withObject`, and to use a human readable string instead of a type name in error messages whenever possible. For instance:
- before: `expected Object for Object, but encountered X`
after: `expected Object for add computed field, but encountered X`
- before: `Expecting an object for update query`
after: `expected Object for update query, but encountered X`
This PR also renames `CreateFunctionPermission` to `FunctionPermissionArgument`, to remove the quite surprising `type DropFunctionPermission = CreateFunctionPermission`.
This PR also deletes some dead code, mostly in RQL.DML.
This PR also moves a PG-specific source resolving function from DDL.Schema.Source to the only place where it is used: App.hs.
https://github.com/hasura/graphql-engine-mono/pull/1844
GitOrigin-RevId: a594521194bb7fe6a111b02a9e099896f9fed59c
2021-07-27 13:41:42 +03:00
|
|
|
}
|
2021-02-19 05:39:30 +03:00
|
|
|
|
|
|
|
instance FromJSON RQLMetadata where
|
|
|
|
parseJSON = withObject "RQLMetadata" $ \o -> do
|
|
|
|
_rqlMetadataResourceVersion <- o .:? "resource_version"
|
|
|
|
_rqlMetadata <- parseJSON $ Object o
|
2021-09-24 01:56:37 +03:00
|
|
|
pure RQLMetadata {..}
|
|
|
|
|
|
|
|
runMetadataQuery ::
|
2021-10-13 19:38:56 +03:00
|
|
|
( MonadIO m,
|
2021-09-24 01:56:37 +03:00
|
|
|
MonadBaseControl IO m,
|
|
|
|
Tracing.MonadTrace m,
|
|
|
|
MonadMetadataStorage m,
|
|
|
|
MonadResolveSource m
|
|
|
|
) =>
|
|
|
|
Env.Environment ->
|
|
|
|
L.Logger L.Hasura ->
|
|
|
|
InstanceId ->
|
|
|
|
UserInfo ->
|
|
|
|
HTTP.Manager ->
|
|
|
|
ServerConfigCtx ->
|
|
|
|
RebuildableSchemaCache ->
|
|
|
|
RQLMetadata ->
|
|
|
|
m (EncJSON, RebuildableSchemaCache)
|
|
|
|
runMetadataQuery env logger instanceId userInfo httpManager serverConfigCtx schemaCache RQLMetadata {..} = do
|
2021-02-19 05:39:30 +03:00
|
|
|
(metadata, currentResourceVersion) <- fetchMetadata
|
2021-01-07 12:04:22 +03:00
|
|
|
((r, modMetadata), modSchemaCache, cacheInvalidations) <-
|
2021-02-19 05:39:30 +03:00
|
|
|
runMetadataQueryM env currentResourceVersion _rqlMetadata
|
2021-09-24 01:56:37 +03:00
|
|
|
& flip runReaderT logger
|
|
|
|
& runMetadataT metadata
|
|
|
|
& runCacheRWT schemaCache
|
|
|
|
& peelRun (RunCtx userInfo httpManager serverConfigCtx)
|
|
|
|
& runExceptT
|
|
|
|
& liftEitherM
|
2021-01-07 12:04:22 +03:00
|
|
|
-- set modified metadata in storage
|
2021-05-21 05:46:58 +03:00
|
|
|
if queryModifiesMetadata _rqlMetadata
|
2021-09-24 01:56:37 +03:00
|
|
|
then case _sccMaintenanceMode serverConfigCtx of
|
|
|
|
MaintenanceModeDisabled -> do
|
|
|
|
-- set modified metadata in storage
|
|
|
|
newResourceVersion <- setMetadata (fromMaybe currentResourceVersion _rqlMetadataResourceVersion) modMetadata
|
|
|
|
-- notify schema cache sync
|
|
|
|
notifySchemaCacheSync newResourceVersion instanceId cacheInvalidations
|
|
|
|
(_, modSchemaCache', _) <-
|
|
|
|
setMetadataResourceVersionInSchemaCache newResourceVersion
|
2021-04-06 06:25:02 +03:00
|
|
|
& runCacheRWT modSchemaCache
|
|
|
|
& peelRun (RunCtx userInfo httpManager serverConfigCtx)
|
|
|
|
& runExceptT
|
|
|
|
& liftEitherM
|
2021-08-24 10:36:32 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
pure (r, modSchemaCache')
|
|
|
|
MaintenanceModeEnabled ->
|
|
|
|
throw500 "metadata cannot be modified in maintenance mode"
|
|
|
|
else pure (r, modSchemaCache)
|
2021-01-07 12:04:22 +03:00
|
|
|
|
2021-02-19 05:39:30 +03:00
|
|
|
queryModifiesMetadata :: RQLMetadataRequest -> Bool
|
2021-02-18 19:46:14 +03:00
|
|
|
queryModifiesMetadata = \case
|
|
|
|
RMV1 q ->
|
|
|
|
case q of
|
2021-09-24 01:56:37 +03:00
|
|
|
RMRedeliverEvent _ -> False
|
|
|
|
RMInvokeEventTrigger _ -> False
|
2021-04-27 07:22:32 +03:00
|
|
|
RMGetInconsistentMetadata _ -> False
|
2021-09-24 01:56:37 +03:00
|
|
|
RMIntrospectRemoteSchema _ -> False
|
|
|
|
RMDumpInternalState _ -> False
|
|
|
|
RMSetCatalogState _ -> False
|
|
|
|
RMGetCatalogState _ -> False
|
|
|
|
RMExportMetadata _ -> False
|
|
|
|
RMGetEventInvocations _ -> False
|
|
|
|
RMGetScheduledEvents _ -> False
|
|
|
|
RMCreateScheduledEvent _ -> False
|
|
|
|
RMDeleteScheduledEvent _ -> False
|
2021-10-05 02:49:51 +03:00
|
|
|
RMTestWebhookTransform _ -> False
|
2021-09-24 01:56:37 +03:00
|
|
|
RMBulk qs -> any queryModifiesMetadata qs
|
|
|
|
_ -> True
|
2021-02-19 05:39:30 +03:00
|
|
|
RMV2 q ->
|
|
|
|
case q of
|
2021-04-27 07:22:32 +03:00
|
|
|
RMV2ExportMetadata _ -> False
|
2021-09-24 01:56:37 +03:00
|
|
|
_ -> True
|
|
|
|
|
|
|
|
runMetadataQueryM ::
|
2021-10-13 19:38:56 +03:00
|
|
|
( MonadIO m,
|
2021-09-24 01:56:37 +03:00
|
|
|
MonadBaseControl IO m,
|
|
|
|
CacheRWM m,
|
|
|
|
Tracing.MonadTrace m,
|
|
|
|
UserInfoM m,
|
|
|
|
HTTP.HasHttpManagerM m,
|
|
|
|
MetadataM m,
|
|
|
|
MonadMetadataStorageQueryAPI m,
|
|
|
|
HasServerConfigCtx m,
|
|
|
|
MonadReader r m,
|
|
|
|
Has (L.Logger L.Hasura) r
|
|
|
|
) =>
|
|
|
|
Env.Environment ->
|
|
|
|
MetadataResourceVersion ->
|
|
|
|
RQLMetadataRequest ->
|
|
|
|
m EncJSON
|
|
|
|
runMetadataQueryM env currentResourceVersion =
|
|
|
|
withPathK "args" . \case
|
|
|
|
RMV1 q -> runMetadataQueryV1M env currentResourceVersion q
|
|
|
|
RMV2 q -> runMetadataQueryV2M currentResourceVersion q
|
|
|
|
|
|
|
|
runMetadataQueryV1M ::
|
|
|
|
forall m r.
|
2021-10-13 19:38:56 +03:00
|
|
|
( MonadIO m,
|
2021-09-24 01:56:37 +03:00
|
|
|
MonadBaseControl IO m,
|
|
|
|
CacheRWM m,
|
|
|
|
Tracing.MonadTrace m,
|
|
|
|
UserInfoM m,
|
|
|
|
HTTP.HasHttpManagerM m,
|
|
|
|
MetadataM m,
|
|
|
|
MonadMetadataStorageQueryAPI m,
|
|
|
|
HasServerConfigCtx m,
|
|
|
|
MonadReader r m,
|
|
|
|
Has (L.Logger L.Hasura) r
|
|
|
|
) =>
|
|
|
|
Env.Environment ->
|
|
|
|
MetadataResourceVersion ->
|
|
|
|
RQLMetadataV1 ->
|
|
|
|
m EncJSON
|
2021-02-19 05:39:30 +03:00
|
|
|
runMetadataQueryV1M env currentResourceVersion = \case
|
2021-09-24 01:56:37 +03:00
|
|
|
RMAddSource q -> dispatchMetadata runAddSource q
|
|
|
|
RMDropSource q -> runDropSource q
|
|
|
|
RMRenameSource q -> runRenameSource q
|
|
|
|
RMTrackTable q -> dispatchMetadata runTrackTableV2Q q
|
|
|
|
RMUntrackTable q -> dispatchMetadata runUntrackTableQ q
|
2021-10-07 16:02:19 +03:00
|
|
|
RMSetFunctionCustomization q -> dispatchMetadata runSetFunctionCustomization q
|
2021-09-24 01:56:37 +03:00
|
|
|
RMSetTableCustomization q -> dispatchMetadata runSetTableCustomization q
|
|
|
|
RMPgSetTableIsEnum q -> runSetExistingTableIsEnumQ q
|
|
|
|
RMCreateInsertPermission q -> dispatchMetadata runCreatePerm q
|
|
|
|
RMCreateSelectPermission q -> dispatchMetadata runCreatePerm q
|
|
|
|
RMCreateUpdatePermission q -> dispatchMetadata runCreatePerm q
|
|
|
|
RMCreateDeletePermission q -> dispatchMetadata runCreatePerm q
|
|
|
|
RMDropInsertPermission q -> dispatchMetadata runDropPerm q
|
|
|
|
RMDropSelectPermission q -> dispatchMetadata runDropPerm q
|
|
|
|
RMDropUpdatePermission q -> dispatchMetadata runDropPerm q
|
|
|
|
RMDropDeletePermission q -> dispatchMetadata runDropPerm q
|
|
|
|
RMSetPermissionComment q -> dispatchMetadata runSetPermComment q
|
|
|
|
RMCreateObjectRelationship q -> dispatchMetadata (runCreateRelationship ObjRel . unCreateObjRel) q
|
|
|
|
RMCreateArrayRelationship q -> dispatchMetadata (runCreateRelationship ArrRel . unCreateArrRel) q
|
|
|
|
RMDropRelationship q -> dispatchMetadata runDropRel q
|
|
|
|
RMSetRelationshipComment q -> dispatchMetadata runSetRelComment q
|
|
|
|
RMRenameRelationship q -> dispatchMetadata runRenameRel q
|
|
|
|
RMCreateRemoteRelationship q -> dispatchMetadata runCreateRemoteRelationship q
|
|
|
|
RMUpdateRemoteRelationship q -> dispatchMetadata runUpdateRemoteRelationship q
|
|
|
|
RMDeleteRemoteRelationship q -> runDeleteRemoteRelationship q
|
|
|
|
RMTrackFunction q -> dispatchMetadata runTrackFunctionV2 q
|
|
|
|
RMUntrackFunction q -> dispatchMetadata runUntrackFunc q
|
|
|
|
RMCreateFunctionPermission q -> dispatchMetadata runCreateFunctionPermission q
|
|
|
|
RMDropFunctionPermission q -> dispatchMetadata runDropFunctionPermission q
|
|
|
|
RMAddComputedField q -> runAddComputedField q
|
|
|
|
RMDropComputedField q -> runDropComputedField q
|
|
|
|
RMCreateEventTrigger q -> dispatchMetadata runCreateEventTriggerQuery q
|
|
|
|
RMDeleteEventTrigger q -> dispatchMetadataAndEventTrigger runDeleteEventTriggerQuery q
|
|
|
|
RMRedeliverEvent q -> dispatchEventTrigger runRedeliverEvent q
|
|
|
|
RMInvokeEventTrigger q -> dispatchEventTrigger runInvokeEventTrigger q
|
|
|
|
RMAddRemoteSchema q -> runAddRemoteSchema env q
|
|
|
|
RMUpdateRemoteSchema q -> runUpdateRemoteSchema env q
|
|
|
|
RMRemoveRemoteSchema q -> runRemoveRemoteSchema q
|
|
|
|
RMReloadRemoteSchema q -> runReloadRemoteSchema q
|
|
|
|
RMIntrospectRemoteSchema q -> runIntrospectRemoteSchema q
|
|
|
|
RMAddRemoteSchemaPermissions q -> runAddRemoteSchemaPermissions q
|
|
|
|
RMDropRemoteSchemaPermissions q -> runDropRemoteSchemaPermissions q
|
|
|
|
RMCreateCronTrigger q -> runCreateCronTrigger q
|
|
|
|
RMDeleteCronTrigger q -> runDeleteCronTrigger q
|
|
|
|
RMCreateScheduledEvent q -> runCreateScheduledEvent q
|
|
|
|
RMDeleteScheduledEvent q -> runDeleteScheduledEvent q
|
|
|
|
RMGetScheduledEvents q -> runGetScheduledEvents q
|
|
|
|
RMGetEventInvocations q -> runGetEventInvocations q
|
|
|
|
RMCreateAction q -> runCreateAction q
|
|
|
|
RMDropAction q -> runDropAction q
|
|
|
|
RMUpdateAction q -> runUpdateAction q
|
|
|
|
RMCreateActionPermission q -> runCreateActionPermission q
|
|
|
|
RMDropActionPermission q -> runDropActionPermission q
|
|
|
|
RMCreateQueryCollection q -> runCreateCollection q
|
|
|
|
RMDropQueryCollection q -> runDropCollection q
|
|
|
|
RMAddQueryToCollection q -> runAddQueryToCollection q
|
|
|
|
RMDropQueryFromCollection q -> runDropQueryFromCollection q
|
|
|
|
RMAddCollectionToAllowlist q -> runAddCollectionToAllowlist q
|
|
|
|
RMDropCollectionFromAllowlist q -> runDropCollectionFromAllowlist q
|
|
|
|
RMCreateRestEndpoint q -> runCreateEndpoint q
|
|
|
|
RMDropRestEndpoint q -> runDropEndpoint q
|
|
|
|
RMSetCustomTypes q -> runSetCustomTypes q
|
|
|
|
RMSetApiLimits q -> runSetApiLimits q
|
|
|
|
RMRemoveApiLimits -> runRemoveApiLimits
|
|
|
|
RMSetMetricsConfig q -> runSetMetricsConfig q
|
|
|
|
RMRemoveMetricsConfig -> runRemoveMetricsConfig
|
|
|
|
RMAddInheritedRole q -> runAddInheritedRole q
|
|
|
|
RMDropInheritedRole q -> runDropInheritedRole q
|
|
|
|
RMReplaceMetadata q -> runReplaceMetadata q
|
|
|
|
RMExportMetadata q -> runExportMetadata q
|
|
|
|
RMClearMetadata q -> runClearMetadata q
|
|
|
|
RMReloadMetadata q -> runReloadMetadata q
|
|
|
|
RMGetInconsistentMetadata q -> runGetInconsistentMetadata q
|
|
|
|
RMDropInconsistentMetadata q -> runDropInconsistentMetadata q
|
2021-05-05 15:25:27 +03:00
|
|
|
RMSetGraphqlSchemaIntrospectionOptions q -> runSetGraphqlSchemaIntrospectionOptions q
|
2021-09-24 01:56:37 +03:00
|
|
|
RMAddHostToTLSAllowlist q -> runAddHostToTLSAllowlist q
|
|
|
|
RMDropHostFromTLSAllowlist q -> runDropHostFromTLSAllowlist q
|
|
|
|
RMDumpInternalState q -> runDumpInternalState q
|
|
|
|
RMGetCatalogState q -> runGetCatalogState q
|
|
|
|
RMSetCatalogState q -> runSetCatalogState q
|
2021-12-03 10:12:43 +03:00
|
|
|
RMTestWebhookTransform q -> runTestWebhookTransform env q
|
2021-09-24 01:56:37 +03:00
|
|
|
RMSetQueryTagsConfig q -> runSetQueryTagsConfig q
|
|
|
|
RMBulk q -> encJFromList <$> indexedMapM (runMetadataQueryM env currentResourceVersion) q
|
2021-07-07 04:43:42 +03:00
|
|
|
where
|
2021-09-24 01:56:37 +03:00
|
|
|
dispatchMetadata ::
|
|
|
|
(forall b. BackendMetadata b => i b -> a) ->
|
|
|
|
AnyBackend i ->
|
|
|
|
a
|
2021-09-06 14:15:36 +03:00
|
|
|
dispatchMetadata f x = dispatchAnyBackend @BackendMetadata x f
|
|
|
|
|
|
|
|
dispatchEventTrigger :: (forall b. BackendEventTrigger b => i b -> a) -> AnyBackend i -> a
|
|
|
|
dispatchEventTrigger f x = dispatchAnyBackend @BackendEventTrigger x f
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
dispatchMetadataAndEventTrigger ::
|
|
|
|
(forall b. (BackendMetadata b, BackendEventTrigger b) => i b -> a) ->
|
|
|
|
AnyBackend i ->
|
|
|
|
a
|
2021-09-09 14:54:19 +03:00
|
|
|
dispatchMetadataAndEventTrigger f x = dispatchAnyBackendWithTwoConstraints @BackendMetadata @BackendEventTrigger x f
|
2021-02-16 11:08:19 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
runMetadataQueryV2M ::
|
|
|
|
( MonadIO m,
|
|
|
|
CacheRWM m,
|
|
|
|
MetadataM m,
|
|
|
|
MonadMetadataStorageQueryAPI m,
|
|
|
|
MonadReader r m,
|
|
|
|
Has (L.Logger L.Hasura) r
|
|
|
|
) =>
|
|
|
|
MetadataResourceVersion ->
|
|
|
|
RQLMetadataV2 ->
|
|
|
|
m EncJSON
|
2021-02-19 05:39:30 +03:00
|
|
|
runMetadataQueryV2M currentResourceVersion = \case
|
2021-02-16 11:08:19 +03:00
|
|
|
RMV2ReplaceMetadata q -> runReplaceMetadataV2 q
|
2021-09-24 01:56:37 +03:00
|
|
|
RMV2ExportMetadata q -> runExportMetadataV2 currentResourceVersion q
|