2022-03-16 03:39:21 +03:00
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
|
|
|
|
2022-03-14 18:34:14 +03:00
|
|
|
-- | Testing column presets.
|
2022-03-15 19:08:47 +03:00
|
|
|
-- See the main hasura documentation for more information.
|
|
|
|
--
|
|
|
|
-- - Postgres: https://hasura.io/docs/latest/graphql/core/databases/postgres/schema/default-values/column-presets.html
|
|
|
|
-- - MSSQL: https://hasura.io/docs/latest/graphql/core/databases/ms-sql-server/schema/default-values/mssql-column-presets.html
|
2022-03-14 18:34:14 +03:00
|
|
|
module Test.ColumnPresetsSpec (spec) where
|
|
|
|
|
|
|
|
import Harness.Backend.Postgres qualified as Postgres
|
|
|
|
import Harness.Backend.Sqlserver qualified as Sqlserver
|
|
|
|
import Harness.GraphqlEngine qualified as GraphqlEngine
|
|
|
|
import Harness.Quoter.Graphql (graphql)
|
|
|
|
import Harness.Quoter.Yaml (shouldReturnYaml, yaml)
|
|
|
|
import Harness.Test.Context qualified as Context
|
2022-06-17 11:44:04 +03:00
|
|
|
import Harness.Test.Schema (Table (..), table)
|
2022-03-15 19:08:47 +03:00
|
|
|
import Harness.Test.Schema qualified as Schema
|
2022-04-20 20:15:42 +03:00
|
|
|
import Harness.TestEnvironment (TestEnvironment)
|
2022-03-14 18:34:14 +03:00
|
|
|
import Test.Hspec (SpecWith, it)
|
|
|
|
import Prelude
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- * Preamble
|
|
|
|
|
2022-04-20 20:15:42 +03:00
|
|
|
spec :: SpecWith TestEnvironment
|
2022-03-14 18:34:14 +03:00
|
|
|
spec =
|
|
|
|
Context.run
|
|
|
|
[ Context.Context
|
2022-03-15 19:08:47 +03:00
|
|
|
{ name = Context.Backend Context.SQLServer,
|
2022-04-20 20:15:42 +03:00
|
|
|
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
|
2022-03-14 18:34:14 +03:00
|
|
|
setup = sqlserverSetup,
|
2022-03-15 19:08:47 +03:00
|
|
|
teardown = Sqlserver.teardown schema,
|
2022-03-14 18:34:14 +03:00
|
|
|
customOptions = Nothing
|
|
|
|
},
|
|
|
|
Context.Context
|
2022-03-15 19:08:47 +03:00
|
|
|
{ name = Context.Backend Context.Postgres,
|
2022-04-20 20:15:42 +03:00
|
|
|
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
|
2022-03-14 18:34:14 +03:00
|
|
|
setup = postgresSetup,
|
2022-03-15 19:08:47 +03:00
|
|
|
teardown = Postgres.teardown schema,
|
2022-03-14 18:34:14 +03:00
|
|
|
customOptions = Nothing
|
|
|
|
}
|
|
|
|
]
|
|
|
|
tests
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- * Tests
|
|
|
|
|
2022-04-20 20:15:42 +03:00
|
|
|
tests :: Context.Options -> SpecWith TestEnvironment
|
2022-03-14 18:34:14 +03:00
|
|
|
tests opts = do
|
|
|
|
----------------------------------------
|
2022-04-20 20:15:42 +03:00
|
|
|
it "admin role is unaffected by column presets" $ \testEnvironment ->
|
2022-03-14 18:34:14 +03:00
|
|
|
shouldReturnYaml
|
|
|
|
opts
|
|
|
|
( GraphqlEngine.postGraphql
|
2022-04-20 20:15:42 +03:00
|
|
|
testEnvironment
|
2022-03-14 18:34:14 +03:00
|
|
|
[graphql|
|
|
|
|
mutation author {
|
|
|
|
insert_hasura_author(objects:
|
|
|
|
{ name: "Author 1"
|
|
|
|
uuid: "36a6257b-1111-1111-2222-c1b7a7997087"
|
|
|
|
company: "arusah"
|
|
|
|
}) {
|
|
|
|
returning {
|
|
|
|
uuid
|
|
|
|
name
|
|
|
|
company
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
)
|
|
|
|
[yaml|
|
|
|
|
data:
|
|
|
|
insert_hasura_author:
|
|
|
|
returning:
|
|
|
|
- uuid: '36a6257b-1111-1111-2222-c1b7a7997087'
|
|
|
|
name: 'Author 1'
|
|
|
|
company: 'arusah'
|
|
|
|
|]
|
|
|
|
|
|
|
|
----------------------------------------
|
2022-04-20 20:15:42 +03:00
|
|
|
it "applies the column presets to the 'author id' and 'company' columns" $ \testEnvironment ->
|
2022-03-14 18:34:14 +03:00
|
|
|
shouldReturnYaml
|
|
|
|
opts
|
|
|
|
( GraphqlEngine.postGraphqlWithHeaders
|
2022-04-20 20:15:42 +03:00
|
|
|
testEnvironment
|
2022-03-14 18:34:14 +03:00
|
|
|
[ ("X-Hasura-Role", "user"),
|
|
|
|
("X-Hasura-User-Id", "36a6257b-1111-1111-1111-c1b7a7997087")
|
|
|
|
]
|
|
|
|
[graphql|
|
|
|
|
mutation author {
|
|
|
|
insert_hasura_author(objects: { name: "Author 2" }) {
|
|
|
|
returning {
|
|
|
|
uuid
|
|
|
|
name
|
|
|
|
company
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
)
|
|
|
|
[yaml|
|
|
|
|
data:
|
|
|
|
insert_hasura_author:
|
|
|
|
returning:
|
|
|
|
- uuid: '36a6257b-1111-1111-1111-c1b7a7997087'
|
|
|
|
name: 'Author 2'
|
|
|
|
company: 'hasura'
|
|
|
|
|]
|
|
|
|
|
|
|
|
----------------------------------------
|
2022-04-20 20:15:42 +03:00
|
|
|
it "Columns with session variables presets defined are not part of the schema" $ \testEnvironment ->
|
2022-03-14 18:34:14 +03:00
|
|
|
shouldReturnYaml
|
|
|
|
opts
|
|
|
|
( GraphqlEngine.postGraphqlWithHeaders
|
2022-04-20 20:15:42 +03:00
|
|
|
testEnvironment
|
2022-03-14 18:34:14 +03:00
|
|
|
[ ("X-Hasura-Role", "user"),
|
|
|
|
("X-Hasura-User-Id", "36a6257b-1111-1111-1111-c1b7a7997087")
|
|
|
|
]
|
|
|
|
[graphql|
|
|
|
|
mutation author {
|
|
|
|
insert_hasura_author(objects:
|
|
|
|
{ name: "Author 3"
|
|
|
|
uuid: "36a6257b-1111-1111-2222-c1b7a7997087"
|
|
|
|
}) {
|
|
|
|
returning {
|
|
|
|
uuid
|
|
|
|
name
|
|
|
|
company
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
)
|
|
|
|
[yaml|
|
|
|
|
errors:
|
|
|
|
- extensions:
|
|
|
|
path: $.selectionSet.insert_hasura_author.args.objects[0].uuid
|
|
|
|
code: validation-failed
|
An `ErrorMessage` type, to encapsulate.
This introduces an `ErrorMessage` newtype which wraps `Text` in a manner which is designed to be easy to construct, and difficult to deconstruct.
It provides functionality similar to `Data.Text.Extended`, but designed _only_ for error messages. Error messages are constructed through `fromString`, concatenation, or the `toErrorValue` function, which is designed to be overridden for all meaningful domain types that might show up in an error message. Notably, there are not and should never be instances of `ToErrorValue` for `String`, `Text`, `Int`, etc. This is so that we correctly represent the value in a way that is specific to its type. For example, all `Name` values (from the _graphql-parser-hs_ library) are single-quoted now; no exceptions.
I have mostly had to add `instance ToErrorValue` for various backend types (and also add newtypes where necessary). Some of these are not strictly necessary for this changeset, as I had bigger aspirations when I started. These aspirations have been tempered by trying and failing twice.
As such, in this changeset, I have started by introducing this type to the `parseError` and `parseErrorWith` functions. In the future, I would like to extend this to the `QErr` record and the various `throwError` functions, but this is a much larger task and should probably be done in stages.
For now, `toErrorMessage` and `fromErrorMessage` are provided for conversion to and from `Text`, but the intent is to stop exporting these once all error messages are converted to the new type.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5018
GitOrigin-RevId: 84b37e238992e4312255a87ca44f41af65e2d89a
2022-07-18 23:26:01 +03:00
|
|
|
message: |-
|
|
|
|
field 'uuid' not found in type: 'hasura_author_insert_input'
|
2022-03-14 18:34:14 +03:00
|
|
|
|]
|
|
|
|
|
|
|
|
----------------------------------------
|
2022-04-20 20:15:42 +03:00
|
|
|
it "Columns with static presets defined are not part of the schema" $ \testEnvironment ->
|
2022-03-14 18:34:14 +03:00
|
|
|
shouldReturnYaml
|
|
|
|
opts
|
|
|
|
( GraphqlEngine.postGraphqlWithHeaders
|
2022-04-20 20:15:42 +03:00
|
|
|
testEnvironment
|
2022-03-14 18:34:14 +03:00
|
|
|
[ ("X-Hasura-Role", "user"),
|
|
|
|
("X-Hasura-User-Id", "36a6257b-1111-1111-1111-c1b7a7997087")
|
|
|
|
]
|
|
|
|
[graphql|
|
|
|
|
mutation author {
|
|
|
|
insert_hasura_author(objects: { name: "Author 4" company: "arusah" }) {
|
|
|
|
returning {
|
|
|
|
uuid
|
|
|
|
name
|
|
|
|
company
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
)
|
|
|
|
[yaml|
|
|
|
|
errors:
|
|
|
|
- extensions:
|
|
|
|
path: $.selectionSet.insert_hasura_author.args.objects[0].company
|
|
|
|
code: validation-failed
|
An `ErrorMessage` type, to encapsulate.
This introduces an `ErrorMessage` newtype which wraps `Text` in a manner which is designed to be easy to construct, and difficult to deconstruct.
It provides functionality similar to `Data.Text.Extended`, but designed _only_ for error messages. Error messages are constructed through `fromString`, concatenation, or the `toErrorValue` function, which is designed to be overridden for all meaningful domain types that might show up in an error message. Notably, there are not and should never be instances of `ToErrorValue` for `String`, `Text`, `Int`, etc. This is so that we correctly represent the value in a way that is specific to its type. For example, all `Name` values (from the _graphql-parser-hs_ library) are single-quoted now; no exceptions.
I have mostly had to add `instance ToErrorValue` for various backend types (and also add newtypes where necessary). Some of these are not strictly necessary for this changeset, as I had bigger aspirations when I started. These aspirations have been tempered by trying and failing twice.
As such, in this changeset, I have started by introducing this type to the `parseError` and `parseErrorWith` functions. In the future, I would like to extend this to the `QErr` record and the various `throwError` functions, but this is a much larger task and should probably be done in stages.
For now, `toErrorMessage` and `fromErrorMessage` are provided for conversion to and from `Text`, but the intent is to stop exporting these once all error messages are converted to the new type.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5018
GitOrigin-RevId: 84b37e238992e4312255a87ca44f41af65e2d89a
2022-07-18 23:26:01 +03:00
|
|
|
message: |-
|
|
|
|
field 'company' not found in type: 'hasura_author_insert_input'
|
2022-03-14 18:34:14 +03:00
|
|
|
|]
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2022-03-15 19:08:47 +03:00
|
|
|
-- * Backend
|
|
|
|
|
|
|
|
-- ** Schema
|
|
|
|
|
|
|
|
schema :: [Schema.Table]
|
|
|
|
schema =
|
2022-06-17 11:44:04 +03:00
|
|
|
[ (table "author")
|
|
|
|
{ tableColumns =
|
2022-03-15 19:08:47 +03:00
|
|
|
[ Schema.column "uuid" Schema.TStr,
|
|
|
|
Schema.column "name" Schema.TStr,
|
|
|
|
Schema.column "company" Schema.TStr
|
|
|
|
],
|
|
|
|
tablePrimaryKey = ["uuid"],
|
|
|
|
tableReferences = [],
|
|
|
|
tableData = []
|
|
|
|
}
|
|
|
|
]
|
2022-03-14 18:34:14 +03:00
|
|
|
|
2022-03-15 19:08:47 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- ** Postgres backend
|
2022-03-14 18:34:14 +03:00
|
|
|
|
2022-04-20 20:15:42 +03:00
|
|
|
postgresSetup :: (TestEnvironment, ()) -> IO ()
|
|
|
|
postgresSetup (testEnvironment, localTestEnvironment) = do
|
|
|
|
Postgres.setup schema (testEnvironment, localTestEnvironment)
|
|
|
|
postgresCreatePermissions testEnvironment
|
2022-03-14 18:34:14 +03:00
|
|
|
|
2022-04-20 20:15:42 +03:00
|
|
|
postgresCreatePermissions :: TestEnvironment -> IO ()
|
|
|
|
postgresCreatePermissions testEnvironment = do
|
2022-03-14 18:34:14 +03:00
|
|
|
GraphqlEngine.postMetadata_
|
2022-04-20 20:15:42 +03:00
|
|
|
testEnvironment
|
2022-03-14 18:34:14 +03:00
|
|
|
[yaml|
|
|
|
|
type: pg_create_select_permission
|
|
|
|
args:
|
|
|
|
source: postgres
|
|
|
|
table:
|
|
|
|
schema: hasura
|
|
|
|
name: author
|
|
|
|
role: user
|
|
|
|
permission:
|
|
|
|
filter:
|
|
|
|
uuid: X-Hasura-User-Id
|
|
|
|
columns: '*'
|
|
|
|
|]
|
|
|
|
GraphqlEngine.postMetadata_
|
2022-04-20 20:15:42 +03:00
|
|
|
testEnvironment
|
2022-03-14 18:34:14 +03:00
|
|
|
[yaml|
|
|
|
|
type: pg_create_insert_permission
|
|
|
|
args:
|
|
|
|
source: postgres
|
|
|
|
table:
|
|
|
|
schema: hasura
|
|
|
|
name: author
|
|
|
|
role: user
|
|
|
|
permission:
|
|
|
|
check: {}
|
|
|
|
set:
|
|
|
|
uuid: X-Hasura-User-Id
|
|
|
|
company: hasura
|
|
|
|
columns: '*'
|
|
|
|
|]
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2022-03-15 19:08:47 +03:00
|
|
|
-- ** SQL Server backend
|
2022-03-14 18:34:14 +03:00
|
|
|
|
2022-04-20 20:15:42 +03:00
|
|
|
sqlserverSetup :: (TestEnvironment, ()) -> IO ()
|
|
|
|
sqlserverSetup (testEnvironment, localTestEnvironment) = do
|
|
|
|
Sqlserver.setup schema (testEnvironment, localTestEnvironment)
|
|
|
|
sqlserverCreatePermissions testEnvironment
|
2022-03-14 18:34:14 +03:00
|
|
|
|
2022-04-20 20:15:42 +03:00
|
|
|
sqlserverCreatePermissions :: TestEnvironment -> IO ()
|
|
|
|
sqlserverCreatePermissions testEnvironment = do
|
2022-03-14 18:34:14 +03:00
|
|
|
GraphqlEngine.postMetadata_
|
2022-04-20 20:15:42 +03:00
|
|
|
testEnvironment
|
2022-03-14 18:34:14 +03:00
|
|
|
[yaml|
|
|
|
|
type: mssql_create_select_permission
|
|
|
|
args:
|
|
|
|
source: mssql
|
|
|
|
table:
|
|
|
|
schema: hasura
|
|
|
|
name: author
|
|
|
|
role: user
|
|
|
|
permission:
|
|
|
|
filter:
|
|
|
|
uuid: X-Hasura-User-Id
|
|
|
|
columns: '*'
|
|
|
|
|]
|
|
|
|
GraphqlEngine.postMetadata_
|
2022-04-20 20:15:42 +03:00
|
|
|
testEnvironment
|
2022-03-14 18:34:14 +03:00
|
|
|
[yaml|
|
|
|
|
type: mssql_create_insert_permission
|
|
|
|
args:
|
|
|
|
source: mssql
|
|
|
|
table:
|
|
|
|
schema: hasura
|
|
|
|
name: author
|
|
|
|
role: user
|
|
|
|
permission:
|
|
|
|
check: {}
|
|
|
|
set:
|
|
|
|
uuid: X-Hasura-User-Id
|
|
|
|
company: hasura
|
|
|
|
columns: '*'
|
|
|
|
|]
|