graphql-engine/server/tests-hspec/Test/ObjectRelationshipsSpec.hs
jkachmar 647231b685 Yeet some default-extensions
Manually enables:
* EmptyCase
* ExistentialQuantification
* QuantifiedConstraints
* QuasiQuotes
* TemplateHaskell
* TypeFamilyDependencies

...in the following components:
* 'graphql-engine' library
* 'graphql-engine' 'src-test'
* 'graphql-engine' 'tests/integration'
* 'graphql-engine' tests-hspec'

Additionally, performs some light refactoring and documentation.

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/3991
GitOrigin-RevId: 514477d3466b01f60eca8935d0fef60dd0756838
2022-03-16 00:40:17 +00:00

152 lines
3.3 KiB
Haskell

{-# LANGUAGE QuasiQuotes #-}
-- | Testing object relationships.
module Test.ObjectRelationshipsSpec (spec) where
import Harness.Backend.Mysql qualified as Mysql
import Harness.Backend.Sqlserver qualified as Sqlserver
import Harness.GraphqlEngine qualified as GraphqlEngine
import Harness.Quoter.Graphql
import Harness.Quoter.Yaml
import Harness.State (State)
import Harness.Test.Context qualified as Context
import Harness.Test.Schema qualified as Schema
import Test.Hspec
import Prelude
--------------------------------------------------------------------------------
-- Preamble
spec :: SpecWith State
spec = do
Context.run
[ Context.Context
{ name = Context.Backend Context.SQLServer,
mkLocalState = Context.noLocalState,
setup = Sqlserver.setup schema,
teardown = Sqlserver.teardown schema,
customOptions = Nothing
}
]
mssqlTests
Context.run
[ Context.Context
{ name = Context.Backend Context.MySQL,
mkLocalState = Context.noLocalState,
setup = Mysql.setup schema,
teardown = Mysql.teardown schema,
customOptions = Nothing
}
]
mysqlTests
--------------------------------------------------------------------------------
-- Schema
schema :: [Schema.Table]
schema = [author, article]
author :: Schema.Table
author =
Schema.Table
"author"
[ Schema.column "id" Schema.TInt,
Schema.column "name" Schema.TStr
]
["id"]
[]
[ [Schema.VInt 1, Schema.VStr "Author 1"],
[Schema.VInt 2, Schema.VStr "Author 2"]
]
article :: Schema.Table
article =
Schema.Table
"article"
[ Schema.column "id" Schema.TInt,
Schema.columnNull "author_id" Schema.TInt
]
["id"]
[Schema.Reference "author_id" "author" "id"]
[ [ Schema.VInt 1,
Schema.VInt 1
],
[ Schema.VInt 2,
Schema.VInt 1
],
[ Schema.VInt 3,
Schema.VInt 2
],
[ Schema.VInt 4,
Schema.VNull
]
]
--------------------------------------------------------------------------------
-- Tests
mssqlTests :: Context.Options -> SpecWith State
mssqlTests opts = do
usingWhereClause opts
nullField opts
mysqlTests :: Context.Options -> SpecWith State
mysqlTests opts = do
usingWhereClause opts
xdescribe
"Pending: The MySQL backend currently fails with relationship fields that are null.\
\ (https://github.com/hasura/graphql-engine-mono/issues/3650)"
(nullField opts)
usingWhereClause :: Context.Options -> SpecWith State
usingWhereClause opts = do
it "Author of article where id=1" $ \state ->
shouldReturnYaml
opts
( GraphqlEngine.postGraphql
state
[graphql|
query {
hasura_article(where: {id: {_eq: 1}}) {
id
author_by_author_id {
id
}
}
}
|]
)
[yaml|
data:
hasura_article:
- id: 1
author_by_author_id:
id: 1
|]
nullField :: Context.Options -> SpecWith State
nullField opts = do
it "Can realise a null relationship field" $ \state ->
shouldReturnYaml
opts
( GraphqlEngine.postGraphql
state
[graphql|
query {
hasura_article(where: {id: {_eq: 4}}) {
id
author_by_author_id {
id
}
}
}
|]
)
[yaml|
data:
hasura_article:
- author_by_author_id: null
id: 4
|]