graphql-engine/server/src-lib/Hasura/Backends/Postgres/Translate/Update.hs
Auke Booij 3c3ed55914 server: schema that grows (#105)
This PR makes a bunch of schema generation code in Hasura.GraphQL.Schema backend-agnostic, by moving the backend-specific parts into a new BackendSchema type class. This way, the schema generation code can be reused for other backends, simply by implementing new instances of the BackendSchema type class.

This work is now in a state where the schema generators are sufficiently generic to accept the implementation of a new backend. That means that we can start exposing MS SQL schema. Execution is not implemented yet, of course.
The branch currently does not support computed fields or Relay. This is, in a sense, intentional: computed field support is normally baked into the schema generation (through the fieldSelection schema generator), and so this branch shows a programming technique that allows us to expose certain GraphQL schema depending on backend support. We can write support for computed fields and Relay at a later stage.

Co-authored-by: Antoine Leblanc <antoine@hasura.io>
GitOrigin-RevId: df369fc3d189cbda1b931d31678e9450a6601314
2020-12-01 15:51:13 +00:00

54 lines
2.2 KiB
Haskell

module Hasura.Backends.Postgres.Translate.Update
( mkUpdateCTE
) where
import Hasura.Prelude
import qualified Hasura.Backends.Postgres.SQL.DML as S
import Hasura.Backends.Postgres.SQL.Types
import Hasura.Backends.Postgres.Translate.BoolExp
import Hasura.Backends.Postgres.Translate.Insert
import Hasura.Backends.Postgres.Translate.Returning
import Hasura.RQL.IR.Update
import Hasura.RQL.Types
import Hasura.SQL.Types
mkUpdateCTE
:: AnnUpd 'Postgres -> S.CTE
mkUpdateCTE (AnnUpd tn opExps (permFltr, wc) chk _ columnsInfo) =
S.CTEUpdate update
where
update =
S.SQLUpdate tn setExp Nothing tableFltr
. Just
. S.RetExp
$ [ S.selectStar
, asCheckErrorExtractor $ insertCheckConstraint checkExpr
]
setExp = S.SetExp $ map (expandOperator columnsInfo) opExps
tableFltr = Just $ S.WhereFrag tableFltrExpr
tableFltrExpr = toSQLBoolExp (S.QualTable tn) $ andAnnBoolExps permFltr wc
checkExpr = toSQLBoolExp (S.QualTable tn) chk
expandOperator :: [ColumnInfo 'Postgres] -> (PGCol, UpdOpExpG S.SQLExp) -> S.SetExpItem
expandOperator infos (column, op) = S.SetExpItem $ (column,) $ case op of
UpdSet e -> e
UpdInc e -> S.mkSQLOpExp S.incOp identifier (asNum e)
UpdAppend e -> S.mkSQLOpExp S.jsonbConcatOp identifier (asJSON e)
UpdPrepend e -> S.mkSQLOpExp S.jsonbConcatOp (asJSON e) identifier
UpdDeleteKey e -> S.mkSQLOpExp S.jsonbDeleteOp identifier (asText e)
UpdDeleteElem e -> S.mkSQLOpExp S.jsonbDeleteOp identifier (asInt e)
UpdDeleteAtPath a -> S.mkSQLOpExp S.jsonbDeleteAtPathOp identifier (asArray a)
where
identifier = S.SEIdentifier $ toIdentifier column
asInt e = S.SETyAnn e S.intTypeAnn
asText e = S.SETyAnn e S.textTypeAnn
asJSON e = S.SETyAnn e S.jsonbTypeAnn
asArray a = S.SETyAnn (S.SEArray a) S.textArrTypeAnn
asNum e = S.SETyAnn e $
case find (\info -> pgiColumn info == column) infos <&> pgiType of
Just (ColumnScalar s) -> S.mkTypeAnn $ CollectableTypeScalar s
_ -> S.numericTypeAnn