2022-03-16 03:39:21 +03:00
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
|
|
|
|
2022-02-08 20:39:17 +03:00
|
|
|
-- | Testing manual, misconfigured object relationships.
|
|
|
|
-- Specifically, having manual relationships with one-to-many relationships.
|
|
|
|
-- Test case for bug reported at https://github.com/hasura/graphql-engine/issues/7936
|
|
|
|
module Test.ObjectRelationshipsLimitSpec (spec) where
|
|
|
|
|
|
|
|
import Harness.Backend.Postgres as Postgres
|
|
|
|
import Harness.GraphqlEngine qualified as GraphqlEngine
|
|
|
|
import Harness.Quoter.Graphql
|
|
|
|
import Harness.Quoter.Yaml
|
|
|
|
import Harness.State (State)
|
2022-02-21 20:05:09 +03:00
|
|
|
import Harness.Test.Context qualified as Context
|
2022-03-10 14:18:13 +03:00
|
|
|
import Harness.Test.Schema qualified as Schema
|
2022-02-08 20:39:17 +03:00
|
|
|
import Test.Hspec
|
|
|
|
import Prelude
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- * Preamble
|
|
|
|
|
|
|
|
spec :: SpecWith State
|
|
|
|
spec =
|
2022-02-21 20:05:09 +03:00
|
|
|
Context.run
|
|
|
|
[ Context.Context
|
2022-03-15 19:08:47 +03:00
|
|
|
{ name = Context.Backend Context.Postgres,
|
2022-02-21 20:05:09 +03:00
|
|
|
mkLocalState = Context.noLocalState,
|
2022-02-14 20:24:24 +03:00
|
|
|
setup = postgresSetup,
|
|
|
|
teardown = postgresTeardown,
|
2022-02-18 16:35:32 +03:00
|
|
|
customOptions = Nothing
|
2022-02-14 20:24:24 +03:00
|
|
|
}
|
|
|
|
]
|
|
|
|
tests
|
2022-02-08 20:39:17 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2022-03-10 14:18:13 +03:00
|
|
|
-- * Schema
|
|
|
|
|
|
|
|
schema :: [Schema.Table]
|
|
|
|
schema = [author, article]
|
|
|
|
|
|
|
|
author :: Schema.Table
|
|
|
|
author =
|
|
|
|
Schema.Table
|
|
|
|
"author"
|
|
|
|
[ Schema.column "id" Schema.TInt,
|
|
|
|
Schema.column "name" Schema.TStr,
|
|
|
|
Schema.column "createdAt" Schema.TUTCTime
|
|
|
|
]
|
|
|
|
["id"]
|
|
|
|
[]
|
|
|
|
[ [Schema.VInt 1, Schema.VStr "Author 1", Schema.parseUTCTimeOrError "2017-09-21 09:39:44"],
|
|
|
|
[Schema.VInt 2, Schema.VStr "Author 2", Schema.parseUTCTimeOrError "2017-09-21 09:50:44"],
|
|
|
|
[Schema.VInt 3, Schema.VStr "Author 1", Schema.parseUTCTimeOrError "2017-09-21 09:55:44"]
|
|
|
|
]
|
|
|
|
|
|
|
|
article :: Schema.Table
|
|
|
|
article =
|
|
|
|
Schema.Table
|
|
|
|
"article"
|
|
|
|
[ Schema.column "id" Schema.TInt,
|
|
|
|
Schema.column "author_name" Schema.TStr
|
|
|
|
]
|
|
|
|
["id"]
|
|
|
|
[] -- No references; we are using @manual_configuration@ to make the object relationship
|
|
|
|
[ [Schema.VInt 1, Schema.VStr "Author 1"],
|
|
|
|
[Schema.VInt 2, Schema.VStr "Author 2"]
|
|
|
|
]
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- ** Setup and teardown override
|
|
|
|
|
|
|
|
postgresSetup :: (State, ()) -> IO ()
|
|
|
|
postgresSetup (state, ()) = do
|
|
|
|
Postgres.setup schema (state, ())
|
|
|
|
-- also setup special relationship
|
|
|
|
GraphqlEngine.postMetadata_ state $
|
|
|
|
[yaml|
|
|
|
|
type: pg_create_object_relationship
|
|
|
|
args:
|
|
|
|
source: postgres
|
|
|
|
table:
|
|
|
|
schema: hasura
|
|
|
|
name: article
|
|
|
|
name: author
|
|
|
|
using:
|
|
|
|
manual_configuration:
|
|
|
|
remote_table:
|
|
|
|
schema: hasura
|
|
|
|
name: author
|
|
|
|
column_mapping:
|
|
|
|
author_name: name
|
|
|
|
|]
|
|
|
|
|
|
|
|
postgresTeardown :: (State, ()) -> IO ()
|
|
|
|
postgresTeardown (state, ()) = do
|
|
|
|
-- first teardown special relationship
|
|
|
|
GraphqlEngine.postMetadata_ state $
|
|
|
|
[yaml|
|
|
|
|
type: pg_drop_relationship
|
|
|
|
args:
|
|
|
|
source: postgres
|
|
|
|
table:
|
|
|
|
schema: hasura
|
|
|
|
name: article
|
|
|
|
relationship: author
|
|
|
|
|]
|
|
|
|
-- and then rest of the teardown
|
|
|
|
Postgres.teardown schema (state, ())
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2022-02-08 20:39:17 +03:00
|
|
|
-- * Tests
|
|
|
|
|
|
|
|
-- | Many of these may return non-deterministic results because graphql-engine
|
|
|
|
-- need to choose one of the available rows in a misconfigured manual relationship
|
|
|
|
-- that has a one-to-many relationship instead of the expected one-to-one.
|
|
|
|
--
|
|
|
|
-- Because of that, we use 'shouldReturnOneOfYaml' and list all of the possible (valid)
|
|
|
|
-- expected results.
|
2022-02-21 20:05:09 +03:00
|
|
|
tests :: Context.Options -> SpecWith State
|
2022-02-09 18:26:14 +03:00
|
|
|
tests opts = do
|
2022-02-08 20:39:17 +03:00
|
|
|
it "Query by id" $ \state ->
|
|
|
|
shouldReturnOneOfYaml
|
2022-02-09 18:26:14 +03:00
|
|
|
opts
|
2022-02-08 20:39:17 +03:00
|
|
|
( GraphqlEngine.postGraphql
|
|
|
|
state
|
|
|
|
[graphql|
|
|
|
|
query {
|
|
|
|
hasura_article(where: {id: {_eq: 1}}) {
|
|
|
|
id
|
|
|
|
author {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
)
|
|
|
|
[ [yaml|
|
|
|
|
data:
|
|
|
|
hasura_article:
|
|
|
|
- id: 1
|
|
|
|
author:
|
|
|
|
id: 1
|
|
|
|
|],
|
|
|
|
[yaml|
|
|
|
|
data:
|
|
|
|
hasura_article:
|
|
|
|
- id: 1
|
|
|
|
author:
|
|
|
|
id: 3
|
|
|
|
|]
|
|
|
|
]
|
|
|
|
|
|
|
|
it "Query limit 2" $ \state ->
|
|
|
|
shouldReturnOneOfYaml
|
2022-02-09 18:26:14 +03:00
|
|
|
opts
|
2022-02-08 20:39:17 +03:00
|
|
|
( GraphqlEngine.postGraphql
|
|
|
|
state
|
|
|
|
[graphql|
|
|
|
|
query {
|
|
|
|
hasura_article(limit: 2) {
|
|
|
|
id
|
|
|
|
author {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
)
|
|
|
|
[ [yaml|
|
|
|
|
data:
|
|
|
|
hasura_article:
|
|
|
|
- id: 1
|
|
|
|
author:
|
|
|
|
id: 1
|
|
|
|
- id: 2
|
|
|
|
author:
|
|
|
|
id: 2
|
|
|
|
|],
|
|
|
|
[yaml|
|
|
|
|
data:
|
|
|
|
hasura_article:
|
|
|
|
- id: 1
|
|
|
|
author:
|
|
|
|
id: 3
|
|
|
|
- id: 2
|
|
|
|
author:
|
|
|
|
id: 2
|
|
|
|
|]
|
|
|
|
]
|
|
|
|
|
|
|
|
it "where author name" $ \state ->
|
|
|
|
shouldReturnOneOfYaml
|
2022-02-09 18:26:14 +03:00
|
|
|
opts
|
2022-02-08 20:39:17 +03:00
|
|
|
( GraphqlEngine.postGraphql
|
|
|
|
state
|
|
|
|
[graphql|
|
|
|
|
query {
|
|
|
|
hasura_article(where: { author: { name: { _eq: "Author 1" } } }) {
|
|
|
|
id
|
|
|
|
author {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
)
|
|
|
|
[ [yaml|
|
|
|
|
data:
|
|
|
|
hasura_article:
|
|
|
|
- id: 1
|
|
|
|
author:
|
|
|
|
id: 1
|
|
|
|
|],
|
|
|
|
[yaml|
|
|
|
|
data:
|
|
|
|
hasura_article:
|
|
|
|
- id: 1
|
|
|
|
author:
|
|
|
|
id: 3
|
|
|
|
|]
|
|
|
|
]
|
|
|
|
|
|
|
|
it "order by author id" $ \state ->
|
|
|
|
shouldReturnOneOfYaml
|
2022-02-09 18:26:14 +03:00
|
|
|
opts
|
2022-02-08 20:39:17 +03:00
|
|
|
( GraphqlEngine.postGraphql
|
|
|
|
state
|
|
|
|
[graphql|
|
|
|
|
query {
|
|
|
|
hasura_article(order_by: {author: {id: asc}}) {
|
|
|
|
id
|
|
|
|
author {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
)
|
|
|
|
[ [yaml|
|
|
|
|
data:
|
|
|
|
hasura_article:
|
|
|
|
- id: 1
|
|
|
|
author:
|
|
|
|
id: 1
|
|
|
|
- id: 2
|
|
|
|
author:
|
|
|
|
id: 2
|
|
|
|
|],
|
|
|
|
[yaml|
|
|
|
|
data:
|
|
|
|
hasura_article:
|
|
|
|
- id: 2
|
|
|
|
author:
|
|
|
|
id: 2
|
|
|
|
- id: 1
|
|
|
|
author:
|
|
|
|
id: 3
|
|
|
|
|]
|
|
|
|
]
|
|
|
|
|
|
|
|
it "count articles" $ \state ->
|
|
|
|
shouldReturnYaml
|
2022-02-09 18:26:14 +03:00
|
|
|
opts
|
2022-02-08 20:39:17 +03:00
|
|
|
( GraphqlEngine.postGraphql
|
|
|
|
state
|
|
|
|
[graphql|
|
|
|
|
query {
|
|
|
|
hasura_article_aggregate {
|
|
|
|
aggregate {
|
|
|
|
count
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
)
|
|
|
|
[yaml|
|
|
|
|
data:
|
|
|
|
hasura_article_aggregate:
|
|
|
|
aggregate:
|
|
|
|
count: 2
|
|
|
|
|]
|