2021-11-10 03:37:42 +03:00
|
|
|
-- | This module provides building blocks for the GraphQL Schema that the
|
|
|
|
-- GraphQL Engine presents.
|
|
|
|
--
|
|
|
|
-- The functions defined here are used to serve as default implementations for
|
|
|
|
-- their namesakes in the 'BackendSchema' type class.
|
|
|
|
--
|
|
|
|
-- When, for some backend, you want to implement a new feature that manifests
|
|
|
|
-- itself visibly in the schema (e.g., if you're developing support for update
|
|
|
|
-- mutations), this module is likely where your efforts should start.
|
|
|
|
--
|
|
|
|
-- Using these functions help us present a consistent GraphQL schema across
|
|
|
|
-- different backends.
|
|
|
|
--
|
|
|
|
-- There is a bit of tension however, as sometimes we intentionally do want the
|
|
|
|
-- GraphQL Schema relating to some backend to be different in some way.
|
|
|
|
--
|
|
|
|
-- It could be that a backend only has limited support for some common feature,
|
|
|
|
-- or, more interestingly, that some backend just does things differently (c.f.
|
|
|
|
-- MSSQL's @MERGE@ statement with PostgreSQL's @INSERT .. ON CONFLICT@, which
|
|
|
|
-- are similar enough that we want to use the same overall upsert schema but
|
|
|
|
-- different enough that we want to use different field names)
|
|
|
|
--
|
|
|
|
-- When you want to implement new schema for a backend, there is overall three
|
|
|
|
-- different ways do deal with this tension:
|
|
|
|
--
|
|
|
|
-- 1. You can duplicate existing code and implement the new behavior in the
|
|
|
|
-- duplicate.
|
|
|
|
-- 2. You can infuse the new behavior into existing code and switch dynamically
|
|
|
|
-- at runtime (or via type class instance dispatch, which is the same
|
|
|
|
-- for our purposes)
|
|
|
|
-- 3. You can refactor the existing building blocks and compose them differently
|
|
|
|
-- at use sites to get the desired behavior nuances.
|
|
|
|
--
|
|
|
|
-- Of these three, steps 1. and 2. are by far the easiest to execute, while 3.
|
|
|
|
-- requires some critical thought. However, both 1. and 2. produce legacy code
|
|
|
|
-- that is difficult to maintain and understand.
|
|
|
|
--
|
|
|
|
-- As a guideline, if you find yourself wanting add new behavior to some of
|
|
|
|
-- these functions it's very likely that you should consider refactoring them
|
|
|
|
-- instead, thus shifting the responsibility deciding on the correct behavior to
|
|
|
|
-- use sites.
|
|
|
|
--
|
|
|
|
-- It an ongoing effort to adapt and refactor these building blocks such that
|
|
|
|
-- they have the sizes and shapes that result in the most elegant uses of them
|
|
|
|
-- that we can manage.
|
2021-11-04 19:08:33 +03:00
|
|
|
module Hasura.GraphQL.Schema.Build
|
2022-06-29 19:03:25 +03:00
|
|
|
( buildTableDeleteMutationFields,
|
2021-11-04 19:08:33 +03:00
|
|
|
buildTableInsertMutationFields,
|
2022-06-07 08:32:08 +03:00
|
|
|
buildTableQueryAndSubscriptionFields,
|
2022-04-22 22:53:12 +03:00
|
|
|
buildTableStreamingSubscriptionFields,
|
2023-01-10 04:54:40 +03:00
|
|
|
buildSingleBatchTableUpdateMutationFields,
|
2022-07-18 18:15:34 +03:00
|
|
|
setFieldNameCase,
|
|
|
|
buildFieldDescription,
|
2021-11-04 19:08:33 +03:00
|
|
|
)
|
|
|
|
where
|
2021-02-09 15:47:21 +03:00
|
|
|
|
2022-05-26 14:54:30 +03:00
|
|
|
import Data.Has (getter)
|
|
|
|
import Data.Text.Casing qualified as C
|
2021-02-09 15:47:21 +03:00
|
|
|
import Data.Text.Extended
|
2022-07-25 18:53:25 +03:00
|
|
|
import Hasura.GraphQL.ApolloFederation
|
2023-01-10 04:54:40 +03:00
|
|
|
import Hasura.GraphQL.Schema.Backend (BackendTableSelectSchema (..), BackendUpdateOperatorsSchema (..), MonadBuildSchema)
|
2022-08-22 18:57:46 +03:00
|
|
|
import Hasura.GraphQL.Schema.BoolExp (AggregationPredicatesSchema)
|
2021-02-09 15:47:21 +03:00
|
|
|
import Hasura.GraphQL.Schema.Common
|
|
|
|
import Hasura.GraphQL.Schema.Mutation
|
server: Metadata origin for definitions (type parameter version v2)
The code that builds the GraphQL schema, and `buildGQLContext` in particular, is partial: not every value of `(ServerConfigCtx, GraphQLQueryType, SourceCache, HashMap RemoteSchemaName (RemoteSchemaCtx, MetadataObject), ActionCache, AnnotatedCustomTypes)` results in a valid GraphQL schema. When it fails, we want to be able to return better error messages than we currently do.
The key thing that is missing is a way to trace back GraphQL type information to their origin from the Hasura metadata. Currently, we have a number of correctness checks of our GraphQL schema. But these correctness checks only have access to pure GraphQL type information, and hence can only report errors in terms of that. Possibly the worst is the "conflicting definitions" error, which, in practice, can only be debugged by Hasura engineers. This is terrible DX for customers.
This PR allows us to print better error messages, by adding a field to the `Definition` type that traces the GraphQL type to its origin in the metadata. So the idea is simple: just add `MetadataObjId`, or `Maybe` that, or some other sum type of that, to `Definition`.
However, we want to avoid having to import a `Hasura.RQL` module from `Hasura.GraphQL.Parser`. So we instead define this additional field of `Definition` through a new type parameter, which is threaded through in `Hasura.GraphQL.Parser`. We then define type synonyms in `Hasura.GraphQL.Schema.Parser` that fill in this type parameter, so that it is not visible for the majority of the codebase.
The idea of associating metadata information to `Definition`s really comes to fruition when combined with hasura/graphql-engine-mono#4517. Their combination would allow us to use the API of fatal errors (just like the current `MonadError QErr`) to report _inconsistencies_ in the metadata. Such inconsistencies are then _automatically_ ignored. So no ad-hoc decisions need to be made on how to cut out inconsistent metadata from the GraphQL schema. This will allow us to report much better errors, as well as improve the likelihood of a successful HGE startup.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/4770
Co-authored-by: Samir Talwar <47582+SamirTalwar@users.noreply.github.com>
GitOrigin-RevId: 728402b0cae83ae8e83463a826ceeb609001acae
2022-06-28 18:52:26 +03:00
|
|
|
import Hasura.GraphQL.Schema.Parser hiding (EnumValueInfo, field)
|
2021-02-09 15:47:21 +03:00
|
|
|
import Hasura.GraphQL.Schema.Select
|
2022-04-22 22:53:12 +03:00
|
|
|
import Hasura.GraphQL.Schema.SubscriptionStream (selectStreamTable)
|
2022-08-17 15:46:36 +03:00
|
|
|
import Hasura.GraphQL.Schema.Table (getTableIdentifierName, tableSelectPermissions)
|
server: reduce schema contexts to the bare minimum
### Description
This monster of a PR took way too long. As the title suggests, it reduces the schema context carried in the readers to the very strict minimum. In practice, that means that to build a source, we only require:
- the global `SchemaContext`
- the global `SchemaOptions` (soon to be renamed `SchemaSourceOptions`)
- that source's `SourceInfo`
Furthermore, _we no longer carry "default" customization options throughout the schema_. All customization information is extracted from the `SourceInfo`, when required. This prevents an entire category of bugs we had previously encountered, such as parts of the code using uninitialized / unupdated customization info.
In turn, this meant that we could remove the explicit threading of the `SourceInfo` throughout the schema, since it is now always available through the reader context.
Finally, this meant making a few adjustments to relay and actions as well, such as the introduction of a new separate "context" for actions, and a change to how we create some of the action-specific postgres scalar parsers.
I'll highlight with review comments the areas of interest.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6709
GitOrigin-RevId: ea80fddcb24e2513779dd04b0b700a55f0028dd1
2022-11-17 13:34:05 +03:00
|
|
|
import Hasura.GraphQL.Schema.Typename
|
2023-01-10 04:54:40 +03:00
|
|
|
import Hasura.GraphQL.Schema.Update.Batch (updateTable, updateTableByPk)
|
2021-02-09 15:47:21 +03:00
|
|
|
import Hasura.Prelude
|
2021-06-11 06:26:50 +03:00
|
|
|
import Hasura.RQL.IR
|
2023-01-10 04:54:40 +03:00
|
|
|
import Hasura.RQL.IR.Update.Batch
|
2022-04-27 16:57:28 +03:00
|
|
|
import Hasura.RQL.Types.Backend
|
|
|
|
import Hasura.RQL.Types.Common
|
2022-06-07 08:32:08 +03:00
|
|
|
import Hasura.RQL.Types.Permission
|
2023-04-24 18:17:15 +03:00
|
|
|
import Hasura.RQL.Types.Schema.Options qualified as Options
|
2022-04-27 16:57:28 +03:00
|
|
|
import Hasura.RQL.Types.SchemaCache
|
Remove circular dependency in schema building code
### Description
The main goal of this PR is, as stated, to remove the circular dependency in the schema building code. This cycle arises from the existence of remote relationships: when we build the schema for a source A, a remote relationship might force us to jump to the schema of a source B, or some remote schema. As a result, we end up having to do a dispatch from a "leaf" of the schema, similar to the one done at the root. In turn, this forces us to carry along in the schema a lot of information required for that dispatch, AND it forces us to import the instances in scope, creating an import loop.
As discussed in #4489, this PR implements the "dependency injection" solution: we pass to the schema a function to call to do the dispatch, and to get a generated field for a remote relationship. That way, this function can be chosen at the root level, and the leaves need not be aware of the overall context.
This PR grew a bit bigger than that, however; in an attempt to try and remove the `SourceCache` from the schema altogether, it changed a lot of functions across the schema building code, to thread along the `SourceInfo b` of the source being built. This avoids having to do cache lookups within a given source. A few cases remain, such as relay, that we might try to tackle in a subsequent PR.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/4557
GitOrigin-RevId: 9388e48372877520a72a9fd1677005df9f7b2d72
2022-05-27 20:21:22 +03:00
|
|
|
import Hasura.RQL.Types.Source
|
2022-04-27 16:57:28 +03:00
|
|
|
import Hasura.RQL.Types.SourceCustomization
|
2023-05-17 11:53:31 +03:00
|
|
|
import Hasura.Table.Cache
|
2021-02-09 15:47:21 +03:00
|
|
|
import Language.GraphQL.Draft.Syntax qualified as G
|
|
|
|
|
2022-06-07 08:32:08 +03:00
|
|
|
-- | buildTableQueryAndSubscriptionFields builds the field parsers of a table.
|
|
|
|
-- It returns a tuple with array of field parsers that correspond to the field
|
|
|
|
-- parsers of the query root and the field parsers of the subscription root
|
|
|
|
buildTableQueryAndSubscriptionFields ::
|
2021-02-09 15:47:21 +03:00
|
|
|
forall b r m n.
|
2022-06-30 18:22:19 +03:00
|
|
|
( MonadBuildSchema b r m n,
|
2022-08-22 18:57:46 +03:00
|
|
|
AggregationPredicatesSchema b,
|
2022-06-30 18:22:19 +03:00
|
|
|
BackendTableSelectSchema b
|
|
|
|
) =>
|
2022-08-03 22:08:34 +03:00
|
|
|
MkRootFieldName ->
|
2021-02-09 15:47:21 +03:00
|
|
|
TableName b ->
|
|
|
|
TableInfo b ->
|
2022-05-26 14:54:30 +03:00
|
|
|
C.GQLNameIdentifier ->
|
2022-09-06 19:48:04 +03:00
|
|
|
SchemaT
|
|
|
|
r
|
|
|
|
m
|
2022-06-07 08:32:08 +03:00
|
|
|
( [FieldParser n (QueryDB b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b))],
|
2022-07-25 18:53:25 +03:00
|
|
|
[FieldParser n (QueryDB b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b))],
|
|
|
|
Maybe (G.Name, Parser 'Output n (ApolloFederationParserFunction n))
|
2022-06-07 08:32:08 +03:00
|
|
|
)
|
server: reduce schema contexts to the bare minimum
### Description
This monster of a PR took way too long. As the title suggests, it reduces the schema context carried in the readers to the very strict minimum. In practice, that means that to build a source, we only require:
- the global `SchemaContext`
- the global `SchemaOptions` (soon to be renamed `SchemaSourceOptions`)
- that source's `SourceInfo`
Furthermore, _we no longer carry "default" customization options throughout the schema_. All customization information is extracted from the `SourceInfo`, when required. This prevents an entire category of bugs we had previously encountered, such as parts of the code using uninitialized / unupdated customization info.
In turn, this meant that we could remove the explicit threading of the `SourceInfo` throughout the schema, since it is now always available through the reader context.
Finally, this meant making a few adjustments to relay and actions as well, such as the introduction of a new separate "context" for actions, and a change to how we create some of the action-specific postgres scalar parsers.
I'll highlight with review comments the areas of interest.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6709
GitOrigin-RevId: ea80fddcb24e2513779dd04b0b700a55f0028dd1
2022-11-17 13:34:05 +03:00
|
|
|
buildTableQueryAndSubscriptionFields mkRootFieldName tableName tableInfo gqlName = do
|
|
|
|
sourceInfo :: SourceInfo b <- asks getter
|
Move RoleName into SchemaContext.
### Description
I am not 100% sure about this PR; while I think the code is better this way, I'm willing to be convinced otherwise.
In short, this PR moves the `RoleName` field into the `SchemaContext`, instead of being a nebulous `Has RoleName` constraint on the reader monad. The major upside of this is that it makes it an explicit named field, rather than something that must be given as part of a tuple of arguments when calling `runReader`.
However, the downside is that it breaks the helper permissions functions of `Schema.Table`, which relied on `Has RoleName r`. This PR makes the choice of passing the role name explicitly to all of those functions, which in turn means first explicitly fetching the role name in a lot of places. It makes it more explicit when a schema building block relies on the role name, but is a bit verbose...
### Alternatives
Some alternatives worth considering:
- attempting something like `Has context r, Has RoleName context`, which would allow them to be independent from the context but still fetch the role name from the reader, but might require type annotations to not be ambiguous
- keeping the permission functions the same, with `Has RoleName r`, and introducing a bunch of newtypes instead of using tuples to explicitly implement all the required `Has` instances
- changing the permission functions to `Has SchemaContext r`, since they are functions used only to build the schema, and therefore may be allowed to be tied to the context.
What do y'all think?
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5073
GitOrigin-RevId: 8fd09fafb54905a4d115ef30842d35da0c3db5d2
2022-07-29 18:37:09 +03:00
|
|
|
roleName <- retrieve scRole
|
server: reduce schema contexts to the bare minimum
### Description
This monster of a PR took way too long. As the title suggests, it reduces the schema context carried in the readers to the very strict minimum. In practice, that means that to build a source, we only require:
- the global `SchemaContext`
- the global `SchemaOptions` (soon to be renamed `SchemaSourceOptions`)
- that source's `SourceInfo`
Furthermore, _we no longer carry "default" customization options throughout the schema_. All customization information is extracted from the `SourceInfo`, when required. This prevents an entire category of bugs we had previously encountered, such as parts of the code using uninitialized / unupdated customization info.
In turn, this meant that we could remove the explicit threading of the `SourceInfo` throughout the schema, since it is now always available through the reader context.
Finally, this meant making a few adjustments to relay and actions as well, such as the introduction of a new separate "context" for actions, and a change to how we create some of the action-specific postgres scalar parsers.
I'll highlight with review comments the areas of interest.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6709
GitOrigin-RevId: ea80fddcb24e2513779dd04b0b700a55f0028dd1
2022-11-17 13:34:05 +03:00
|
|
|
let customization = _siCustomization sourceInfo
|
|
|
|
tCase = _rscNamingConvention customization
|
|
|
|
mkTypename = runMkTypename $ _rscTypeNames customization
|
|
|
|
-- select table
|
2022-08-03 22:08:34 +03:00
|
|
|
selectName = runMkRootFieldName mkRootFieldName $ setFieldNameCase tCase tableInfo _tcrfSelect mkSelectField gqlName
|
|
|
|
-- select table by pk
|
|
|
|
selectPKName = runMkRootFieldName mkRootFieldName $ setFieldNameCase tCase tableInfo _tcrfSelectByPk mkSelectByPkField gqlName
|
|
|
|
-- select table aggregate
|
|
|
|
selectAggName = runMkRootFieldName mkRootFieldName $ setFieldNameCase tCase tableInfo _tcrfSelectAggregate mkSelectAggregateField gqlName
|
2022-06-07 08:32:08 +03:00
|
|
|
|
server: reduce schema contexts to the bare minimum
### Description
This monster of a PR took way too long. As the title suggests, it reduces the schema context carried in the readers to the very strict minimum. In practice, that means that to build a source, we only require:
- the global `SchemaContext`
- the global `SchemaOptions` (soon to be renamed `SchemaSourceOptions`)
- that source's `SourceInfo`
Furthermore, _we no longer carry "default" customization options throughout the schema_. All customization information is extracted from the `SourceInfo`, when required. This prevents an entire category of bugs we had previously encountered, such as parts of the code using uninitialized / unupdated customization info.
In turn, this meant that we could remove the explicit threading of the `SourceInfo` throughout the schema, since it is now always available through the reader context.
Finally, this meant making a few adjustments to relay and actions as well, such as the introduction of a new separate "context" for actions, and a change to how we create some of the action-specific postgres scalar parsers.
I'll highlight with review comments the areas of interest.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6709
GitOrigin-RevId: ea80fddcb24e2513779dd04b0b700a55f0028dd1
2022-11-17 13:34:05 +03:00
|
|
|
selectTableParser <- optionalFieldParser QDBMultipleRows $ selectTable tableInfo selectName selectDesc
|
|
|
|
selectTableByPkParser <- optionalFieldParser QDBSingleRow $ selectTableByPk tableInfo selectPKName selectPKDesc
|
|
|
|
selectTableAggregateParser <- optionalFieldParser QDBAggregation $ selectTableAggregate tableInfo selectAggName selectAggDesc
|
2022-06-07 08:32:08 +03:00
|
|
|
|
Move RoleName into SchemaContext.
### Description
I am not 100% sure about this PR; while I think the code is better this way, I'm willing to be convinced otherwise.
In short, this PR moves the `RoleName` field into the `SchemaContext`, instead of being a nebulous `Has RoleName` constraint on the reader monad. The major upside of this is that it makes it an explicit named field, rather than something that must be given as part of a tuple of arguments when calling `runReader`.
However, the downside is that it breaks the helper permissions functions of `Schema.Table`, which relied on `Has RoleName r`. This PR makes the choice of passing the role name explicitly to all of those functions, which in turn means first explicitly fetching the role name in a lot of places. It makes it more explicit when a schema building block relies on the role name, but is a bit verbose...
### Alternatives
Some alternatives worth considering:
- attempting something like `Has context r, Has RoleName context`, which would allow them to be independent from the context but still fetch the role name from the reader, but might require type annotations to not be ambiguous
- keeping the permission functions the same, with `Has RoleName r`, and introducing a bunch of newtypes instead of using tuples to explicitly implement all the required `Has` instances
- changing the permission functions to `Has SchemaContext r`, since they are functions used only to build the schema, and therefore may be allowed to be tied to the context.
What do y'all think?
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5073
GitOrigin-RevId: 8fd09fafb54905a4d115ef30842d35da0c3db5d2
2022-07-29 18:37:09 +03:00
|
|
|
case tableSelectPermissions roleName tableInfo of
|
2022-06-07 08:32:08 +03:00
|
|
|
-- No select permission found for the current role, so
|
|
|
|
-- no root fields will be accessible to the role
|
2022-07-25 18:53:25 +03:00
|
|
|
Nothing -> pure (mempty, mempty, Nothing)
|
2022-06-07 08:32:08 +03:00
|
|
|
-- Filter the root fields which have been enabled
|
|
|
|
Just SelPermInfo {..} -> do
|
|
|
|
selectStreamParser <-
|
2022-08-25 11:23:16 +03:00
|
|
|
if (isRootFieldAllowed SRFTSelectStream spiAllowedSubscriptionRootFields)
|
server: reduce schema contexts to the bare minimum
### Description
This monster of a PR took way too long. As the title suggests, it reduces the schema context carried in the readers to the very strict minimum. In practice, that means that to build a source, we only require:
- the global `SchemaContext`
- the global `SchemaOptions` (soon to be renamed `SchemaSourceOptions`)
- that source's `SourceInfo`
Furthermore, _we no longer carry "default" customization options throughout the schema_. All customization information is extracted from the `SourceInfo`, when required. This prevents an entire category of bugs we had previously encountered, such as parts of the code using uninitialized / unupdated customization info.
In turn, this meant that we could remove the explicit threading of the `SourceInfo` throughout the schema, since it is now always available through the reader context.
Finally, this meant making a few adjustments to relay and actions as well, such as the introduction of a new separate "context" for actions, and a change to how we create some of the action-specific postgres scalar parsers.
I'll highlight with review comments the areas of interest.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6709
GitOrigin-RevId: ea80fddcb24e2513779dd04b0b700a55f0028dd1
2022-11-17 13:34:05 +03:00
|
|
|
then buildTableStreamingSubscriptionFields mkRootFieldName tableName tableInfo gqlName
|
2022-06-07 08:32:08 +03:00
|
|
|
else pure mempty
|
|
|
|
|
|
|
|
let (querySelectTableParser, subscriptionSelectTableParser) =
|
|
|
|
getQueryAndSubscriptionRootFields
|
|
|
|
selectTableParser
|
|
|
|
(isRootFieldAllowed QRFTSelect spiAllowedQueryRootFields)
|
|
|
|
(isRootFieldAllowed SRFTSelect spiAllowedSubscriptionRootFields)
|
|
|
|
|
|
|
|
(querySelectTableByPkParser, subscriptionSelectTableByPkParser) =
|
|
|
|
getQueryAndSubscriptionRootFields
|
|
|
|
selectTableByPkParser
|
|
|
|
(isRootFieldAllowed QRFTSelectByPk spiAllowedQueryRootFields)
|
|
|
|
(isRootFieldAllowed SRFTSelectByPk spiAllowedSubscriptionRootFields)
|
|
|
|
|
|
|
|
(querySelectTableAggParser, subscriptionSelectTableAggParser) =
|
|
|
|
getQueryAndSubscriptionRootFields
|
|
|
|
selectTableAggregateParser
|
|
|
|
(isRootFieldAllowed QRFTSelectAggregate spiAllowedQueryRootFields)
|
|
|
|
(isRootFieldAllowed SRFTSelectAggregate spiAllowedSubscriptionRootFields)
|
|
|
|
|
|
|
|
queryRootFields = catMaybes [querySelectTableParser, querySelectTableByPkParser, querySelectTableAggParser]
|
|
|
|
subscriptionRootFields =
|
|
|
|
selectStreamParser
|
|
|
|
<> catMaybes [subscriptionSelectTableParser, subscriptionSelectTableByPkParser, subscriptionSelectTableAggParser]
|
|
|
|
|
2022-07-25 18:53:25 +03:00
|
|
|
-- This parser is for generating apollo federation field _entities
|
|
|
|
apolloFedTableParser <- runMaybeT do
|
|
|
|
guard $ isApolloFedV1enabled (_tciApolloFederationConfig (_tiCoreInfo tableInfo))
|
server: reduce schema contexts to the bare minimum
### Description
This monster of a PR took way too long. As the title suggests, it reduces the schema context carried in the readers to the very strict minimum. In practice, that means that to build a source, we only require:
- the global `SchemaContext`
- the global `SchemaOptions` (soon to be renamed `SchemaSourceOptions`)
- that source's `SourceInfo`
Furthermore, _we no longer carry "default" customization options throughout the schema_. All customization information is extracted from the `SourceInfo`, when required. This prevents an entire category of bugs we had previously encountered, such as parts of the code using uninitialized / unupdated customization info.
In turn, this meant that we could remove the explicit threading of the `SourceInfo` throughout the schema, since it is now always available through the reader context.
Finally, this meant making a few adjustments to relay and actions as well, such as the introduction of a new separate "context" for actions, and a change to how we create some of the action-specific postgres scalar parsers.
I'll highlight with review comments the areas of interest.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6709
GitOrigin-RevId: ea80fddcb24e2513779dd04b0b700a55f0028dd1
2022-11-17 13:34:05 +03:00
|
|
|
tableSelSet <- MaybeT $ tableSelectionSet tableInfo
|
Move RoleName into SchemaContext.
### Description
I am not 100% sure about this PR; while I think the code is better this way, I'm willing to be convinced otherwise.
In short, this PR moves the `RoleName` field into the `SchemaContext`, instead of being a nebulous `Has RoleName` constraint on the reader monad. The major upside of this is that it makes it an explicit named field, rather than something that must be given as part of a tuple of arguments when calling `runReader`.
However, the downside is that it breaks the helper permissions functions of `Schema.Table`, which relied on `Has RoleName r`. This PR makes the choice of passing the role name explicitly to all of those functions, which in turn means first explicitly fetching the role name in a lot of places. It makes it more explicit when a schema building block relies on the role name, but is a bit verbose...
### Alternatives
Some alternatives worth considering:
- attempting something like `Has context r, Has RoleName context`, which would allow them to be independent from the context but still fetch the role name from the reader, but might require type annotations to not be ambiguous
- keeping the permission functions the same, with `Has RoleName r`, and introducing a bunch of newtypes instead of using tuples to explicitly implement all the required `Has` instances
- changing the permission functions to `Has SchemaContext r`, since they are functions used only to build the schema, and therefore may be allowed to be tied to the context.
What do y'all think?
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5073
GitOrigin-RevId: 8fd09fafb54905a4d115ef30842d35da0c3db5d2
2022-07-29 18:37:09 +03:00
|
|
|
selectPerm <- hoistMaybe $ tableSelectPermissions roleName tableInfo
|
2022-07-25 18:53:25 +03:00
|
|
|
stringifyNumbers <- retrieve Options.soStringifyNumbers
|
|
|
|
primaryKeys <- hoistMaybe $ fmap _pkColumns . _tciPrimaryKey . _tiCoreInfo $ tableInfo
|
|
|
|
let tableSelPerm = tablePermissionsInfo selectPerm
|
2022-08-17 15:46:36 +03:00
|
|
|
tableGQLName <- getTableIdentifierName tableInfo
|
server: reduce schema contexts to the bare minimum
### Description
This monster of a PR took way too long. As the title suggests, it reduces the schema context carried in the readers to the very strict minimum. In practice, that means that to build a source, we only require:
- the global `SchemaContext`
- the global `SchemaOptions` (soon to be renamed `SchemaSourceOptions`)
- that source's `SourceInfo`
Furthermore, _we no longer carry "default" customization options throughout the schema_. All customization information is extracted from the `SourceInfo`, when required. This prevents an entire category of bugs we had previously encountered, such as parts of the code using uninitialized / unupdated customization info.
In turn, this meant that we could remove the explicit threading of the `SourceInfo` throughout the schema, since it is now always available through the reader context.
Finally, this meant making a few adjustments to relay and actions as well, such as the introduction of a new separate "context" for actions, and a change to how we create some of the action-specific postgres scalar parsers.
I'll highlight with review comments the areas of interest.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6709
GitOrigin-RevId: ea80fddcb24e2513779dd04b0b700a55f0028dd1
2022-11-17 13:34:05 +03:00
|
|
|
let objectTypename = mkTypename $ applyTypeNameCaseIdentifier tCase $ mkTableTypeName $ tableGQLName
|
2022-07-25 18:53:25 +03:00
|
|
|
pure $ (objectTypename, convertToApolloFedParserFunc sourceInfo tableInfo tableSelPerm stringifyNumbers (Just tCase) primaryKeys tableSelSet)
|
|
|
|
|
|
|
|
pure (queryRootFields, subscriptionRootFields, apolloFedTableParser)
|
2022-02-28 10:49:13 +03:00
|
|
|
where
|
|
|
|
selectDesc = buildFieldDescription defaultSelectDesc $ _crfComment _tcrfSelect
|
|
|
|
selectPKDesc = buildFieldDescription defaultSelectPKDesc $ _crfComment _tcrfSelectByPk
|
|
|
|
selectAggDesc = buildFieldDescription defaultSelectAggDesc $ _crfComment _tcrfSelectAggregate
|
|
|
|
defaultSelectDesc = "fetch data from the table: " <>> tableName
|
|
|
|
defaultSelectPKDesc = "fetch data from the table: " <> tableName <<> " using primary key columns"
|
|
|
|
defaultSelectAggDesc = "fetch aggregated fields from the table: " <>> tableName
|
|
|
|
TableCustomRootFields {..} = _tcCustomRootFields . _tciCustomConfig $ _tiCoreInfo tableInfo
|
2021-02-09 15:47:21 +03:00
|
|
|
|
2022-06-07 08:32:08 +03:00
|
|
|
-- This function checks if a root field is allowed to be exposed
|
|
|
|
-- in the query root and a subscription root and when it is allowed,
|
|
|
|
-- the parser will be returned.
|
|
|
|
getQueryAndSubscriptionRootFields parser allowedInQuery allowedInSubscription =
|
|
|
|
case (allowedInQuery, allowedInSubscription) of
|
|
|
|
(True, True) -> (parser, parser)
|
|
|
|
(True, False) -> (parser, Nothing)
|
|
|
|
(False, True) -> (Nothing, parser)
|
|
|
|
(False, False) -> (Nothing, Nothing)
|
|
|
|
|
2022-04-22 22:53:12 +03:00
|
|
|
buildTableStreamingSubscriptionFields ::
|
|
|
|
forall b r m n.
|
2022-06-30 18:22:19 +03:00
|
|
|
( MonadBuildSchema b r m n,
|
2022-08-22 18:57:46 +03:00
|
|
|
AggregationPredicatesSchema b,
|
2022-06-30 18:22:19 +03:00
|
|
|
BackendTableSelectSchema b
|
|
|
|
) =>
|
2022-08-03 22:08:34 +03:00
|
|
|
MkRootFieldName ->
|
2022-04-22 22:53:12 +03:00
|
|
|
TableName b ->
|
|
|
|
TableInfo b ->
|
2022-05-26 14:54:30 +03:00
|
|
|
C.GQLNameIdentifier ->
|
2022-09-06 19:48:04 +03:00
|
|
|
SchemaT r m [FieldParser n (QueryDB b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b))]
|
server: reduce schema contexts to the bare minimum
### Description
This monster of a PR took way too long. As the title suggests, it reduces the schema context carried in the readers to the very strict minimum. In practice, that means that to build a source, we only require:
- the global `SchemaContext`
- the global `SchemaOptions` (soon to be renamed `SchemaSourceOptions`)
- that source's `SourceInfo`
Furthermore, _we no longer carry "default" customization options throughout the schema_. All customization information is extracted from the `SourceInfo`, when required. This prevents an entire category of bugs we had previously encountered, such as parts of the code using uninitialized / unupdated customization info.
In turn, this meant that we could remove the explicit threading of the `SourceInfo` throughout the schema, since it is now always available through the reader context.
Finally, this meant making a few adjustments to relay and actions as well, such as the introduction of a new separate "context" for actions, and a change to how we create some of the action-specific postgres scalar parsers.
I'll highlight with review comments the areas of interest.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6709
GitOrigin-RevId: ea80fddcb24e2513779dd04b0b700a55f0028dd1
2022-11-17 13:34:05 +03:00
|
|
|
buildTableStreamingSubscriptionFields mkRootFieldName tableName tableInfo tableIdentifier = do
|
2022-11-04 16:08:40 +03:00
|
|
|
-- Check in schema options whether we should include streaming subscription
|
|
|
|
-- fields
|
|
|
|
include <- retrieve Options.soIncludeStreamFields
|
|
|
|
case include of
|
|
|
|
Options.Don'tIncludeStreamFields -> pure mempty
|
|
|
|
Options.IncludeStreamFields -> do
|
server: reduce schema contexts to the bare minimum
### Description
This monster of a PR took way too long. As the title suggests, it reduces the schema context carried in the readers to the very strict minimum. In practice, that means that to build a source, we only require:
- the global `SchemaContext`
- the global `SchemaOptions` (soon to be renamed `SchemaSourceOptions`)
- that source's `SourceInfo`
Furthermore, _we no longer carry "default" customization options throughout the schema_. All customization information is extracted from the `SourceInfo`, when required. This prevents an entire category of bugs we had previously encountered, such as parts of the code using uninitialized / unupdated customization info.
In turn, this meant that we could remove the explicit threading of the `SourceInfo` throughout the schema, since it is now always available through the reader context.
Finally, this meant making a few adjustments to relay and actions as well, such as the introduction of a new separate "context" for actions, and a change to how we create some of the action-specific postgres scalar parsers.
I'll highlight with review comments the areas of interest.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6709
GitOrigin-RevId: ea80fddcb24e2513779dd04b0b700a55f0028dd1
2022-11-17 13:34:05 +03:00
|
|
|
sourceInfo :: SourceInfo b <- asks getter
|
|
|
|
let customization = _siCustomization sourceInfo
|
|
|
|
tCase = _rscNamingConvention customization
|
|
|
|
customRootFields = _tcCustomRootFields $ _tciCustomConfig $ _tiCoreInfo tableInfo
|
2022-11-04 16:08:40 +03:00
|
|
|
selectDesc = Just $ G.Description $ "fetch data from the table in a streaming manner: " <>> tableName
|
|
|
|
selectStreamName =
|
2023-05-24 16:51:56 +03:00
|
|
|
runMkRootFieldName mkRootFieldName
|
|
|
|
$ setFieldNameCase tCase tableInfo (_tcrfSelectStream customRootFields) mkSelectStreamField tableIdentifier
|
2022-11-04 16:08:40 +03:00
|
|
|
catMaybes
|
|
|
|
<$> sequenceA
|
server: reduce schema contexts to the bare minimum
### Description
This monster of a PR took way too long. As the title suggests, it reduces the schema context carried in the readers to the very strict minimum. In practice, that means that to build a source, we only require:
- the global `SchemaContext`
- the global `SchemaOptions` (soon to be renamed `SchemaSourceOptions`)
- that source's `SourceInfo`
Furthermore, _we no longer carry "default" customization options throughout the schema_. All customization information is extracted from the `SourceInfo`, when required. This prevents an entire category of bugs we had previously encountered, such as parts of the code using uninitialized / unupdated customization info.
In turn, this meant that we could remove the explicit threading of the `SourceInfo` throughout the schema, since it is now always available through the reader context.
Finally, this meant making a few adjustments to relay and actions as well, such as the introduction of a new separate "context" for actions, and a change to how we create some of the action-specific postgres scalar parsers.
I'll highlight with review comments the areas of interest.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6709
GitOrigin-RevId: ea80fddcb24e2513779dd04b0b700a55f0028dd1
2022-11-17 13:34:05 +03:00
|
|
|
[ optionalFieldParser QDBStreamMultipleRows $ selectStreamTable tableInfo selectStreamName selectDesc
|
2022-11-04 16:08:40 +03:00
|
|
|
]
|
2022-04-22 22:53:12 +03:00
|
|
|
|
2021-02-09 15:47:21 +03:00
|
|
|
buildTableInsertMutationFields ::
|
|
|
|
forall b r m n.
|
2022-08-03 22:08:34 +03:00
|
|
|
( MonadBuildSchema b r m n,
|
|
|
|
BackendTableSelectSchema b
|
|
|
|
) =>
|
server: reduce schema contexts to the bare minimum
### Description
This monster of a PR took way too long. As the title suggests, it reduces the schema context carried in the readers to the very strict minimum. In practice, that means that to build a source, we only require:
- the global `SchemaContext`
- the global `SchemaOptions` (soon to be renamed `SchemaSourceOptions`)
- that source's `SourceInfo`
Furthermore, _we no longer carry "default" customization options throughout the schema_. All customization information is extracted from the `SourceInfo`, when required. This prevents an entire category of bugs we had previously encountered, such as parts of the code using uninitialized / unupdated customization info.
In turn, this meant that we could remove the explicit threading of the `SourceInfo` throughout the schema, since it is now always available through the reader context.
Finally, this meant making a few adjustments to relay and actions as well, such as the introduction of a new separate "context" for actions, and a change to how we create some of the action-specific postgres scalar parsers.
I'll highlight with review comments the areas of interest.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6709
GitOrigin-RevId: ea80fddcb24e2513779dd04b0b700a55f0028dd1
2022-11-17 13:34:05 +03:00
|
|
|
(TableInfo b -> SchemaT r m (InputFieldsParser n (BackendInsert b (UnpreparedValue b)))) ->
|
2022-08-03 22:08:34 +03:00
|
|
|
MkRootFieldName ->
|
Role-invariant schema constructors
We build the GraphQL schema by combining building blocks such as `tableSelectionSet` and `columnParser`. These building blocks individually build `{InputFields,Field,}Parser` objects. Those object specify the valid GraphQL schema.
Since the GraphQL schema is role-dependent, at some point we need to know what fragment of the GraphQL schema a specific role is allowed to access, and this is stored in `{Sel,Upd,Ins,Del}PermInfo` objects.
We have passed around these permission objects as function arguments to the schema building blocks since we first started dealing with permissions during the PDV refactor - see hasura/graphql-engine@5168b99e463199b1934d8645bd6cd37eddb64ae1 in hasura/graphql-engine#4111. This means that, for instance, `tableSelectionSet` has as its type:
```haskell
tableSelectionSet ::
forall b r m n.
MonadBuildSchema b r m n =>
SourceName ->
TableInfo b ->
SelPermInfo b ->
m (Parser 'Output n (AnnotatedFields b))
```
There are three reasons to change this.
1. We often pass a `Maybe (xPermInfo b)` instead of a proper `xPermInfo b`, and it's not clear what the intended semantics of this is. Some potential improvements on the data types involved are discussed in issue hasura/graphql-engine-mono#3125.
2. In most cases we also already pass a `TableInfo b`, and together with the `MonadRole` that is usually also in scope, this means that we could look up the required permissions regardless: so passing the permissions explicitly undermines the "single source of truth" principle. Breaking this principle also makes the code more difficult to read.
3. We are working towards role-based parsers (see hasura/graphql-engine-mono#2711), where the `{InputFields,Field,}Parser` objects are constructed in a role-invariant way, so that we have a single object that can be used for all roles. In particular, this means that the schema building blocks _need_ to be constructed in a role-invariant way. While this PR doesn't accomplish that, it does reduce the amount of role-specific arguments being passed, thus fixing hasura/graphql-engine-mono#3068.
Concretely, this PR simply drops the `xPermInfo b` argument from almost all schema building blocks. Instead these objects are looked up from the `TableInfo b` as-needed. The resulting code is considerably simpler and shorter.
One way to interpret this change is as follows. Before this PR, we figured out permissions at the top-level in `Hasura.GraphQL.Schema`, passing down the obtained `xPermInfo` objects as required. After this PR, we have a bottom-up approach where the schema building blocks themselves decide whether they want to be included for a particular role.
So this moves some permission logic out of `Hasura.GraphQL.Schema`, which is very complex.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/3608
GitOrigin-RevId: 51a744f34ec7d57bc8077667ae7f9cb9c4f6c962
2022-02-17 11:16:20 +03:00
|
|
|
Scenario ->
|
2021-02-09 15:47:21 +03:00
|
|
|
TableName b ->
|
|
|
|
TableInfo b ->
|
2022-05-26 14:54:30 +03:00
|
|
|
C.GQLNameIdentifier ->
|
2022-09-06 19:48:04 +03:00
|
|
|
SchemaT r m [FieldParser n (AnnotatedInsert b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b))]
|
server: reduce schema contexts to the bare minimum
### Description
This monster of a PR took way too long. As the title suggests, it reduces the schema context carried in the readers to the very strict minimum. In practice, that means that to build a source, we only require:
- the global `SchemaContext`
- the global `SchemaOptions` (soon to be renamed `SchemaSourceOptions`)
- that source's `SourceInfo`
Furthermore, _we no longer carry "default" customization options throughout the schema_. All customization information is extracted from the `SourceInfo`, when required. This prevents an entire category of bugs we had previously encountered, such as parts of the code using uninitialized / unupdated customization info.
In turn, this meant that we could remove the explicit threading of the `SourceInfo` throughout the schema, since it is now always available through the reader context.
Finally, this meant making a few adjustments to relay and actions as well, such as the introduction of a new separate "context" for actions, and a change to how we create some of the action-specific postgres scalar parsers.
I'll highlight with review comments the areas of interest.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6709
GitOrigin-RevId: ea80fddcb24e2513779dd04b0b700a55f0028dd1
2022-11-17 13:34:05 +03:00
|
|
|
buildTableInsertMutationFields backendInsertAction mkRootFieldName scenario tableName tableInfo gqlName = do
|
|
|
|
sourceInfo :: SourceInfo b <- asks getter
|
|
|
|
let customization = _siCustomization sourceInfo
|
|
|
|
tCase = _rscNamingConvention customization
|
|
|
|
-- insert in table
|
2022-08-03 22:08:34 +03:00
|
|
|
insertName = runMkRootFieldName mkRootFieldName $ setFieldNameCase tCase tableInfo _tcrfInsert mkInsertField gqlName
|
|
|
|
-- insert one in table
|
|
|
|
insertOneName = runMkRootFieldName mkRootFieldName $ setFieldNameCase tCase tableInfo _tcrfInsertOne mkInsertOneField gqlName
|
|
|
|
|
server: reduce schema contexts to the bare minimum
### Description
This monster of a PR took way too long. As the title suggests, it reduces the schema context carried in the readers to the very strict minimum. In practice, that means that to build a source, we only require:
- the global `SchemaContext`
- the global `SchemaOptions` (soon to be renamed `SchemaSourceOptions`)
- that source's `SourceInfo`
Furthermore, _we no longer carry "default" customization options throughout the schema_. All customization information is extracted from the `SourceInfo`, when required. This prevents an entire category of bugs we had previously encountered, such as parts of the code using uninitialized / unupdated customization info.
In turn, this meant that we could remove the explicit threading of the `SourceInfo` throughout the schema, since it is now always available through the reader context.
Finally, this meant making a few adjustments to relay and actions as well, such as the introduction of a new separate "context" for actions, and a change to how we create some of the action-specific postgres scalar parsers.
I'll highlight with review comments the areas of interest.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6709
GitOrigin-RevId: ea80fddcb24e2513779dd04b0b700a55f0028dd1
2022-11-17 13:34:05 +03:00
|
|
|
insert <- insertIntoTable backendInsertAction scenario tableInfo insertName insertDesc
|
Role-invariant schema constructors
We build the GraphQL schema by combining building blocks such as `tableSelectionSet` and `columnParser`. These building blocks individually build `{InputFields,Field,}Parser` objects. Those object specify the valid GraphQL schema.
Since the GraphQL schema is role-dependent, at some point we need to know what fragment of the GraphQL schema a specific role is allowed to access, and this is stored in `{Sel,Upd,Ins,Del}PermInfo` objects.
We have passed around these permission objects as function arguments to the schema building blocks since we first started dealing with permissions during the PDV refactor - see hasura/graphql-engine@5168b99e463199b1934d8645bd6cd37eddb64ae1 in hasura/graphql-engine#4111. This means that, for instance, `tableSelectionSet` has as its type:
```haskell
tableSelectionSet ::
forall b r m n.
MonadBuildSchema b r m n =>
SourceName ->
TableInfo b ->
SelPermInfo b ->
m (Parser 'Output n (AnnotatedFields b))
```
There are three reasons to change this.
1. We often pass a `Maybe (xPermInfo b)` instead of a proper `xPermInfo b`, and it's not clear what the intended semantics of this is. Some potential improvements on the data types involved are discussed in issue hasura/graphql-engine-mono#3125.
2. In most cases we also already pass a `TableInfo b`, and together with the `MonadRole` that is usually also in scope, this means that we could look up the required permissions regardless: so passing the permissions explicitly undermines the "single source of truth" principle. Breaking this principle also makes the code more difficult to read.
3. We are working towards role-based parsers (see hasura/graphql-engine-mono#2711), where the `{InputFields,Field,}Parser` objects are constructed in a role-invariant way, so that we have a single object that can be used for all roles. In particular, this means that the schema building blocks _need_ to be constructed in a role-invariant way. While this PR doesn't accomplish that, it does reduce the amount of role-specific arguments being passed, thus fixing hasura/graphql-engine-mono#3068.
Concretely, this PR simply drops the `xPermInfo b` argument from almost all schema building blocks. Instead these objects are looked up from the `TableInfo b` as-needed. The resulting code is considerably simpler and shorter.
One way to interpret this change is as follows. Before this PR, we figured out permissions at the top-level in `Hasura.GraphQL.Schema`, passing down the obtained `xPermInfo` objects as required. After this PR, we have a bottom-up approach where the schema building blocks themselves decide whether they want to be included for a particular role.
So this moves some permission logic out of `Hasura.GraphQL.Schema`, which is very complex.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/3608
GitOrigin-RevId: 51a744f34ec7d57bc8077667ae7f9cb9c4f6c962
2022-02-17 11:16:20 +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.
|
server: reduce schema contexts to the bare minimum
### Description
This monster of a PR took way too long. As the title suggests, it reduces the schema context carried in the readers to the very strict minimum. In practice, that means that to build a source, we only require:
- the global `SchemaContext`
- the global `SchemaOptions` (soon to be renamed `SchemaSourceOptions`)
- that source's `SourceInfo`
Furthermore, _we no longer carry "default" customization options throughout the schema_. All customization information is extracted from the `SourceInfo`, when required. This prevents an entire category of bugs we had previously encountered, such as parts of the code using uninitialized / unupdated customization info.
In turn, this meant that we could remove the explicit threading of the `SourceInfo` throughout the schema, since it is now always available through the reader context.
Finally, this meant making a few adjustments to relay and actions as well, such as the introduction of a new separate "context" for actions, and a change to how we create some of the action-specific postgres scalar parsers.
I'll highlight with review comments the areas of interest.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6709
GitOrigin-RevId: ea80fddcb24e2513779dd04b0b700a55f0028dd1
2022-11-17 13:34:05 +03:00
|
|
|
insertOne <- insertOneIntoTable backendInsertAction scenario tableInfo insertOneName insertOneDesc
|
Role-invariant schema constructors
We build the GraphQL schema by combining building blocks such as `tableSelectionSet` and `columnParser`. These building blocks individually build `{InputFields,Field,}Parser` objects. Those object specify the valid GraphQL schema.
Since the GraphQL schema is role-dependent, at some point we need to know what fragment of the GraphQL schema a specific role is allowed to access, and this is stored in `{Sel,Upd,Ins,Del}PermInfo` objects.
We have passed around these permission objects as function arguments to the schema building blocks since we first started dealing with permissions during the PDV refactor - see hasura/graphql-engine@5168b99e463199b1934d8645bd6cd37eddb64ae1 in hasura/graphql-engine#4111. This means that, for instance, `tableSelectionSet` has as its type:
```haskell
tableSelectionSet ::
forall b r m n.
MonadBuildSchema b r m n =>
SourceName ->
TableInfo b ->
SelPermInfo b ->
m (Parser 'Output n (AnnotatedFields b))
```
There are three reasons to change this.
1. We often pass a `Maybe (xPermInfo b)` instead of a proper `xPermInfo b`, and it's not clear what the intended semantics of this is. Some potential improvements on the data types involved are discussed in issue hasura/graphql-engine-mono#3125.
2. In most cases we also already pass a `TableInfo b`, and together with the `MonadRole` that is usually also in scope, this means that we could look up the required permissions regardless: so passing the permissions explicitly undermines the "single source of truth" principle. Breaking this principle also makes the code more difficult to read.
3. We are working towards role-based parsers (see hasura/graphql-engine-mono#2711), where the `{InputFields,Field,}Parser` objects are constructed in a role-invariant way, so that we have a single object that can be used for all roles. In particular, this means that the schema building blocks _need_ to be constructed in a role-invariant way. While this PR doesn't accomplish that, it does reduce the amount of role-specific arguments being passed, thus fixing hasura/graphql-engine-mono#3068.
Concretely, this PR simply drops the `xPermInfo b` argument from almost all schema building blocks. Instead these objects are looked up from the `TableInfo b` as-needed. The resulting code is considerably simpler and shorter.
One way to interpret this change is as follows. Before this PR, we figured out permissions at the top-level in `Hasura.GraphQL.Schema`, passing down the obtained `xPermInfo` objects as required. After this PR, we have a bottom-up approach where the schema building blocks themselves decide whether they want to be included for a particular role.
So this moves some permission logic out of `Hasura.GraphQL.Schema`, which is very complex.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/3608
GitOrigin-RevId: 51a744f34ec7d57bc8077667ae7f9cb9c4f6c962
2022-02-17 11:16:20 +03:00
|
|
|
pure $ catMaybes [insert, insertOne]
|
2022-02-28 10:49:13 +03:00
|
|
|
where
|
|
|
|
insertDesc = buildFieldDescription defaultInsertDesc $ _crfComment _tcrfInsert
|
|
|
|
insertOneDesc = buildFieldDescription defaultInsertOneDesc $ _crfComment _tcrfInsertOne
|
|
|
|
defaultInsertDesc = "insert data into the table: " <>> tableName
|
|
|
|
defaultInsertOneDesc = "insert a single row into the table: " <>> tableName
|
|
|
|
TableCustomRootFields {..} = _tcCustomRootFields . _tciCustomConfig $ _tiCoreInfo tableInfo
|
2021-02-09 15:47:21 +03:00
|
|
|
|
2023-01-10 04:54:40 +03:00
|
|
|
-- | This function implements the parsers for the basic, single batch, update mutations. It
|
2021-11-10 03:37:42 +03:00
|
|
|
-- implements the mutation schema in the general shape described in
|
|
|
|
-- @https://hasura.io/docs/latest/graphql/core/databases/postgres/mutations/update.html@.
|
2023-01-10 04:54:40 +03:00
|
|
|
-- (ie. update_<table> and update_<table>_by_pk root fields)
|
2021-11-10 03:37:42 +03:00
|
|
|
--
|
2023-01-10 04:54:40 +03:00
|
|
|
-- Different backends can have different update types (single batch, multiple batches, etc),
|
|
|
|
-- and so the parsed UpdateBatch needs to be embedded in the custom UpdateVariant defined
|
|
|
|
-- by the backend, which is done by passing a function to this function.
|
|
|
|
buildSingleBatchTableUpdateMutationFields ::
|
2021-02-09 15:47:21 +03:00
|
|
|
forall b r m n.
|
2022-06-30 18:22:19 +03:00
|
|
|
( MonadBuildSchema b r m n,
|
2022-08-22 18:57:46 +03:00
|
|
|
AggregationPredicatesSchema b,
|
2023-01-10 04:54:40 +03:00
|
|
|
BackendTableSelectSchema b,
|
|
|
|
BackendUpdateOperatorsSchema b
|
2022-06-30 18:22:19 +03:00
|
|
|
) =>
|
2023-01-10 04:54:40 +03:00
|
|
|
-- | Embed the UpdateBack in the backend-specific UpdateVariant
|
|
|
|
(UpdateBatch b (UpdateOperators b) (UnpreparedValue b) -> UpdateVariant b (UnpreparedValue b)) ->
|
2022-05-31 17:41:09 +03:00
|
|
|
Scenario ->
|
2021-11-10 03:37:42 +03:00
|
|
|
-- | table info
|
2021-02-09 15:47:21 +03:00
|
|
|
TableInfo b ->
|
2021-11-10 03:37:42 +03:00
|
|
|
-- | field display name
|
2022-05-26 14:54:30 +03:00
|
|
|
C.GQLNameIdentifier ->
|
2022-09-06 19:48:04 +03:00
|
|
|
SchemaT r m [FieldParser n (AnnotatedUpdateG b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b))]
|
2023-01-10 04:54:40 +03:00
|
|
|
buildSingleBatchTableUpdateMutationFields mkSingleBatchUpdateVariant scenario tableInfo gqlName = do
|
|
|
|
update <- updateTable mkSingleBatchUpdateVariant scenario tableInfo gqlName
|
|
|
|
updateByPk <- updateTableByPk mkSingleBatchUpdateVariant scenario tableInfo gqlName
|
Role-invariant schema constructors
We build the GraphQL schema by combining building blocks such as `tableSelectionSet` and `columnParser`. These building blocks individually build `{InputFields,Field,}Parser` objects. Those object specify the valid GraphQL schema.
Since the GraphQL schema is role-dependent, at some point we need to know what fragment of the GraphQL schema a specific role is allowed to access, and this is stored in `{Sel,Upd,Ins,Del}PermInfo` objects.
We have passed around these permission objects as function arguments to the schema building blocks since we first started dealing with permissions during the PDV refactor - see hasura/graphql-engine@5168b99e463199b1934d8645bd6cd37eddb64ae1 in hasura/graphql-engine#4111. This means that, for instance, `tableSelectionSet` has as its type:
```haskell
tableSelectionSet ::
forall b r m n.
MonadBuildSchema b r m n =>
SourceName ->
TableInfo b ->
SelPermInfo b ->
m (Parser 'Output n (AnnotatedFields b))
```
There are three reasons to change this.
1. We often pass a `Maybe (xPermInfo b)` instead of a proper `xPermInfo b`, and it's not clear what the intended semantics of this is. Some potential improvements on the data types involved are discussed in issue hasura/graphql-engine-mono#3125.
2. In most cases we also already pass a `TableInfo b`, and together with the `MonadRole` that is usually also in scope, this means that we could look up the required permissions regardless: so passing the permissions explicitly undermines the "single source of truth" principle. Breaking this principle also makes the code more difficult to read.
3. We are working towards role-based parsers (see hasura/graphql-engine-mono#2711), where the `{InputFields,Field,}Parser` objects are constructed in a role-invariant way, so that we have a single object that can be used for all roles. In particular, this means that the schema building blocks _need_ to be constructed in a role-invariant way. While this PR doesn't accomplish that, it does reduce the amount of role-specific arguments being passed, thus fixing hasura/graphql-engine-mono#3068.
Concretely, this PR simply drops the `xPermInfo b` argument from almost all schema building blocks. Instead these objects are looked up from the `TableInfo b` as-needed. The resulting code is considerably simpler and shorter.
One way to interpret this change is as follows. Before this PR, we figured out permissions at the top-level in `Hasura.GraphQL.Schema`, passing down the obtained `xPermInfo` objects as required. After this PR, we have a bottom-up approach where the schema building blocks themselves decide whether they want to be included for a particular role.
So this moves some permission logic out of `Hasura.GraphQL.Schema`, which is very complex.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/3608
GitOrigin-RevId: 51a744f34ec7d57bc8077667ae7f9cb9c4f6c962
2022-02-17 11:16:20 +03:00
|
|
|
pure $ catMaybes [update, updateByPk]
|
2021-02-09 15:47:21 +03:00
|
|
|
|
|
|
|
buildTableDeleteMutationFields ::
|
|
|
|
forall b r m n.
|
2022-06-30 18:22:19 +03:00
|
|
|
( MonadBuildSchema b r m n,
|
2022-08-22 18:57:46 +03:00
|
|
|
AggregationPredicatesSchema b,
|
2022-06-30 18:22:19 +03:00
|
|
|
BackendTableSelectSchema b
|
|
|
|
) =>
|
2022-08-03 22:08:34 +03:00
|
|
|
MkRootFieldName ->
|
2022-05-31 17:41:09 +03:00
|
|
|
Scenario ->
|
2021-02-09 15:47:21 +03:00
|
|
|
TableName b ->
|
|
|
|
TableInfo b ->
|
2022-05-26 14:54:30 +03:00
|
|
|
C.GQLNameIdentifier ->
|
2022-09-06 19:48:04 +03:00
|
|
|
SchemaT r m [FieldParser n (AnnDelG b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b))]
|
server: reduce schema contexts to the bare minimum
### Description
This monster of a PR took way too long. As the title suggests, it reduces the schema context carried in the readers to the very strict minimum. In practice, that means that to build a source, we only require:
- the global `SchemaContext`
- the global `SchemaOptions` (soon to be renamed `SchemaSourceOptions`)
- that source's `SourceInfo`
Furthermore, _we no longer carry "default" customization options throughout the schema_. All customization information is extracted from the `SourceInfo`, when required. This prevents an entire category of bugs we had previously encountered, such as parts of the code using uninitialized / unupdated customization info.
In turn, this meant that we could remove the explicit threading of the `SourceInfo` throughout the schema, since it is now always available through the reader context.
Finally, this meant making a few adjustments to relay and actions as well, such as the introduction of a new separate "context" for actions, and a change to how we create some of the action-specific postgres scalar parsers.
I'll highlight with review comments the areas of interest.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6709
GitOrigin-RevId: ea80fddcb24e2513779dd04b0b700a55f0028dd1
2022-11-17 13:34:05 +03:00
|
|
|
buildTableDeleteMutationFields mkRootFieldName scenario tableName tableInfo gqlName = do
|
|
|
|
sourceInfo :: SourceInfo b <- asks getter
|
|
|
|
let customization = _siCustomization sourceInfo
|
|
|
|
tCase = _rscNamingConvention customization
|
|
|
|
-- delete from table
|
2022-08-03 22:08:34 +03:00
|
|
|
deleteName = runMkRootFieldName mkRootFieldName $ setFieldNameCase tCase tableInfo _tcrfDelete mkDeleteField gqlName
|
|
|
|
-- delete from table by pk
|
|
|
|
deletePKName = runMkRootFieldName mkRootFieldName $ setFieldNameCase tCase tableInfo _tcrfDeleteByPk mkDeleteByPkField gqlName
|
|
|
|
|
server: reduce schema contexts to the bare minimum
### Description
This monster of a PR took way too long. As the title suggests, it reduces the schema context carried in the readers to the very strict minimum. In practice, that means that to build a source, we only require:
- the global `SchemaContext`
- the global `SchemaOptions` (soon to be renamed `SchemaSourceOptions`)
- that source's `SourceInfo`
Furthermore, _we no longer carry "default" customization options throughout the schema_. All customization information is extracted from the `SourceInfo`, when required. This prevents an entire category of bugs we had previously encountered, such as parts of the code using uninitialized / unupdated customization info.
In turn, this meant that we could remove the explicit threading of the `SourceInfo` throughout the schema, since it is now always available through the reader context.
Finally, this meant making a few adjustments to relay and actions as well, such as the introduction of a new separate "context" for actions, and a change to how we create some of the action-specific postgres scalar parsers.
I'll highlight with review comments the areas of interest.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6709
GitOrigin-RevId: ea80fddcb24e2513779dd04b0b700a55f0028dd1
2022-11-17 13:34:05 +03:00
|
|
|
delete <- deleteFromTable scenario tableInfo deleteName deleteDesc
|
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.
|
server: reduce schema contexts to the bare minimum
### Description
This monster of a PR took way too long. As the title suggests, it reduces the schema context carried in the readers to the very strict minimum. In practice, that means that to build a source, we only require:
- the global `SchemaContext`
- the global `SchemaOptions` (soon to be renamed `SchemaSourceOptions`)
- that source's `SourceInfo`
Furthermore, _we no longer carry "default" customization options throughout the schema_. All customization information is extracted from the `SourceInfo`, when required. This prevents an entire category of bugs we had previously encountered, such as parts of the code using uninitialized / unupdated customization info.
In turn, this meant that we could remove the explicit threading of the `SourceInfo` throughout the schema, since it is now always available through the reader context.
Finally, this meant making a few adjustments to relay and actions as well, such as the introduction of a new separate "context" for actions, and a change to how we create some of the action-specific postgres scalar parsers.
I'll highlight with review comments the areas of interest.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6709
GitOrigin-RevId: ea80fddcb24e2513779dd04b0b700a55f0028dd1
2022-11-17 13:34:05 +03:00
|
|
|
deleteByPk <- deleteFromTableByPk scenario tableInfo deletePKName deletePKDesc
|
Role-invariant schema constructors
We build the GraphQL schema by combining building blocks such as `tableSelectionSet` and `columnParser`. These building blocks individually build `{InputFields,Field,}Parser` objects. Those object specify the valid GraphQL schema.
Since the GraphQL schema is role-dependent, at some point we need to know what fragment of the GraphQL schema a specific role is allowed to access, and this is stored in `{Sel,Upd,Ins,Del}PermInfo` objects.
We have passed around these permission objects as function arguments to the schema building blocks since we first started dealing with permissions during the PDV refactor - see hasura/graphql-engine@5168b99e463199b1934d8645bd6cd37eddb64ae1 in hasura/graphql-engine#4111. This means that, for instance, `tableSelectionSet` has as its type:
```haskell
tableSelectionSet ::
forall b r m n.
MonadBuildSchema b r m n =>
SourceName ->
TableInfo b ->
SelPermInfo b ->
m (Parser 'Output n (AnnotatedFields b))
```
There are three reasons to change this.
1. We often pass a `Maybe (xPermInfo b)` instead of a proper `xPermInfo b`, and it's not clear what the intended semantics of this is. Some potential improvements on the data types involved are discussed in issue hasura/graphql-engine-mono#3125.
2. In most cases we also already pass a `TableInfo b`, and together with the `MonadRole` that is usually also in scope, this means that we could look up the required permissions regardless: so passing the permissions explicitly undermines the "single source of truth" principle. Breaking this principle also makes the code more difficult to read.
3. We are working towards role-based parsers (see hasura/graphql-engine-mono#2711), where the `{InputFields,Field,}Parser` objects are constructed in a role-invariant way, so that we have a single object that can be used for all roles. In particular, this means that the schema building blocks _need_ to be constructed in a role-invariant way. While this PR doesn't accomplish that, it does reduce the amount of role-specific arguments being passed, thus fixing hasura/graphql-engine-mono#3068.
Concretely, this PR simply drops the `xPermInfo b` argument from almost all schema building blocks. Instead these objects are looked up from the `TableInfo b` as-needed. The resulting code is considerably simpler and shorter.
One way to interpret this change is as follows. Before this PR, we figured out permissions at the top-level in `Hasura.GraphQL.Schema`, passing down the obtained `xPermInfo` objects as required. After this PR, we have a bottom-up approach where the schema building blocks themselves decide whether they want to be included for a particular role.
So this moves some permission logic out of `Hasura.GraphQL.Schema`, which is very complex.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/3608
GitOrigin-RevId: 51a744f34ec7d57bc8077667ae7f9cb9c4f6c962
2022-02-17 11:16:20 +03:00
|
|
|
pure $ catMaybes [delete, deleteByPk]
|
2022-02-28 10:49:13 +03:00
|
|
|
where
|
|
|
|
deleteDesc = buildFieldDescription defaultDeleteDesc $ _crfComment _tcrfDelete
|
|
|
|
deletePKDesc = buildFieldDescription defaultDeletePKDesc $ _crfComment _tcrfDeleteByPk
|
|
|
|
defaultDeleteDesc = "delete data from the table: " <>> tableName
|
|
|
|
defaultDeletePKDesc = "delete single row from the table: " <>> tableName
|
|
|
|
TableCustomRootFields {..} = _tcCustomRootFields . _tciCustomConfig $ _tiCoreInfo tableInfo
|
2021-02-09 15:47:21 +03:00
|
|
|
|
2022-02-28 10:49:13 +03:00
|
|
|
buildFieldDescription :: Text -> Comment -> Maybe G.Description
|
|
|
|
buildFieldDescription defaultDescription = \case
|
|
|
|
Automatic -> Just $ G.Description defaultDescription
|
|
|
|
Explicit comment -> G.Description . toTxt <$> comment
|