mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-18 13:02:11 +03:00
512a4dbb92
### Description This PR changes all the schema code to operate in a specific `SchemaT` monad, rather than in an arbitrary `m` monad. `SchemaT` is intended to be used opaquely with `runSourceSchema` and `runRemoteSchema`. The main goal of this is to allow a different reader context per part of the schema: this PR also minimizes the contexts. This means that we no longer require `SchemaOptions` when building remote schemas' schema, and this PR therefore removes a lot of dummy / placeholder values accordingly. ### Performance and stacking This PR has been through several iterations. #5339 was the original version, that accomplished the same thing by stacking readers on top of the stack at every remote relationship boundary. This raised performance concerns, and @0x777 confirmed with an ad-hoc test that in some extreme cases we could see up to a 10% performance impact. This version, while more verbose, allows us to unstack / re-stack the readers, and avoid that problem. #5517 adds a new benchmark set to be able to automatically measure this on every PR. ### Remaining work - [x] a comment (or perhaps even a Note?) should be added to `SchemaT` - [x] we probably want for #5517 to be merged first so that we can confirm the lack of performance penalty PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5458 GitOrigin-RevId: e06b83d90da475f745b838f1fd8f8b4d9d3f4b10
148 lines
5.5 KiB
Haskell
148 lines
5.5 KiB
Haskell
{-# LANGUAGE ApplicativeDo #-}
|
|
|
|
-- | MSSQL Schema IfMatched
|
|
--
|
|
-- This module contains the building blocks for parsing @if_matched@ clauses
|
|
-- (represented as 'IfMatched'), which in the MSSQL backend are used to
|
|
-- implement upsert functionality.
|
|
--
|
|
-- These are used by 'Hasura.Backends.MSSQL.Instances.Schema.backendInsertParser' to
|
|
-- construct a mssql-specific schema parser for insert (and upsert) mutations.
|
|
module Hasura.Backends.MSSQL.Schema.IfMatched
|
|
( ifMatchedFieldParser,
|
|
)
|
|
where
|
|
|
|
import Data.Text.Extended
|
|
import Hasura.Backends.MSSQL.Types.Insert
|
|
import Hasura.Backends.MSSQL.Types.Internal (ScalarType (..))
|
|
import Hasura.GraphQL.Parser.Class
|
|
import Hasura.GraphQL.Schema.Backend
|
|
import Hasura.GraphQL.Schema.BoolExp
|
|
import Hasura.GraphQL.Schema.Common
|
|
import Hasura.GraphQL.Schema.Parser
|
|
( InputFieldsParser,
|
|
Kind (..),
|
|
Parser,
|
|
)
|
|
import Hasura.GraphQL.Schema.Parser qualified as P
|
|
import Hasura.GraphQL.Schema.Table
|
|
import Hasura.GraphQL.Schema.Typename (mkTypename)
|
|
import Hasura.Name qualified as Name
|
|
import Hasura.Prelude
|
|
import Hasura.RQL.IR.BoolExp
|
|
import Hasura.RQL.IR.Value
|
|
import Hasura.RQL.Types.Backend
|
|
import Hasura.RQL.Types.Column
|
|
import Hasura.RQL.Types.SchemaCache
|
|
import Hasura.RQL.Types.Source
|
|
import Hasura.RQL.Types.Table
|
|
import Hasura.SQL.Backend
|
|
import Language.GraphQL.Draft.Syntax qualified as G
|
|
|
|
-- | Field-parser for:
|
|
--
|
|
-- > if_matched: tablename_if_matched
|
|
-- >
|
|
-- > input tablename_if_matched {
|
|
-- > match_columns: [tablename_select_column!]
|
|
-- > update_columns: [tablename_update_columns!]
|
|
-- > where: tablename_bool_exp
|
|
-- > }
|
|
--
|
|
-- Note that the types ordinarily produced by this parser are only created if
|
|
-- the active role has /both/ select and update permissions to the table
|
|
-- @tablename@ defined /and/ these grant non-empty column permissions.
|
|
ifMatchedFieldParser ::
|
|
forall r m n.
|
|
( MonadBuildSchema 'MSSQL r m n,
|
|
AggregationPredicatesSchema 'MSSQL
|
|
) =>
|
|
SourceInfo 'MSSQL ->
|
|
TableInfo 'MSSQL ->
|
|
SchemaT r m (InputFieldsParser n (Maybe (IfMatched (UnpreparedValue 'MSSQL))))
|
|
ifMatchedFieldParser sourceInfo tableInfo = do
|
|
maybeObject <- ifMatchedObjectParser sourceInfo tableInfo
|
|
return $ withJust maybeObject $ P.fieldOptional Name._if_matched (Just "upsert condition")
|
|
|
|
-- | Parse a @tablename_if_matched@ object.
|
|
ifMatchedObjectParser ::
|
|
forall r m n.
|
|
( MonadBuildSchema 'MSSQL r m n,
|
|
AggregationPredicatesSchema 'MSSQL
|
|
) =>
|
|
SourceInfo 'MSSQL ->
|
|
TableInfo 'MSSQL ->
|
|
SchemaT r m (Maybe (Parser 'Input n (IfMatched (UnpreparedValue 'MSSQL))))
|
|
ifMatchedObjectParser sourceInfo tableInfo = runMaybeT do
|
|
-- Short-circuit if we don't have sufficient permissions.
|
|
roleName <- retrieve scRole
|
|
updatePerms <- hoistMaybe $ _permUpd $ getRolePermInfo roleName tableInfo
|
|
matchColumnsEnum <- MaybeT $ tableInsertMatchColumnsEnum sourceInfo tableInfo
|
|
lift do
|
|
updateColumnsEnum <- updateColumnsPlaceholderParser tableInfo
|
|
tableGQLName <- getTableGQLName tableInfo
|
|
objectName <- mkTypename $ tableGQLName <> Name.__if_matched
|
|
let _imColumnPresets = partialSQLExpToUnpreparedValue <$> upiSet updatePerms
|
|
updateFilter = fmap partialSQLExpToUnpreparedValue <$> upiFilter updatePerms
|
|
objectDesc = G.Description $ "upsert condition type for table " <>> tableInfoName tableInfo
|
|
matchColumnsName = Name._match_columns
|
|
updateColumnsName = Name._update_columns
|
|
whereName = Name._where
|
|
whereExpParser <- boolExp sourceInfo tableInfo
|
|
pure $
|
|
P.object objectName (Just objectDesc) do
|
|
_imConditions <-
|
|
(\whereExp -> BoolAnd $ updateFilter : maybeToList whereExp)
|
|
<$> P.fieldOptional whereName Nothing whereExpParser
|
|
_imMatchColumns <-
|
|
P.fieldWithDefault matchColumnsName Nothing (G.VList []) (P.list matchColumnsEnum)
|
|
_imUpdateColumns <-
|
|
P.fieldWithDefault updateColumnsName Nothing (G.VList []) (P.list updateColumnsEnum) `P.bindFields` \cs ->
|
|
-- this can only happen if the placeholder was used
|
|
sequenceA cs `onNothing` parseError "erroneous column name"
|
|
|
|
pure $ IfMatched {..}
|
|
|
|
-- | Table insert_match_columns enum
|
|
--
|
|
-- Parser for an enum type that matches the columns that can be used
|
|
-- for insert match_columns for a given table.
|
|
-- Maps to the insert_match_columns object.
|
|
--
|
|
-- Return Nothing if there's no column the current user has "select"
|
|
-- permissions for.
|
|
tableInsertMatchColumnsEnum ::
|
|
forall r m n.
|
|
MonadBuildSourceSchema r m n =>
|
|
SourceInfo 'MSSQL ->
|
|
TableInfo 'MSSQL ->
|
|
SchemaT r m (Maybe (Parser 'Both n (Column 'MSSQL)))
|
|
tableInsertMatchColumnsEnum sourceInfo tableInfo = do
|
|
tableGQLName <- getTableGQLName @'MSSQL tableInfo
|
|
columns <- tableSelectColumns sourceInfo tableInfo
|
|
enumName <- mkTypename $ tableGQLName <> Name.__insert_match_column
|
|
let description =
|
|
Just $
|
|
G.Description $
|
|
"select match_columns of table " <>> tableInfoName tableInfo
|
|
pure $
|
|
P.enum enumName description
|
|
<$> nonEmpty
|
|
[ ( define $ ciName column,
|
|
ciColumn column
|
|
)
|
|
| column <- columns,
|
|
isMatchColumnValid column
|
|
]
|
|
where
|
|
define name =
|
|
P.Definition name (Just $ G.Description "column name") Nothing [] P.EnumValueInfo
|
|
|
|
-- | Check whether a column can be used for match_columns.
|
|
isMatchColumnValid :: ColumnInfo 'MSSQL -> Bool
|
|
isMatchColumnValid = \case
|
|
-- Unfortunately MSSQL does not support comparison for TEXT types.
|
|
ColumnInfo {ciType = ColumnScalar TextType} -> False
|
|
_ -> True
|