mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-15 17:31:56 +03:00
Server/tests: Scalar Computed Fields > Postgres
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7094 GitOrigin-RevId: f5e632adc99ffa628a09d6703d2fdba9d2ade53e
This commit is contained in:
parent
07682d4afb
commit
74e30b8b0b
@ -58,7 +58,7 @@ Each ✅ below links **directly** to the feature within a particular type of dat
|
||||
| Views | [✅](/schema/postgres/views.mdx) | [✅](/schema/postgres/views.mdx) | [✅](/schema/ms-sql-server/views.mdx) | ✅ | [✅](/schema/postgres/views.mdx) | [✅](/schema/postgres/views.mdx) |
|
||||
| Custom Functions | [✅](/schema/postgres/custom-functions.mdx) | [✅](/schema/postgres/custom-functions.mdx) | ❌ | ❌ | [❌](/databases/postgres/cockroachdb/hasura-cockroachdb-compatibility.mdx/#functions) | [✅](/schema/postgres/custom-functions.mdx) |
|
||||
| Enums | [✅](/schema/postgres/enums.mdx) | [✅](/schema/postgres/enums.mdx) | ❌ | ❌ | [✅](/schema/postgres/enums.mdx) | ❌ |
|
||||
| Computed Fields | [✅](/schema/postgres/computed-fields.mdx) | [✅](/schema/postgres/computed-fields.mdx) | ❌ | [✅](/schema/bigquery/computed-fields.mdx) | [❌](/databases/postgres/cockroachdb/hasura-cockroachdb-compatibility.mdx/#functions) | [✅](/schema/postgres/computed-fields.mdx) |
|
||||
| Computed Fields | [✅](/schema/postgres/computed-fields.mdx) | [❌](/databases/postgres/citus-hyperscale-postgres/hasura-citus-compatibility.mdx/#computed-fields) | ❌ | [✅](/schema/bigquery/computed-fields.mdx) | [❌](/databases/postgres/cockroachdb/hasura-cockroachdb-compatibility.mdx/#functions) | ❌ |
|
||||
| Data Validations | [✅](/schema/postgres/data-validations.mdx) | [✅](/schema/postgres/data-validations.mdx) | ✅ | [✅](/schema/bigquery/data-validations.mdx) | [✅](/schema/postgres/data-validations.mdx) | [✅](/schema/postgres/data-validations.mdx) |
|
||||
| Relay Schema | [✅](/schema/postgres/relay-schema.mdx) | [✅](/schema/postgres/relay-schema.mdx) | ❌ | ❌ | ❌ | [✅](/schema/postgres/relay-schema.mdx) |
|
||||
| Naming Conventions | [✅](/schema/postgres/naming-convention.mdx) | [❌](/databases/postgres/citus-hyperscale-postgres/hasura-citus-compatibility.mdx/#naming-conventions) | ❌ | ❌ | [❌](/databases/postgres/cockroachdb/hasura-cockroachdb-compatibility.mdx/#naming-conventions) | ❌ |
|
||||
|
@ -62,6 +62,9 @@ restrictions:
|
||||
Citus does not yet support creating triggers ([See issue](https://github.com/citusdata/citus/issues/4425)) on reference
|
||||
tables. Hence this is currently not supported.
|
||||
|
||||
## Computed fields
|
||||
Computed fields are currently supported on Postgres and BigQuery only. [Raise a GitHub issue](https://github.com/hasura/graphql-engine/issues/new?assignees=&labels=k%2Fenhancement&template=02_feature_request.md) to request support for computed fields on Citus sources.
|
||||
|
||||
## Naming conventions
|
||||
|
||||
Naming conventions are currently only implemented as an experimental feature on Postgres sources.
|
||||
|
@ -48,6 +48,7 @@ library
|
||||
, postgres-options
|
||||
, safe-exceptions
|
||||
, split
|
||||
, string-interpolate
|
||||
, sop-core
|
||||
, test-harness
|
||||
, text
|
||||
@ -143,7 +144,8 @@ library
|
||||
Test.Regression.ObjectRelationshipsLimit7936Spec
|
||||
Test.Regression.StreamConflictSpec
|
||||
Test.Regression.UsingTheSameFunctionForRootFieldAndComputedField8643Spec
|
||||
Test.Schema.ComputedFieldsSpec
|
||||
Test.Schema.ComputedFields.TableSpec
|
||||
Test.Schema.ComputedFields.ScalarSpec
|
||||
Test.Schema.ConflictsSpec
|
||||
Test.Schema.CustomFieldNames.MutationSpec
|
||||
Test.Schema.CustomFieldNames.QuerySpec
|
||||
|
@ -0,0 +1,150 @@
|
||||
{-# LANGUAGE QuasiQuotes #-}
|
||||
|
||||
-- | Tests scalar computed fields whose associated SQL function
|
||||
-- returns a base type like Integer, Boolean, Geography etc.
|
||||
--
|
||||
-- https://hasura.io/docs/latest/schema/postgres/computed-fields/#1-scalar-computed-fields
|
||||
module Test.Schema.ComputedFields.ScalarSpec (spec) where
|
||||
|
||||
import Data.List.NonEmpty qualified as NE
|
||||
import Data.String.Interpolate (i)
|
||||
import Data.Text qualified as T
|
||||
import Harness.Backend.Postgres qualified as Postgres
|
||||
import Harness.GraphqlEngine qualified as GraphqlEngine
|
||||
import Harness.Quoter.Graphql (graphql)
|
||||
import Harness.Quoter.Yaml (interpolateYaml, yaml)
|
||||
import Harness.Test.BackendType (BackendType (..))
|
||||
import Harness.Test.BackendType qualified as BackendType
|
||||
import Harness.Test.Fixture qualified as Fixture
|
||||
import Harness.Test.Schema (SchemaName (..), Table (..), table)
|
||||
import Harness.Test.Schema qualified as Schema
|
||||
import Harness.Test.SchemaName (SchemaName (..))
|
||||
import Harness.TestEnvironment (TestEnvironment)
|
||||
import Harness.Yaml (shouldReturnYaml)
|
||||
import Hasura.Prelude
|
||||
import Test.Hspec (SpecWith, it)
|
||||
|
||||
-- ** Preamble
|
||||
|
||||
spec :: SpecWith TestEnvironment
|
||||
spec =
|
||||
Fixture.run
|
||||
( NE.fromList
|
||||
[ (Fixture.fixture $ Fixture.Backend Fixture.Postgres)
|
||||
{ Fixture.setupTeardown = \(testEnv, _) ->
|
||||
[ Postgres.setupTablesAction schema testEnv
|
||||
]
|
||||
<> setupFunction testEnv
|
||||
<> setupMetadata testEnv BackendType.Postgres
|
||||
}
|
||||
]
|
||||
)
|
||||
tests
|
||||
|
||||
-- ** Schema
|
||||
|
||||
schema :: [Table]
|
||||
schema = [authorTable]
|
||||
|
||||
authorTable :: Table
|
||||
authorTable =
|
||||
(table "author")
|
||||
{ tableColumns =
|
||||
[ Schema.column "id" Schema.TInt,
|
||||
Schema.column "first_name" Schema.TStr,
|
||||
Schema.column "last_name" Schema.TStr
|
||||
],
|
||||
tablePrimaryKey = ["id"],
|
||||
tableData =
|
||||
[ [ Schema.VInt 1,
|
||||
Schema.VStr "Author",
|
||||
Schema.VStr "1"
|
||||
],
|
||||
[ Schema.VInt 2,
|
||||
Schema.VStr "Author",
|
||||
Schema.VStr "2"
|
||||
]
|
||||
]
|
||||
}
|
||||
|
||||
-- ** Setup and teardown
|
||||
|
||||
-- | SQL
|
||||
authorFullNameSQL :: SchemaName -> String
|
||||
authorFullNameSQL schemaName =
|
||||
[i|
|
||||
CREATE FUNCTION #{ unSchemaName schemaName }.author_full_name(author_row author)
|
||||
RETURNS TEXT AS $$
|
||||
SELECT author_row.first_name || ' ' || author_row.last_name
|
||||
$$ LANGUAGE sql STABLE;
|
||||
|]
|
||||
|
||||
setupFunction :: TestEnvironment -> [Fixture.SetupAction]
|
||||
setupFunction testEnv =
|
||||
let schemaName = Schema.getSchemaName testEnv
|
||||
in [ Fixture.SetupAction
|
||||
{ Fixture.setupAction =
|
||||
Postgres.run_ testEnv (authorFullNameSQL schemaName),
|
||||
Fixture.teardownAction = \_ -> pure ()
|
||||
}
|
||||
]
|
||||
|
||||
setupMetadata :: TestEnvironment -> BackendType -> [Fixture.SetupAction]
|
||||
setupMetadata testEnv backend =
|
||||
let schemaName :: Schema.SchemaName
|
||||
schemaName = Schema.getSchemaName testEnv
|
||||
|
||||
source :: String
|
||||
source = Fixture.defaultSource backend
|
||||
|
||||
backendPrefix :: String
|
||||
backendPrefix = BackendType.defaultBackendTypeString backend
|
||||
in [ Fixture.SetupAction
|
||||
{ Fixture.setupAction =
|
||||
GraphqlEngine.postMetadata_
|
||||
testEnv
|
||||
[interpolateYaml|
|
||||
type: #{ backendPrefix }_add_computed_field
|
||||
args:
|
||||
source: #{ source }
|
||||
name: full_name
|
||||
table:
|
||||
schema: #{ schemaName }
|
||||
name: author
|
||||
definition:
|
||||
function:
|
||||
schema: #{ schemaName }
|
||||
name: author_full_name
|
||||
|],
|
||||
Fixture.teardownAction = \_ -> pure ()
|
||||
}
|
||||
]
|
||||
|
||||
-- * Tests
|
||||
|
||||
tests :: Fixture.Options -> SpecWith TestEnvironment
|
||||
tests opts = do
|
||||
it "Query data from the authors table" $ \testEnv -> do
|
||||
let schemaName = Schema.getSchemaName testEnv
|
||||
|
||||
shouldReturnYaml
|
||||
opts
|
||||
( GraphqlEngine.postGraphql
|
||||
testEnv
|
||||
[graphql|
|
||||
query {
|
||||
#{schemaName}_author {
|
||||
id
|
||||
full_name
|
||||
}
|
||||
}
|
||||
|]
|
||||
)
|
||||
[interpolateYaml|
|
||||
data:
|
||||
#{schemaName}_author:
|
||||
- id: 1
|
||||
full_name: Author 1
|
||||
- id: 2
|
||||
full_name: Author 2
|
||||
|]
|
@ -1,7 +1,7 @@
|
||||
{-# LANGUAGE QuasiQuotes #-}
|
||||
|
||||
-- | All tests related to computed fields
|
||||
module Test.Schema.ComputedFieldsSpec (spec) where
|
||||
module Test.Schema.ComputedFields.TableSpec (spec) where
|
||||
|
||||
import Data.List.NonEmpty qualified as NE
|
||||
import Data.Text qualified as T
|
Loading…
Reference in New Issue
Block a user