server: fix bug when executing a custom function with a row type argument

GitOrigin-RevId: d3a1fa8c9f6b420f4c88eb60c92019c8d0fdc58d
This commit is contained in:
Karthikeyan Chinnakonda 2021-05-06 14:48:57 +05:30 committed by hasura-bot
parent 3fad5f6678
commit d7ccf81526
5 changed files with 93 additions and 64 deletions

View File

@ -6,6 +6,7 @@
(Add entries below in the order of: server, console, cli, docs, others)
- server: fix query execution of custom function containing a composite argument type
- server: fix a bug in query validation that would cause some queries using default variable values to be rejected (fix #6867)
- server: custom URI schemes are now supported in CORS config (fix #5818) (#5940)
- console: read-only modify page for mssql

View File

@ -285,6 +285,7 @@ data PGScalarType
| PGLquery
| PGLtxtquery
| PGUnknown !Text
| PGCompositeScalar !Text
deriving (Show, Eq, Ord, Generic, Data)
instance NFData PGScalarType
instance Hashable PGScalarType
@ -320,6 +321,7 @@ instance ToSQL PGScalarType where
PGLquery -> "lquery"
PGLtxtquery -> "ltxtquery"
PGUnknown t -> TB.text t
PGCompositeScalar t -> TB.text t
instance ToJSON PGScalarType where
toJSON = String . toSQLTxt
@ -509,12 +511,7 @@ typeToTable (QualifiedPGType sch n _) =
mkFunctionArgScalarType :: QualifiedPGType -> PGScalarType
mkFunctionArgScalarType (QualifiedPGType _schema name type') =
case type' of
-- When the function argument is a row type argument
-- then it's possible that there can be an object type
-- with the table name depending upon whether the table
-- is tracked or not. As a result, we get a conflict between
-- both these types (scalar and object type with same name).
-- To avoid this, we suffix the table name with `_scalar`
-- and create a new scalar type
PGKindComposite -> PGUnknown $ toTxt name <> "_scalar"
-- The suffix `_scalar` is added in
-- the @mkScalarTypeName@ function.
PGKindComposite -> PGCompositeScalar $ toTxt name
_ -> name

View File

@ -202,6 +202,8 @@ parsePGValue ty val = case (ty, val) of
PGLtxtquery -> PGValLtxtquery <$> parseJSON val
PGUnknown tyName ->
fail $ "A string is expected for type: " ++ T.unpack tyName
PGCompositeScalar tyName ->
fail $ "A string is expected for type: " ++ T.unpack tyName
txtEncodedVal :: PGScalarValue -> TxtEncodedVal
txtEncodedVal = \case
@ -271,6 +273,7 @@ pgTypeOid = \case
PGLquery -> PTI.text
PGLtxtquery -> PTI.text
(PGUnknown _) -> PTI.auto
PGCompositeScalar _ -> PTI.auto
binEncoder :: PGScalarValue -> Q.PrepArg
binEncoder = \case

View File

@ -392,6 +392,17 @@ mkScalarTypeName PG.PGBoolean = pure boolScalar
mkScalarTypeName PG.PGFloat = pure floatScalar
mkScalarTypeName PG.PGText = pure stringScalar
mkScalarTypeName PG.PGVarchar = pure stringScalar
mkScalarTypeName (PG.PGCompositeScalar compositeScalarType) =
-- When the function argument is a row type argument
-- then it's possible that there can be an object type
-- with the table name depending upon whether the table
-- is tracked or not. As a result, we get a conflict between
-- both these types (scalar and object type with same name).
-- To avoid this, we suffix the table name with `_scalar`
-- and create a new scalar type
(<> $$(G.litName "_scalar")) <$> G.mkName compositeScalarType `onNothing` throw400 ValidationFailed
("cannot use SQL type " <> compositeScalarType <<> " in the GraphQL schema because its name is not a "
<> "valid GraphQL identifier")
mkScalarTypeName scalarType = G.mkName (toSQLTxt scalarType) `onNothing` throw400 ValidationFailed
("cannot use SQL type " <> scalarType <<> " in the GraphQL schema because its name is not a "
<> "valid GraphQL identifier")

View File

@ -38,3 +38,20 @@
kind
}
}
- description: Execute the tracked function with a composite row type argument
url: /v1/graphql
status: 200
query:
query: |
query {
search_post_by_test (args: {t: "(1, hasura,311cf381-71e7-449b-bac5-86cd6deafd5b)"}) {
title
content
}
}
response:
data:
search_post_by_test:
- title: post by hasura
content: content for post