graphql-engine/server/tests-hspec/Test/NestedRelationshipsSpec.hs
2022-01-21 07:49:31 +00:00

223 lines
4.4 KiB
Haskell

{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RecordWildCards #-}
-- | Testing nested relationships.
--
-- Original inspiration for this module Test.is <https://github.com/hasura/graphql-engine-mono/blob/08caf7df10cad0aea0916327736147a0a8f808d1/server/tests-py/queries/graphql_query/mysql/nested_select_query_deep.yaml>
module Test.NestedRelationshipsSpec (spec) where
import Harness.Backend.Mysql as Mysql
import Harness.Constants
import Harness.GraphqlEngine qualified as GraphqlEngine
import Harness.Quoter.Graphql
import Harness.Quoter.Sql
import Harness.Quoter.Yaml
import Harness.State (State)
import Harness.Test.Feature qualified as Feature
import Test.Hspec
import Prelude
--------------------------------------------------------------------------------
-- Preamble
spec :: SpecWith State
spec =
Feature.feature
Feature.Feature
{ Feature.backends =
[ Feature.Backend
{ name = "MySQL",
setup = mysqlSetup,
teardown = mysqlTeardown
}
],
Feature.tests = tests
}
--------------------------------------------------------------------------------
-- MySQL backend
mysqlSetup :: State -> IO ()
mysqlSetup state = do
-- Clear and reconfigure the metadata
GraphqlEngine.post_
state
"/v1/metadata"
[yaml|
type: replace_metadata
args:
version: 3
sources:
- name: mysql
kind: mysql
tables: []
configuration:
database: *mysqlDatabase
user: *mysqlUser
password: *mysqlPassword
host: *mysqlHost
port: *mysqlPort
pool_settings: {}
|]
-- Setup tables
Mysql.run_
[sql|
CREATE TABLE author
(
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(45) UNIQUE KEY,
createdAt DATETIME
);
|]
Mysql.run_
[sql|
INSERT INTO author
(name, createdAt)
VALUES
( 'Author 1', '2017-09-21 09:39:44' ),
( 'Author 2', '2017-09-21 09:50:44' );
|]
-- Setup tables
Mysql.run_
[sql|
CREATE TABLE article (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
title TEXT,
content TEXT,
is_published BIT,
published_on TIMESTAMP,
author_id INT UNSIGNED,
co_author_id INT UNSIGNED,
FOREIGN KEY (author_id) REFERENCES author(id),
FOREIGN KEY (co_author_id) REFERENCES author(id)
);
|]
Mysql.run_
[sql|
INSERT INTO article
(title, content, author_id, is_published)
VALUES
( 'Article 1', 'Sample article content 1', 1, 0 ),
( 'Article 2', 'Sample article content 2', 1, 1 ),
( 'Article 3', 'Sample article content 3', 2, 1 );
|]
-- Track the tables
GraphqlEngine.post_
state
"/v1/metadata"
[yaml|
type: mysql_track_table
args:
source: mysql
table:
schema: hasura
name: author
|]
GraphqlEngine.post_
state
"/v1/metadata"
[yaml|
type: mysql_track_table
args:
source: mysql
table:
schema: hasura
name: article
|]
-- Setup relationships
GraphqlEngine.post_
state
"/v1/metadata"
[yaml|
type: mysql_create_object_relationship
args:
source: mysql
table:
name: article
schema: hasura
name: author
using:
foreign_key_constraint_on: author_id
|]
GraphqlEngine.post_
state
"/v1/metadata"
[yaml|
type: mysql_create_array_relationship
args:
source: mysql
table:
name: author
schema: hasura
name: articles
using:
foreign_key_constraint_on:
table:
name: article
schema: hasura
column: author_id
|]
mysqlTeardown :: State -> IO ()
mysqlTeardown _ = do
Mysql.run_
[sql|
DROP TABLE article;
|]
Mysql.run_
[sql|
DROP TABLE author;
|]
--------------------------------------------------------------------------------
-- Tests
tests :: SpecWith State
tests = do
it "Nested select on article" $ \state ->
shouldReturnYaml
( GraphqlEngine.postGraphql
state
[graphql|
query {
hasura_article(where: {id: {_eq: 1}}) {
id
author {
id
articles(where: {id: {_eq: 1}}) {
id
author {
id
articles(where: {id: {_eq: 1}}) {
id
author {
id
}
}
}
}
}
}
}
|]
)
[yaml|
data:
hasura_article:
- id: 1
author:
id: 1
articles:
- id: 1
author:
id: 1
articles:
- id: 1
author:
id: 1
|]