2022-03-17 23:53:56 +03:00
|
|
|
{-# LANGUAGE DuplicateRecordFields #-}
|
2022-03-16 03:39:21 +03:00
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
|
|
|
|
2022-02-18 16:35:32 +03:00
|
|
|
-- | Tests for array remote relationships to databases. Remote relationships are
|
|
|
|
-- relationships that are not local to a given source or remote schema, and are
|
|
|
|
-- handled by the engine itself. Array relationsips are 1-to-many relationships.
|
|
|
|
--
|
|
|
|
-- All tests use the same GraphQL syntax, and the only difference is in the
|
|
|
|
-- setup: we do a cartesian product of all kinds of sources we support on the
|
|
|
|
-- left-hand side and all databases we support on the right-hand side.
|
|
|
|
module Test.RemoteRelationship.XToDBArrayRelationshipSpec
|
|
|
|
( spec,
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Control.Lens (findOf, has, only, (^?!))
|
|
|
|
import Data.Aeson (Value)
|
|
|
|
import Data.Aeson.Lens (key, values, _String)
|
2022-03-17 23:53:56 +03:00
|
|
|
import Data.Char (isUpper, toLower)
|
2022-08-03 17:18:43 +03:00
|
|
|
import Data.List.NonEmpty qualified as NE
|
2022-03-17 23:53:56 +03:00
|
|
|
import Data.List.Split (dropBlanks, keepDelimsL, split, whenElt)
|
2022-02-18 16:35:32 +03:00
|
|
|
import Data.Maybe qualified as Unsafe (fromJust)
|
2022-03-17 23:53:56 +03:00
|
|
|
import Data.Morpheus.Document (gqlDocument)
|
|
|
|
import Data.Morpheus.Types
|
|
|
|
import Data.Morpheus.Types qualified as Morpheus
|
|
|
|
import Data.Typeable (Typeable)
|
2022-02-18 16:35:32 +03:00
|
|
|
import Harness.Backend.Postgres qualified as Postgres
|
2022-04-06 10:18:59 +03:00
|
|
|
import Harness.Backend.Sqlserver qualified as SQLServer
|
2022-02-18 16:35:32 +03:00
|
|
|
import Harness.GraphqlEngine qualified as GraphqlEngine
|
|
|
|
import Harness.Quoter.Graphql (graphql)
|
|
|
|
import Harness.Quoter.Yaml (shouldBeYaml, shouldReturnYaml, yaml)
|
2022-03-17 23:53:56 +03:00
|
|
|
import Harness.RemoteServer qualified as RemoteServer
|
2022-02-21 20:05:09 +03:00
|
|
|
import Harness.Test.Context (Context (..))
|
|
|
|
import Harness.Test.Context qualified as Context
|
2022-06-17 11:44:04 +03:00
|
|
|
import Harness.Test.Schema (Table (..))
|
2022-03-10 14:18:13 +03:00
|
|
|
import Harness.Test.Schema qualified as Schema
|
2022-04-20 20:15:42 +03:00
|
|
|
import Harness.TestEnvironment (Server, TestEnvironment, stopServer)
|
2022-08-03 17:18:43 +03:00
|
|
|
import Hasura.Prelude
|
2022-02-18 16:35:32 +03:00
|
|
|
import Test.Hspec (SpecWith, describe, it)
|
|
|
|
|
2022-08-03 17:18:43 +03:00
|
|
|
-------------------------------------------------------------------------------
|
2022-02-18 16:35:32 +03:00
|
|
|
-- Preamble
|
|
|
|
|
2022-04-20 20:15:42 +03:00
|
|
|
spec :: SpecWith TestEnvironment
|
|
|
|
spec = Context.runWithLocalTestEnvironment contexts tests
|
2022-02-18 16:35:32 +03:00
|
|
|
where
|
2022-04-07 18:45:59 +03:00
|
|
|
lhsContexts = [lhsPostgres, lhsSQLServer, lhsRemoteServer]
|
2022-04-06 10:18:59 +03:00
|
|
|
rhsContexts = [rhsPostgres, rhsSQLServer]
|
2022-08-03 17:18:43 +03:00
|
|
|
contexts = NE.fromList $ combine <$> lhsContexts <*> rhsContexts
|
2022-02-18 16:35:32 +03:00
|
|
|
|
|
|
|
-- | Combines a lhs and a rhs.
|
|
|
|
--
|
|
|
|
-- The rhs is set up first, then the lhs can create the remote relationship.
|
|
|
|
--
|
|
|
|
-- Teardown is done in the opposite order.
|
|
|
|
--
|
|
|
|
-- The metadata is cleared befored each setup.
|
|
|
|
combine :: LHSContext -> RHSContext -> Context (Maybe Server)
|
|
|
|
combine lhs (tableName, rhs) =
|
|
|
|
Context
|
2022-02-21 20:05:09 +03:00
|
|
|
{ name = Context.Combine lhsName rhsName,
|
2022-04-20 20:15:42 +03:00
|
|
|
mkLocalTestEnvironment = lhsMkLocalTestEnvironment,
|
|
|
|
setup = \(testEnvironment, localTestEnvironment) -> do
|
|
|
|
GraphqlEngine.clearMetadata testEnvironment
|
|
|
|
rhsSetup (testEnvironment, ())
|
|
|
|
lhsSetup (testEnvironment, localTestEnvironment),
|
|
|
|
teardown = \testEnvironment@(globalTestEnvironment, _) -> do
|
|
|
|
lhsTeardown testEnvironment
|
|
|
|
rhsTeardown (globalTestEnvironment, ())
|
|
|
|
GraphqlEngine.clearMetadata globalTestEnvironment,
|
2022-02-18 16:35:32 +03:00
|
|
|
customOptions =
|
2022-02-21 20:05:09 +03:00
|
|
|
Context.combineOptions lhsOptions rhsOptions
|
2022-02-18 16:35:32 +03:00
|
|
|
}
|
|
|
|
where
|
2022-02-22 14:54:43 +03:00
|
|
|
Context
|
|
|
|
{ name = lhsName,
|
2022-04-20 20:15:42 +03:00
|
|
|
mkLocalTestEnvironment = lhsMkLocalTestEnvironment,
|
2022-02-22 14:54:43 +03:00
|
|
|
setup = lhsSetup,
|
|
|
|
teardown = lhsTeardown,
|
|
|
|
customOptions = lhsOptions
|
|
|
|
} = lhs tableName
|
|
|
|
Context
|
|
|
|
{ name = rhsName,
|
|
|
|
setup = rhsSetup,
|
|
|
|
teardown = rhsTeardown,
|
|
|
|
customOptions = rhsOptions
|
|
|
|
} = rhs
|
2022-02-18 16:35:32 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- | LHS context.
|
|
|
|
--
|
|
|
|
-- Each LHS context is responsible for setting up the remote relationship, and
|
|
|
|
-- for tearing it down. Each lhs context is given the JSON representation for
|
|
|
|
-- the table name on the RHS.
|
|
|
|
type LHSContext = Value -> Context (Maybe Server)
|
|
|
|
|
|
|
|
lhsPostgres :: LHSContext
|
|
|
|
lhsPostgres tableName =
|
|
|
|
Context
|
2022-03-15 19:08:47 +03:00
|
|
|
{ name = Context.Backend Context.Postgres,
|
2022-04-20 20:15:42 +03:00
|
|
|
mkLocalTestEnvironment = lhsPostgresMkLocalTestEnvironment,
|
2022-02-18 16:35:32 +03:00
|
|
|
setup = lhsPostgresSetup tableName,
|
|
|
|
teardown = lhsPostgresTeardown,
|
|
|
|
customOptions = Nothing
|
|
|
|
}
|
|
|
|
|
2022-04-07 18:45:59 +03:00
|
|
|
lhsSQLServer :: LHSContext
|
|
|
|
lhsSQLServer tableName =
|
|
|
|
Context
|
|
|
|
{ name = Context.Backend Context.SQLServer,
|
2022-04-20 20:15:42 +03:00
|
|
|
mkLocalTestEnvironment = lhsSQLServerMkLocalTestEnvironment,
|
2022-04-07 18:45:59 +03:00
|
|
|
setup = lhsSQLServerSetup tableName,
|
|
|
|
teardown = lhsSQLServerTeardown,
|
|
|
|
customOptions = Nothing
|
|
|
|
}
|
|
|
|
|
2022-03-17 23:53:56 +03:00
|
|
|
lhsRemoteServer :: LHSContext
|
|
|
|
lhsRemoteServer tableName =
|
|
|
|
Context
|
|
|
|
{ name = Context.RemoteGraphQLServer,
|
2022-04-20 20:15:42 +03:00
|
|
|
mkLocalTestEnvironment = lhsRemoteServerMkLocalTestEnvironment,
|
2022-03-17 23:53:56 +03:00
|
|
|
setup = lhsRemoteServerSetup tableName,
|
|
|
|
teardown = lhsRemoteServerTeardown,
|
|
|
|
customOptions = Nothing
|
|
|
|
}
|
2022-02-18 16:35:32 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- | RHS context
|
|
|
|
--
|
|
|
|
-- Each RHS context is responsible for setting up the target table, and for
|
|
|
|
-- returning the JSON representation of said table.
|
|
|
|
type RHSContext = (Value, Context ())
|
|
|
|
|
|
|
|
rhsPostgres :: RHSContext
|
|
|
|
rhsPostgres =
|
|
|
|
let table =
|
|
|
|
[yaml|
|
|
|
|
schema: hasura
|
|
|
|
name: album
|
|
|
|
|]
|
|
|
|
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-02-18 16:35:32 +03:00
|
|
|
setup = rhsPostgresSetup,
|
|
|
|
teardown = rhsPostgresTeardown,
|
|
|
|
customOptions = Nothing
|
|
|
|
}
|
|
|
|
in (table, context)
|
|
|
|
|
2022-04-06 10:18:59 +03:00
|
|
|
rhsSQLServer :: RHSContext
|
|
|
|
rhsSQLServer =
|
|
|
|
let table =
|
|
|
|
[yaml|
|
|
|
|
schema: hasura
|
|
|
|
name: album
|
|
|
|
|]
|
|
|
|
context =
|
|
|
|
Context
|
|
|
|
{ name = Context.Backend Context.SQLServer,
|
2022-04-20 20:15:42 +03:00
|
|
|
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
|
2022-04-06 10:18:59 +03:00
|
|
|
setup = rhsSQLServerSetup,
|
|
|
|
teardown = rhsSQLServerTeardown,
|
|
|
|
customOptions = Nothing
|
|
|
|
}
|
|
|
|
in (table, context)
|
2022-02-18 16:35:32 +03:00
|
|
|
|
2022-03-10 14:18:13 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Schema
|
|
|
|
|
|
|
|
-- | LHS
|
|
|
|
artist :: Schema.Table
|
|
|
|
artist =
|
2022-06-17 11:44:04 +03:00
|
|
|
(Schema.table "artist")
|
|
|
|
{ tableColumns =
|
|
|
|
[ Schema.columnNull "id" Schema.TInt,
|
|
|
|
Schema.column "name" Schema.TStr
|
|
|
|
],
|
|
|
|
tableData =
|
|
|
|
[ [Schema.VInt 1, Schema.VStr "artist1"],
|
|
|
|
[Schema.VInt 2, Schema.VStr "artist2"],
|
|
|
|
[Schema.VInt 3, Schema.VStr "artist3_no_albums"],
|
|
|
|
[Schema.VNull, Schema.VStr "artist4_no_id"]
|
|
|
|
]
|
|
|
|
}
|
2022-03-10 14:18:13 +03:00
|
|
|
|
|
|
|
-- | RHS
|
|
|
|
album :: Schema.Table
|
|
|
|
album =
|
2022-06-17 11:44:04 +03:00
|
|
|
(Schema.table "album")
|
|
|
|
{ tableColumns =
|
|
|
|
[ Schema.column "id" Schema.TInt,
|
|
|
|
Schema.column "title" Schema.TStr,
|
|
|
|
Schema.columnNull "artist_id" Schema.TInt
|
|
|
|
],
|
|
|
|
tablePrimaryKey = ["id"],
|
|
|
|
tableData =
|
|
|
|
[ [Schema.VInt 1, Schema.VStr "album1_artist1", Schema.VInt 1],
|
|
|
|
[Schema.VInt 2, Schema.VStr "album2_artist1", Schema.VInt 1],
|
|
|
|
[Schema.VInt 3, Schema.VStr "album3_artist1", Schema.VInt 1],
|
|
|
|
[Schema.VInt 4, Schema.VStr "album4_artist2", Schema.VInt 2]
|
|
|
|
]
|
|
|
|
}
|
2022-03-10 14:18:13 +03:00
|
|
|
|
2022-02-18 16:35:32 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- LHS Postgres
|
|
|
|
|
2022-04-20 20:15:42 +03:00
|
|
|
lhsPostgresMkLocalTestEnvironment :: TestEnvironment -> IO (Maybe Server)
|
|
|
|
lhsPostgresMkLocalTestEnvironment _ = pure Nothing
|
2022-02-21 20:05:09 +03:00
|
|
|
|
2022-04-20 20:15:42 +03:00
|
|
|
lhsPostgresSetup :: Value -> (TestEnvironment, Maybe Server) -> IO ()
|
|
|
|
lhsPostgresSetup rhsTableName (testEnvironment, _) = do
|
2022-03-10 14:18:13 +03:00
|
|
|
let sourceName = "source"
|
2022-02-18 16:35:32 +03:00
|
|
|
sourceConfig = Postgres.defaultSourceConfiguration
|
2022-03-15 19:08:47 +03:00
|
|
|
schemaName = Context.defaultSchema Context.Postgres
|
2022-03-10 14:18:13 +03:00
|
|
|
-- Add remote source
|
|
|
|
GraphqlEngine.postMetadata_
|
2022-04-20 20:15:42 +03:00
|
|
|
testEnvironment
|
2022-03-10 14:18:13 +03:00
|
|
|
[yaml|
|
|
|
|
type: pg_add_source
|
|
|
|
args:
|
|
|
|
name: *sourceName
|
|
|
|
configuration: *sourceConfig
|
|
|
|
|]
|
|
|
|
-- setup tables only
|
|
|
|
Postgres.createTable artist
|
|
|
|
Postgres.insertTable artist
|
2022-04-20 20:15:42 +03:00
|
|
|
Schema.trackTable Context.Postgres sourceName artist testEnvironment
|
2022-03-10 14:18:13 +03:00
|
|
|
|
2022-02-18 16:35:32 +03:00
|
|
|
GraphqlEngine.postMetadata_
|
2022-04-20 20:15:42 +03:00
|
|
|
testEnvironment
|
2022-02-18 16:35:32 +03:00
|
|
|
[yaml|
|
|
|
|
type: bulk
|
|
|
|
args:
|
|
|
|
- type: pg_create_select_permission
|
|
|
|
args:
|
2022-03-10 14:18:13 +03:00
|
|
|
source: *sourceName
|
2022-02-18 16:35:32 +03:00
|
|
|
role: role1
|
2022-03-10 14:18:13 +03:00
|
|
|
table:
|
|
|
|
schema: *schemaName
|
|
|
|
name: artist
|
2022-02-18 16:35:32 +03:00
|
|
|
permission:
|
|
|
|
columns: '*'
|
|
|
|
filter: {}
|
|
|
|
- type: pg_create_select_permission
|
|
|
|
args:
|
2022-03-10 14:18:13 +03:00
|
|
|
source: *sourceName
|
2022-02-18 16:35:32 +03:00
|
|
|
role: role2
|
2022-03-10 14:18:13 +03:00
|
|
|
table:
|
|
|
|
schema: *schemaName
|
|
|
|
name: artist
|
2022-02-18 16:35:32 +03:00
|
|
|
permission:
|
|
|
|
columns: '*'
|
|
|
|
filter: {}
|
|
|
|
- type: pg_create_remote_relationship
|
|
|
|
args:
|
2022-03-10 14:18:13 +03:00
|
|
|
source: *sourceName
|
|
|
|
table:
|
|
|
|
schema: *schemaName
|
|
|
|
name: artist
|
2022-02-18 16:35:32 +03:00
|
|
|
name: albums
|
|
|
|
definition:
|
|
|
|
to_source:
|
|
|
|
source: target
|
|
|
|
table: *rhsTableName
|
|
|
|
relationship_type: array
|
|
|
|
field_mapping:
|
|
|
|
id: artist_id
|
|
|
|
|]
|
|
|
|
|
2022-04-20 20:15:42 +03:00
|
|
|
lhsPostgresTeardown :: (TestEnvironment, Maybe Server) -> IO ()
|
2022-03-17 23:53:56 +03:00
|
|
|
lhsPostgresTeardown _ = Postgres.dropTable artist
|
|
|
|
|
2022-04-07 18:45:59 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- LHS SQLServer
|
|
|
|
|
2022-04-20 20:15:42 +03:00
|
|
|
lhsSQLServerMkLocalTestEnvironment :: TestEnvironment -> IO (Maybe Server)
|
|
|
|
lhsSQLServerMkLocalTestEnvironment _ = pure Nothing
|
2022-04-07 18:45:59 +03:00
|
|
|
|
2022-04-20 20:15:42 +03:00
|
|
|
lhsSQLServerSetup :: Value -> (TestEnvironment, Maybe Server) -> IO ()
|
|
|
|
lhsSQLServerSetup rhsTableName (testEnvironment, _) = do
|
2022-04-07 18:45:59 +03:00
|
|
|
let sourceName = "source"
|
|
|
|
sourceConfig = SQLServer.defaultSourceConfiguration
|
|
|
|
schemaName = Context.defaultSchema Context.SQLServer
|
|
|
|
-- Add remote source
|
|
|
|
GraphqlEngine.postMetadata_
|
2022-04-20 20:15:42 +03:00
|
|
|
testEnvironment
|
2022-04-07 18:45:59 +03:00
|
|
|
[yaml|
|
|
|
|
type: mssql_add_source
|
|
|
|
args:
|
|
|
|
name: *sourceName
|
|
|
|
configuration: *sourceConfig
|
|
|
|
|]
|
|
|
|
-- setup tables only
|
|
|
|
SQLServer.createTable artist
|
|
|
|
SQLServer.insertTable artist
|
2022-04-20 20:15:42 +03:00
|
|
|
Schema.trackTable Context.SQLServer sourceName artist testEnvironment
|
2022-04-07 18:45:59 +03:00
|
|
|
|
|
|
|
GraphqlEngine.postMetadata_
|
2022-04-20 20:15:42 +03:00
|
|
|
testEnvironment
|
2022-04-07 18:45:59 +03:00
|
|
|
[yaml|
|
|
|
|
type: bulk
|
|
|
|
args:
|
|
|
|
- type: mssql_create_select_permission
|
|
|
|
args:
|
|
|
|
source: *sourceName
|
|
|
|
role: role1
|
|
|
|
table:
|
|
|
|
schema: *schemaName
|
|
|
|
name: artist
|
|
|
|
permission:
|
|
|
|
columns: '*'
|
|
|
|
filter: {}
|
|
|
|
- type: mssql_create_select_permission
|
|
|
|
args:
|
|
|
|
source: *sourceName
|
|
|
|
role: role2
|
|
|
|
table:
|
|
|
|
schema: *schemaName
|
|
|
|
name: artist
|
|
|
|
permission:
|
|
|
|
columns: '*'
|
|
|
|
filter: {}
|
|
|
|
- type: mssql_create_remote_relationship
|
|
|
|
args:
|
|
|
|
source: *sourceName
|
|
|
|
table:
|
|
|
|
schema: *schemaName
|
|
|
|
name: artist
|
|
|
|
name: albums
|
|
|
|
definition:
|
|
|
|
to_source:
|
|
|
|
source: target
|
|
|
|
table: *rhsTableName
|
|
|
|
relationship_type: array
|
|
|
|
field_mapping:
|
|
|
|
id: artist_id
|
|
|
|
|]
|
|
|
|
|
2022-04-20 20:15:42 +03:00
|
|
|
lhsSQLServerTeardown :: (TestEnvironment, Maybe Server) -> IO ()
|
2022-04-07 18:45:59 +03:00
|
|
|
lhsSQLServerTeardown _ = SQLServer.dropTable artist
|
|
|
|
|
2022-03-17 23:53:56 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- LHS Remote Server
|
|
|
|
|
|
|
|
-- | To circumvent Morpheus' default behaviour, which is to capitalize type
|
|
|
|
-- names and field names for Haskell records to be consistent with their
|
|
|
|
-- corresponding GraphQL equivalents, we define most of the schema manually with
|
|
|
|
-- the following options.
|
|
|
|
hasuraTypeOptions :: Morpheus.GQLTypeOptions
|
|
|
|
hasuraTypeOptions =
|
|
|
|
Morpheus.defaultTypeOptions
|
|
|
|
{ -- transformation to apply to constructors, for enums; we simply map to
|
|
|
|
-- lower case:
|
|
|
|
-- Asc -> asc
|
|
|
|
Morpheus.constructorTagModifier = map toLower,
|
|
|
|
-- transformation to apply to field names; we drop all characters up to and
|
|
|
|
-- including the first underscore:
|
|
|
|
-- hta_where -> where
|
|
|
|
Morpheus.fieldLabelModifier = tail . dropWhile (/= '_'),
|
|
|
|
-- transformation to apply to type names; we split the name on uppercase
|
|
|
|
-- letters, intercalate with underscore, and map everything to lowercase:
|
|
|
|
-- HasuraTrack -> hasura_track
|
|
|
|
Morpheus.typeNameModifier = \_ ->
|
|
|
|
map toLower
|
|
|
|
. intercalate "_"
|
|
|
|
. split (dropBlanks $ keepDelimsL $ whenElt isUpper)
|
|
|
|
}
|
|
|
|
|
|
|
|
data Query m = Query
|
|
|
|
{ hasura_artist :: HasuraArtistArgs -> m [HasuraArtist m]
|
|
|
|
}
|
|
|
|
deriving (Generic)
|
|
|
|
|
|
|
|
instance Typeable m => Morpheus.GQLType (Query m)
|
|
|
|
|
|
|
|
data HasuraArtistArgs = HasuraArtistArgs
|
|
|
|
{ aa_where :: Maybe HasuraArtistBoolExp,
|
|
|
|
aa_order_by :: Maybe [HasuraArtistOrderBy],
|
|
|
|
aa_limit :: Maybe Int
|
|
|
|
}
|
|
|
|
deriving (Generic)
|
|
|
|
|
|
|
|
instance Morpheus.GQLType HasuraArtistArgs where
|
|
|
|
typeOptions _ _ = hasuraTypeOptions
|
|
|
|
|
|
|
|
data HasuraArtist m = HasuraArtist
|
|
|
|
{ a_id :: m (Maybe Int),
|
|
|
|
a_name :: m (Maybe Text)
|
|
|
|
}
|
|
|
|
deriving (Generic)
|
|
|
|
|
|
|
|
instance Typeable m => Morpheus.GQLType (HasuraArtist m) where
|
|
|
|
typeOptions _ _ = hasuraTypeOptions
|
|
|
|
|
|
|
|
data HasuraArtistOrderBy = HasuraArtistOrderBy
|
|
|
|
{ aob_id :: Maybe OrderType,
|
|
|
|
aob_name :: Maybe OrderType
|
|
|
|
}
|
|
|
|
deriving (Generic)
|
|
|
|
|
|
|
|
instance Morpheus.GQLType HasuraArtistOrderBy where
|
|
|
|
typeOptions _ _ = hasuraTypeOptions
|
|
|
|
|
|
|
|
data HasuraArtistBoolExp = HasuraArtistBoolExp
|
|
|
|
{ abe__and :: Maybe [HasuraArtistBoolExp],
|
|
|
|
abe__or :: Maybe [HasuraArtistBoolExp],
|
|
|
|
abe__not :: Maybe HasuraArtistBoolExp,
|
|
|
|
abe_id :: Maybe IntCompExp,
|
|
|
|
abe_name :: Maybe StringCompExp
|
|
|
|
}
|
|
|
|
deriving (Generic)
|
|
|
|
|
|
|
|
instance Morpheus.GQLType HasuraArtistBoolExp where
|
|
|
|
typeOptions _ _ = hasuraTypeOptions
|
|
|
|
|
|
|
|
data OrderType = Asc | Desc
|
|
|
|
deriving (Show, Generic)
|
|
|
|
|
|
|
|
instance Morpheus.GQLType OrderType where
|
|
|
|
typeOptions _ _ = hasuraTypeOptions
|
|
|
|
|
|
|
|
[gqlDocument|
|
|
|
|
|
|
|
|
input IntCompExp {
|
|
|
|
_eq: Int
|
|
|
|
}
|
|
|
|
|
|
|
|
input StringCompExp {
|
|
|
|
_eq: String
|
|
|
|
}
|
|
|
|
|
|
|
|
|]
|
|
|
|
|
2022-04-20 20:15:42 +03:00
|
|
|
lhsRemoteServerMkLocalTestEnvironment :: TestEnvironment -> IO (Maybe Server)
|
|
|
|
lhsRemoteServerMkLocalTestEnvironment _ = do
|
2022-03-17 23:53:56 +03:00
|
|
|
server <-
|
|
|
|
RemoteServer.run $
|
|
|
|
RemoteServer.generateQueryInterpreter (Query {hasura_artist})
|
|
|
|
pure $ Just server
|
|
|
|
where
|
|
|
|
-- Implements the @hasura_artist@ field of the @Query@ type.
|
|
|
|
hasura_artist (HasuraArtistArgs {..}) = do
|
|
|
|
let filterFunction = case aa_where of
|
|
|
|
Nothing -> const True
|
|
|
|
Just whereArg -> flip matchArtist whereArg
|
|
|
|
orderByFunction = case aa_order_by of
|
|
|
|
Nothing -> \_ _ -> EQ
|
|
|
|
Just orderByArg -> orderArtist orderByArg
|
2022-07-01 13:49:31 +03:00
|
|
|
limitFunction = maybe id take aa_limit
|
2022-03-17 23:53:56 +03:00
|
|
|
pure $
|
|
|
|
artists
|
|
|
|
& filter filterFunction
|
|
|
|
& sortBy orderByFunction
|
|
|
|
& limitFunction
|
|
|
|
& map mkArtist
|
|
|
|
-- Returns True iif the given artist matches the given boolean expression.
|
|
|
|
matchArtist artistInfo@(artistId, artistName) (HasuraArtistBoolExp {..}) =
|
|
|
|
and
|
|
|
|
[ maybe True (all (matchArtist artistInfo)) abe__and,
|
|
|
|
maybe True (any (matchArtist artistInfo)) abe__or,
|
|
|
|
maybe True (not . matchArtist artistInfo) abe__not,
|
|
|
|
maybe True (matchMaybeInt artistId) abe_id,
|
|
|
|
maybe True (matchString artistName) abe_name
|
|
|
|
]
|
|
|
|
matchString stringField StringCompExp {..} = Just stringField == _eq
|
|
|
|
matchMaybeInt maybeIntField IntCompExp {..} = maybeIntField == _eq
|
|
|
|
-- Returns an ordering between the two given artists.
|
|
|
|
orderArtist
|
|
|
|
orderByList
|
|
|
|
(artistId1, artistName1)
|
|
|
|
(artistId2, artistName2) =
|
|
|
|
flip foldMap orderByList \HasuraArtistOrderBy {..} ->
|
|
|
|
if
|
|
|
|
| Just idOrder <- aob_id ->
|
|
|
|
compareWithNullLast idOrder artistId1 artistId2
|
|
|
|
| Just nameOrder <- aob_name -> case nameOrder of
|
|
|
|
Asc -> compare artistName1 artistName2
|
|
|
|
Desc -> compare artistName2 artistName1
|
|
|
|
| otherwise ->
|
|
|
|
error "empty artist_order object"
|
|
|
|
compareWithNullLast Desc x1 x2 = compareWithNullLast Asc x2 x1
|
|
|
|
compareWithNullLast Asc Nothing Nothing = EQ
|
|
|
|
compareWithNullLast Asc (Just _) Nothing = LT
|
|
|
|
compareWithNullLast Asc Nothing (Just _) = GT
|
|
|
|
compareWithNullLast Asc (Just x1) (Just x2) = compare x1 x2
|
|
|
|
artists =
|
|
|
|
[ (Just 1, "artist1"),
|
|
|
|
(Just 2, "artist2"),
|
2022-04-07 18:45:59 +03:00
|
|
|
(Just 3, "artist3_no_albums"),
|
|
|
|
(Nothing, "artist4_no_id")
|
2022-03-17 23:53:56 +03:00
|
|
|
]
|
|
|
|
mkArtist (artistId, artistName) =
|
|
|
|
HasuraArtist
|
|
|
|
{ a_id = pure artistId,
|
|
|
|
a_name = pure $ Just artistName
|
|
|
|
}
|
|
|
|
|
2022-04-20 20:15:42 +03:00
|
|
|
lhsRemoteServerSetup :: Value -> (TestEnvironment, Maybe Server) -> IO ()
|
|
|
|
lhsRemoteServerSetup tableName (testEnvironment, maybeRemoteServer) = case maybeRemoteServer of
|
|
|
|
Nothing -> error "XToDBArrayRelationshipSpec: remote server local testEnvironment did not succesfully create a server"
|
2022-03-17 23:53:56 +03:00
|
|
|
Just remoteServer -> do
|
|
|
|
let remoteSchemaEndpoint = GraphqlEngine.serverUrl remoteServer ++ "/graphql"
|
|
|
|
GraphqlEngine.postMetadata_
|
2022-04-20 20:15:42 +03:00
|
|
|
testEnvironment
|
2022-03-17 23:53:56 +03:00
|
|
|
[yaml|
|
|
|
|
type: bulk
|
|
|
|
args:
|
|
|
|
- type: add_remote_schema
|
|
|
|
args:
|
|
|
|
name: source
|
|
|
|
definition:
|
|
|
|
url: *remoteSchemaEndpoint
|
|
|
|
- type: create_remote_schema_remote_relationship
|
|
|
|
args:
|
|
|
|
remote_schema: source
|
|
|
|
type_name: hasura_artist
|
|
|
|
name: albums
|
|
|
|
definition:
|
|
|
|
to_source:
|
|
|
|
source: target
|
|
|
|
table: *tableName
|
|
|
|
relationship_type: array
|
|
|
|
field_mapping:
|
|
|
|
id: artist_id
|
|
|
|
|]
|
|
|
|
|
2022-04-20 20:15:42 +03:00
|
|
|
lhsRemoteServerTeardown :: (TestEnvironment, Maybe Server) -> IO ()
|
2022-03-17 23:53:56 +03:00
|
|
|
lhsRemoteServerTeardown (_, maybeServer) = traverse_ stopServer maybeServer
|
2022-02-18 16:35:32 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- RHS Postgres
|
|
|
|
|
2022-04-20 20:15:42 +03:00
|
|
|
rhsPostgresSetup :: (TestEnvironment, ()) -> IO ()
|
|
|
|
rhsPostgresSetup (testEnvironment, _) = do
|
2022-03-10 14:18:13 +03:00
|
|
|
let sourceName = "target"
|
2022-02-18 16:35:32 +03:00
|
|
|
sourceConfig = Postgres.defaultSourceConfiguration
|
2022-03-15 19:08:47 +03:00
|
|
|
schemaName = Context.defaultSchema Context.Postgres
|
2022-03-10 14:18:13 +03:00
|
|
|
-- Add remote source
|
|
|
|
GraphqlEngine.postMetadata_
|
2022-04-20 20:15:42 +03:00
|
|
|
testEnvironment
|
2022-03-10 14:18:13 +03:00
|
|
|
[yaml|
|
|
|
|
type: pg_add_source
|
|
|
|
args:
|
|
|
|
name: *sourceName
|
|
|
|
configuration: *sourceConfig
|
|
|
|
|]
|
|
|
|
-- setup tables only
|
|
|
|
Postgres.createTable album
|
|
|
|
Postgres.insertTable album
|
2022-04-20 20:15:42 +03:00
|
|
|
Schema.trackTable Context.Postgres sourceName album testEnvironment
|
2022-03-10 14:18:13 +03:00
|
|
|
|
2022-02-18 16:35:32 +03:00
|
|
|
GraphqlEngine.postMetadata_
|
2022-04-20 20:15:42 +03:00
|
|
|
testEnvironment
|
2022-02-18 16:35:32 +03:00
|
|
|
[yaml|
|
|
|
|
type: bulk
|
|
|
|
args:
|
|
|
|
- type: pg_create_select_permission
|
|
|
|
args:
|
2022-03-10 14:18:13 +03:00
|
|
|
source: *sourceName
|
2022-02-18 16:35:32 +03:00
|
|
|
role: role1
|
2022-03-10 14:18:13 +03:00
|
|
|
table:
|
|
|
|
schema: *schemaName
|
|
|
|
name: album
|
2022-02-18 16:35:32 +03:00
|
|
|
permission:
|
|
|
|
columns:
|
|
|
|
- title
|
|
|
|
- artist_id
|
|
|
|
filter:
|
|
|
|
artist_id:
|
|
|
|
_eq: x-hasura-artist-id
|
|
|
|
- type: pg_create_select_permission
|
|
|
|
args:
|
2022-03-10 14:18:13 +03:00
|
|
|
source: *sourceName
|
2022-02-18 16:35:32 +03:00
|
|
|
role: role2
|
2022-03-10 14:18:13 +03:00
|
|
|
table:
|
|
|
|
schema: *schemaName
|
|
|
|
name: album
|
2022-02-18 16:35:32 +03:00
|
|
|
permission:
|
|
|
|
columns: [id, title, artist_id]
|
|
|
|
filter:
|
|
|
|
artist_id:
|
|
|
|
_eq: x-hasura-artist-id
|
2022-04-06 10:18:59 +03:00
|
|
|
limit: 2
|
2022-02-18 16:35:32 +03:00
|
|
|
allow_aggregations: true
|
|
|
|
|]
|
|
|
|
|
2022-04-20 20:15:42 +03:00
|
|
|
rhsPostgresTeardown :: (TestEnvironment, ()) -> IO ()
|
2022-03-17 23:53:56 +03:00
|
|
|
rhsPostgresTeardown _ = Postgres.dropTable album
|
2022-02-18 16:35:32 +03:00
|
|
|
|
2022-04-06 10:18:59 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- RHS SQLServer
|
|
|
|
|
2022-04-20 20:15:42 +03:00
|
|
|
rhsSQLServerSetup :: (TestEnvironment, ()) -> IO ()
|
|
|
|
rhsSQLServerSetup (testEnvironment, _) = do
|
2022-04-06 10:18:59 +03:00
|
|
|
let sourceName = "target"
|
|
|
|
sourceConfig = SQLServer.defaultSourceConfiguration
|
|
|
|
schemaName = Context.defaultSchema Context.SQLServer
|
|
|
|
-- Add remote source
|
|
|
|
GraphqlEngine.postMetadata_
|
2022-04-20 20:15:42 +03:00
|
|
|
testEnvironment
|
2022-04-06 10:18:59 +03:00
|
|
|
[yaml|
|
|
|
|
type: mssql_add_source
|
|
|
|
args:
|
|
|
|
name: *sourceName
|
|
|
|
configuration: *sourceConfig
|
|
|
|
|]
|
|
|
|
-- setup tables only
|
|
|
|
SQLServer.createTable album
|
|
|
|
SQLServer.insertTable album
|
2022-04-20 20:15:42 +03:00
|
|
|
Schema.trackTable Context.SQLServer sourceName album testEnvironment
|
2022-04-06 10:18:59 +03:00
|
|
|
|
|
|
|
GraphqlEngine.postMetadata_
|
2022-04-20 20:15:42 +03:00
|
|
|
testEnvironment
|
2022-04-06 10:18:59 +03:00
|
|
|
[yaml|
|
|
|
|
type: bulk
|
|
|
|
args:
|
|
|
|
- type: mssql_create_select_permission
|
|
|
|
args:
|
|
|
|
source: *sourceName
|
|
|
|
role: role1
|
|
|
|
table:
|
|
|
|
schema: *schemaName
|
|
|
|
name: album
|
|
|
|
permission:
|
|
|
|
columns:
|
|
|
|
- title
|
|
|
|
- artist_id
|
|
|
|
filter:
|
|
|
|
artist_id:
|
|
|
|
_eq: x-hasura-artist-id
|
|
|
|
- type: mssql_create_select_permission
|
|
|
|
args:
|
|
|
|
source: *sourceName
|
|
|
|
role: role2
|
|
|
|
table:
|
|
|
|
schema: *schemaName
|
|
|
|
name: album
|
|
|
|
permission:
|
|
|
|
columns: [id, title, artist_id]
|
|
|
|
filter:
|
|
|
|
artist_id:
|
|
|
|
_eq: x-hasura-artist-id
|
|
|
|
limit: 2
|
|
|
|
allow_aggregations: true
|
|
|
|
|]
|
|
|
|
|
2022-04-20 20:15:42 +03:00
|
|
|
rhsSQLServerTeardown :: (TestEnvironment, ()) -> IO ()
|
2022-04-06 10:18:59 +03:00
|
|
|
rhsSQLServerTeardown _ = SQLServer.dropTable album
|
|
|
|
|
2022-02-18 16:35:32 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Tests
|
|
|
|
|
2022-04-20 20:15:42 +03:00
|
|
|
tests :: Context.Options -> SpecWith (TestEnvironment, Maybe Server)
|
2022-03-17 23:53:56 +03:00
|
|
|
tests opts = describe "array-relationship" do
|
2022-02-18 16:35:32 +03:00
|
|
|
schemaTests opts
|
|
|
|
executionTests opts
|
|
|
|
permissionTests opts
|
|
|
|
|
2022-04-20 20:15:42 +03:00
|
|
|
schemaTests :: Context.Options -> SpecWith (TestEnvironment, Maybe Server)
|
2022-02-18 16:35:32 +03:00
|
|
|
schemaTests _opts =
|
|
|
|
-- we introspect the schema and validate it
|
2022-04-20 20:15:42 +03:00
|
|
|
it "graphql-schema" \(testEnvironment, _) -> do
|
2022-02-18 16:35:32 +03:00
|
|
|
let query =
|
|
|
|
[graphql|
|
|
|
|
fragment type_info on __Type {
|
|
|
|
name
|
|
|
|
kind
|
|
|
|
ofType {
|
|
|
|
name
|
|
|
|
kind
|
|
|
|
ofType {
|
|
|
|
name
|
|
|
|
kind
|
|
|
|
ofType {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
query {
|
|
|
|
artist_fields: __type(name: "hasura_artist") {
|
|
|
|
fields {
|
|
|
|
name
|
|
|
|
type {
|
|
|
|
...type_info
|
|
|
|
}
|
|
|
|
args {
|
|
|
|
name
|
|
|
|
type {
|
|
|
|
...type_info
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
2022-04-20 20:15:42 +03:00
|
|
|
introspectionResult <- GraphqlEngine.postGraphql testEnvironment query
|
2022-02-18 16:35:32 +03:00
|
|
|
let focusArtistFields =
|
|
|
|
key "data"
|
|
|
|
. key "artist_fields"
|
|
|
|
. key "fields"
|
|
|
|
. values
|
|
|
|
albumsField =
|
|
|
|
Unsafe.fromJust $
|
|
|
|
findOf
|
|
|
|
focusArtistFields
|
|
|
|
(has $ key "name" . _String . only "albums")
|
|
|
|
introspectionResult
|
|
|
|
albumsAggregateField =
|
|
|
|
Unsafe.fromJust $
|
|
|
|
findOf
|
|
|
|
focusArtistFields
|
|
|
|
(has $ key "name" . _String . only "albums_aggregate")
|
|
|
|
introspectionResult
|
|
|
|
|
|
|
|
-- check the return type of albums field
|
|
|
|
shouldBeYaml
|
|
|
|
(albumsField ^?! key "type")
|
|
|
|
[yaml|
|
|
|
|
kind: NON_NULL
|
|
|
|
name: null
|
|
|
|
ofType:
|
|
|
|
kind: LIST
|
|
|
|
name: null
|
|
|
|
ofType:
|
|
|
|
kind: NON_NULL
|
|
|
|
name: null
|
|
|
|
ofType:
|
|
|
|
name: hasura_album
|
|
|
|
|]
|
|
|
|
|
|
|
|
-- check the return type of albums_aggregate field
|
|
|
|
shouldBeYaml
|
|
|
|
(albumsAggregateField ^?! key "type")
|
|
|
|
[yaml|
|
|
|
|
name: null
|
|
|
|
kind: NON_NULL
|
|
|
|
ofType:
|
|
|
|
name: hasura_album_aggregate
|
|
|
|
kind: OBJECT
|
|
|
|
ofType: null
|
|
|
|
|]
|
|
|
|
|
|
|
|
-- | Basic queries using DB-to-DB joins
|
2022-04-20 20:15:42 +03:00
|
|
|
executionTests :: Context.Options -> SpecWith (TestEnvironment, Maybe Server)
|
2022-03-17 23:53:56 +03:00
|
|
|
executionTests opts = describe "execution" do
|
2022-02-18 16:35:32 +03:00
|
|
|
-- fetches the relationship data
|
2022-04-20 20:15:42 +03:00
|
|
|
it "related-data" \(testEnvironment, _) -> do
|
2022-02-18 16:35:32 +03:00
|
|
|
let query =
|
|
|
|
[graphql|
|
|
|
|
query {
|
|
|
|
artist: hasura_artist(where: {name: {_eq: "artist1"}}) {
|
|
|
|
name
|
|
|
|
albums {
|
|
|
|
title
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
expectedResponse =
|
|
|
|
[yaml|
|
|
|
|
data:
|
|
|
|
artist:
|
|
|
|
- name: artist1
|
|
|
|
albums:
|
|
|
|
- title: album1_artist1
|
|
|
|
- title: album2_artist1
|
2022-04-06 10:18:59 +03:00
|
|
|
- title: album3_artist1
|
2022-02-18 16:35:32 +03:00
|
|
|
|]
|
|
|
|
shouldReturnYaml
|
|
|
|
opts
|
2022-04-20 20:15:42 +03:00
|
|
|
(GraphqlEngine.postGraphql testEnvironment query)
|
2022-02-18 16:35:32 +03:00
|
|
|
expectedResponse
|
|
|
|
|
|
|
|
-- when there are no matching rows, the relationship response should be []
|
2022-04-20 20:15:42 +03:00
|
|
|
it "related-data-empty-array" \(testEnvironment, _) -> do
|
2022-02-18 16:35:32 +03:00
|
|
|
let query =
|
|
|
|
[graphql|
|
|
|
|
query {
|
2022-04-07 18:45:59 +03:00
|
|
|
artist: hasura_artist(where: {name: {_eq: "artist3_no_albums"}}) {
|
2022-02-18 16:35:32 +03:00
|
|
|
name
|
|
|
|
albums {
|
|
|
|
title
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
expectedResponse =
|
|
|
|
[yaml|
|
|
|
|
data:
|
|
|
|
artist:
|
2022-04-07 18:45:59 +03:00
|
|
|
- name: artist3_no_albums
|
2022-02-18 16:35:32 +03:00
|
|
|
albums: []
|
|
|
|
|]
|
|
|
|
shouldReturnYaml
|
|
|
|
opts
|
2022-04-20 20:15:42 +03:00
|
|
|
(GraphqlEngine.postGraphql testEnvironment query)
|
2022-02-18 16:35:32 +03:00
|
|
|
expectedResponse
|
|
|
|
|
|
|
|
-- when any of the join columns are null, the relationship should be null
|
2022-04-20 20:15:42 +03:00
|
|
|
it "related-data-null" \(testEnvironment, _) -> do
|
2022-02-18 16:35:32 +03:00
|
|
|
let query =
|
|
|
|
[graphql|
|
|
|
|
query {
|
2022-04-07 18:45:59 +03:00
|
|
|
artist: hasura_artist(where: {name: {_eq: "artist4_no_id"}}) {
|
2022-02-18 16:35:32 +03:00
|
|
|
name
|
|
|
|
albums {
|
|
|
|
title
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
expectedResponse =
|
|
|
|
[yaml|
|
|
|
|
data:
|
|
|
|
artist:
|
2022-04-07 18:45:59 +03:00
|
|
|
- name: artist4_no_id
|
2022-02-18 16:35:32 +03:00
|
|
|
albums: null
|
|
|
|
|]
|
|
|
|
shouldReturnYaml
|
|
|
|
opts
|
2022-04-20 20:15:42 +03:00
|
|
|
(GraphqlEngine.postGraphql testEnvironment query)
|
2022-02-18 16:35:32 +03:00
|
|
|
expectedResponse
|
|
|
|
|
|
|
|
-- when the lhs response has both null and non-null values for join columns
|
2022-04-20 20:15:42 +03:00
|
|
|
it "related-data-non-null-and-null" \(testEnvironment, _) -> do
|
2022-02-18 16:35:32 +03:00
|
|
|
let query =
|
|
|
|
[graphql|
|
|
|
|
query {
|
|
|
|
artist: hasura_artist(
|
|
|
|
where: {
|
|
|
|
_or: [
|
|
|
|
{name: {_eq: "artist1"}},
|
2022-04-07 18:45:59 +03:00
|
|
|
{name: {_eq: "artist4_no_id"}}
|
2022-02-18 16:35:32 +03:00
|
|
|
]
|
|
|
|
},
|
2022-04-07 18:45:59 +03:00
|
|
|
order_by: [{name: asc}]
|
2022-02-18 16:35:32 +03:00
|
|
|
) {
|
|
|
|
name
|
|
|
|
albums {
|
|
|
|
title
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
expectedResponse =
|
|
|
|
[yaml|
|
|
|
|
data:
|
|
|
|
artist:
|
|
|
|
- name: artist1
|
|
|
|
albums:
|
|
|
|
- title: album1_artist1
|
|
|
|
- title: album2_artist1
|
2022-04-06 10:18:59 +03:00
|
|
|
- title: album3_artist1
|
2022-04-07 18:45:59 +03:00
|
|
|
- name: artist4_no_id
|
2022-02-18 16:35:32 +03:00
|
|
|
albums: null
|
|
|
|
|]
|
|
|
|
shouldReturnYaml
|
|
|
|
opts
|
2022-04-20 20:15:42 +03:00
|
|
|
(GraphqlEngine.postGraphql testEnvironment query)
|
2022-02-18 16:35:32 +03:00
|
|
|
expectedResponse
|
|
|
|
|
|
|
|
-- TODO:
|
|
|
|
-- 1. where
|
|
|
|
-- 1. limit with order_by
|
|
|
|
-- 1. offset
|
|
|
|
-- 1. _aggregate
|
|
|
|
|
|
|
|
-- | tests that describe an array relationship's data in the presence of permisisons
|
2022-04-20 20:15:42 +03:00
|
|
|
permissionTests :: Context.Options -> SpecWith (TestEnvironment, Maybe Server)
|
2022-03-17 23:53:56 +03:00
|
|
|
permissionTests opts = describe "permission" do
|
2022-02-18 16:35:32 +03:00
|
|
|
-- only the allowed rows on the target table are queryable
|
2022-04-20 20:15:42 +03:00
|
|
|
it "only-allowed-rows" \(testEnvironment, _) -> do
|
2022-02-18 16:35:32 +03:00
|
|
|
let userHeaders = [("x-hasura-role", "role1"), ("x-hasura-artist-id", "1")]
|
|
|
|
query =
|
|
|
|
[graphql|
|
|
|
|
query {
|
|
|
|
artist: hasura_artist(
|
2022-04-07 18:45:59 +03:00
|
|
|
order_by: [{name: asc}]
|
2022-02-18 16:35:32 +03:00
|
|
|
) {
|
|
|
|
name
|
|
|
|
albums {
|
|
|
|
title
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
expectedResponse =
|
|
|
|
[yaml|
|
|
|
|
data:
|
|
|
|
artist:
|
|
|
|
- name: artist1
|
|
|
|
albums:
|
|
|
|
- title: album1_artist1
|
|
|
|
- title: album2_artist1
|
2022-04-06 10:18:59 +03:00
|
|
|
- title: album3_artist1
|
2022-02-18 16:35:32 +03:00
|
|
|
- name: artist2
|
|
|
|
albums: []
|
2022-04-07 18:45:59 +03:00
|
|
|
- name: artist3_no_albums
|
2022-02-18 16:35:32 +03:00
|
|
|
albums: []
|
2022-04-07 18:45:59 +03:00
|
|
|
- name: artist4_no_id
|
2022-02-18 16:35:32 +03:00
|
|
|
albums: null
|
|
|
|
|]
|
|
|
|
shouldReturnYaml
|
|
|
|
opts
|
2022-04-20 20:15:42 +03:00
|
|
|
(GraphqlEngine.postGraphqlWithHeaders testEnvironment userHeaders query)
|
2022-02-18 16:35:32 +03:00
|
|
|
expectedResponse
|
|
|
|
|
|
|
|
-- we use an introspection query to check column permissions:
|
|
|
|
-- 1. the type 'hasura_album' has only 'artist_id' and 'title', the allowed columns
|
|
|
|
-- 2. the albums field in 'hasura_artist' type is of type 'hasura_album'
|
2022-04-20 20:15:42 +03:00
|
|
|
it "only-allowed-columns" \(testEnvironment, _) -> do
|
2022-02-18 16:35:32 +03:00
|
|
|
let userHeaders = [("x-hasura-role", "role1"), ("x-hasura-artist-id", "1")]
|
|
|
|
query =
|
|
|
|
[graphql|
|
|
|
|
query {
|
|
|
|
album_fields: __type(name: "hasura_album") {
|
|
|
|
fields {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
artist: hasura_artist(
|
|
|
|
where: {name: {_eq: "artist1"}}
|
|
|
|
) {
|
|
|
|
name
|
|
|
|
albums(limit: 1) {
|
|
|
|
__typename
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
expectedResponse =
|
|
|
|
[yaml|
|
|
|
|
data:
|
|
|
|
album_fields:
|
|
|
|
fields:
|
|
|
|
- name: artist_id
|
|
|
|
- name: title
|
|
|
|
artist:
|
|
|
|
- name: artist1
|
|
|
|
albums:
|
|
|
|
- __typename: hasura_album
|
|
|
|
|]
|
|
|
|
shouldReturnYaml
|
|
|
|
opts
|
2022-04-20 20:15:42 +03:00
|
|
|
(GraphqlEngine.postGraphqlWithHeaders testEnvironment userHeaders query)
|
2022-02-18 16:35:32 +03:00
|
|
|
expectedResponse
|
|
|
|
|
|
|
|
-- _aggregate field should not be generated when 'allow_aggregations' isn't set to 'true'
|
2022-04-20 20:15:42 +03:00
|
|
|
it "aggregations-not-allowed" \(testEnvironment, _) -> do
|
2022-02-18 16:35:32 +03:00
|
|
|
let userHeaders = [("x-hasura-role", "role1")]
|
|
|
|
query =
|
|
|
|
[graphql|
|
|
|
|
query {
|
|
|
|
artist_fields: __type(name: "hasura_artist") {
|
|
|
|
fields {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
expectedResponse =
|
|
|
|
[yaml|
|
|
|
|
data:
|
|
|
|
artist_fields:
|
|
|
|
fields:
|
|
|
|
- name: albums
|
|
|
|
- name: id
|
|
|
|
- name: name
|
|
|
|
|]
|
|
|
|
shouldReturnYaml
|
|
|
|
opts
|
2022-04-20 20:15:42 +03:00
|
|
|
(GraphqlEngine.postGraphqlWithHeaders testEnvironment userHeaders query)
|
2022-02-18 16:35:32 +03:00
|
|
|
expectedResponse
|
|
|
|
|
|
|
|
-- _aggregate field should only be allowed when 'allow_aggregations' is set to 'true'
|
2022-04-20 20:15:42 +03:00
|
|
|
it "aggregations-allowed" \(testEnvironment, _) -> do
|
2022-02-18 16:35:32 +03:00
|
|
|
let userHeaders = [("x-hasura-role", "role2")]
|
|
|
|
query =
|
|
|
|
[graphql|
|
|
|
|
query {
|
|
|
|
artist_fields: __type(name: "hasura_artist") {
|
|
|
|
fields {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
expectedResponse =
|
|
|
|
[yaml|
|
|
|
|
data:
|
|
|
|
artist_fields:
|
|
|
|
fields:
|
|
|
|
- name: albums
|
|
|
|
- name: albums_aggregate
|
|
|
|
- name: id
|
|
|
|
- name: name
|
|
|
|
|]
|
|
|
|
shouldReturnYaml
|
|
|
|
opts
|
2022-04-20 20:15:42 +03:00
|
|
|
(GraphqlEngine.postGraphqlWithHeaders testEnvironment userHeaders query)
|
2022-02-18 16:35:32 +03:00
|
|
|
expectedResponse
|
|
|
|
|
|
|
|
-- permission limit should kick in when no query limit is specified
|
2022-04-20 20:15:42 +03:00
|
|
|
it "no-query-limit" \(testEnvironment, _) -> do
|
2022-02-18 16:35:32 +03:00
|
|
|
let userHeaders = [("x-hasura-role", "role2"), ("x-hasura-artist-id", "1")]
|
|
|
|
query =
|
|
|
|
[graphql|
|
|
|
|
query {
|
|
|
|
artist: hasura_artist(
|
|
|
|
where: {name: {_eq: "artist1"}}
|
|
|
|
) {
|
|
|
|
name
|
|
|
|
albums (order_by: {id: asc}){
|
|
|
|
title
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
expectedResponse =
|
|
|
|
[yaml|
|
|
|
|
data:
|
|
|
|
artist:
|
|
|
|
- name: artist1
|
|
|
|
albums:
|
|
|
|
- title: album1_artist1
|
2022-04-06 10:18:59 +03:00
|
|
|
- title: album2_artist1
|
2022-02-18 16:35:32 +03:00
|
|
|
|]
|
|
|
|
shouldReturnYaml
|
|
|
|
opts
|
2022-04-20 20:15:42 +03:00
|
|
|
(GraphqlEngine.postGraphqlWithHeaders testEnvironment userHeaders query)
|
2022-02-18 16:35:32 +03:00
|
|
|
expectedResponse
|
|
|
|
|
|
|
|
-- query limit should be applied when query limit <= permission limit
|
2022-04-20 20:15:42 +03:00
|
|
|
it "user-limit-less-than-permission-limit" \(testEnvironment, _) -> do
|
2022-02-18 16:35:32 +03:00
|
|
|
let userHeaders = [("x-hasura-role", "role2"), ("x-hasura-artist-id", "1")]
|
|
|
|
query =
|
|
|
|
[graphql|
|
|
|
|
query {
|
|
|
|
artist: hasura_artist(
|
|
|
|
where: {name: {_eq: "artist1"}}
|
|
|
|
) {
|
|
|
|
name
|
2022-04-06 10:18:59 +03:00
|
|
|
albums (order_by: {id: asc} limit: 1){
|
2022-02-18 16:35:32 +03:00
|
|
|
title
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
expectedResponse =
|
|
|
|
[yaml|
|
|
|
|
data:
|
|
|
|
artist:
|
|
|
|
- name: artist1
|
2022-04-06 10:18:59 +03:00
|
|
|
albums:
|
|
|
|
- title: album1_artist1
|
2022-02-18 16:35:32 +03:00
|
|
|
|]
|
|
|
|
shouldReturnYaml
|
|
|
|
opts
|
2022-04-20 20:15:42 +03:00
|
|
|
(GraphqlEngine.postGraphqlWithHeaders testEnvironment userHeaders query)
|
2022-02-18 16:35:32 +03:00
|
|
|
expectedResponse
|
|
|
|
|
|
|
|
-- permission limit should be applied when query limit > permission limit
|
2022-04-20 20:15:42 +03:00
|
|
|
it "user-limit-greater-than-permission-limit" \(testEnvironment, _) -> do
|
2022-02-18 16:35:32 +03:00
|
|
|
let userHeaders = [("x-hasura-role", "role2"), ("x-hasura-artist-id", "1")]
|
|
|
|
query =
|
|
|
|
[graphql|
|
|
|
|
query {
|
|
|
|
artist: hasura_artist(
|
|
|
|
where: {name: {_eq: "artist1"}}
|
|
|
|
) {
|
|
|
|
name
|
|
|
|
albums (order_by: {id: asc} limit: 4){
|
|
|
|
title
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
expectedResponse =
|
|
|
|
[yaml|
|
|
|
|
data:
|
|
|
|
artist:
|
|
|
|
- name: artist1
|
|
|
|
albums:
|
|
|
|
- title: album1_artist1
|
2022-04-06 10:18:59 +03:00
|
|
|
- title: album2_artist1
|
2022-02-18 16:35:32 +03:00
|
|
|
|]
|
|
|
|
shouldReturnYaml
|
|
|
|
opts
|
2022-04-20 20:15:42 +03:00
|
|
|
(GraphqlEngine.postGraphqlWithHeaders testEnvironment userHeaders query)
|
2022-02-18 16:35:32 +03:00
|
|
|
expectedResponse
|
|
|
|
|
|
|
|
-- permission limit should only apply on 'nodes' but not on 'aggregate'
|
2022-04-20 20:15:42 +03:00
|
|
|
it "aggregations" \(testEnvironment, _) -> do
|
2022-02-18 16:35:32 +03:00
|
|
|
let userHeaders = [("x-hasura-role", "role2"), ("x-hasura-artist-id", "1")]
|
|
|
|
query =
|
|
|
|
[graphql|
|
|
|
|
query {
|
|
|
|
artist: hasura_artist(
|
|
|
|
where: {name: {_eq: "artist1"}}
|
|
|
|
) {
|
|
|
|
name
|
|
|
|
albums_aggregate (order_by: {id: asc}){
|
|
|
|
aggregate {
|
|
|
|
count
|
|
|
|
}
|
|
|
|
nodes {
|
|
|
|
title
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
expectedResponse =
|
|
|
|
[yaml|
|
|
|
|
data:
|
|
|
|
artist:
|
|
|
|
- name: artist1
|
|
|
|
albums_aggregate:
|
|
|
|
aggregate:
|
2022-04-06 10:18:59 +03:00
|
|
|
count: 3
|
2022-02-18 16:35:32 +03:00
|
|
|
nodes:
|
|
|
|
- title: album1_artist1
|
2022-04-06 10:18:59 +03:00
|
|
|
- title: album2_artist1
|
2022-02-18 16:35:32 +03:00
|
|
|
|]
|
|
|
|
shouldReturnYaml
|
|
|
|
opts
|
2022-04-20 20:15:42 +03:00
|
|
|
(GraphqlEngine.postGraphqlWithHeaders testEnvironment userHeaders query)
|
2022-02-18 16:35:32 +03:00
|
|
|
expectedResponse
|
|
|
|
|
|
|
|
-- query limit applies to both 'aggregate' and 'nodes'
|
2022-04-20 20:15:42 +03:00
|
|
|
it "aggregations-query-limit" \(testEnvironment, _) -> do
|
2022-02-18 16:35:32 +03:00
|
|
|
let userHeaders = [("x-hasura-role", "role2"), ("x-hasura-artist-id", "1")]
|
|
|
|
query =
|
|
|
|
[graphql|
|
|
|
|
query {
|
|
|
|
artist: hasura_artist(
|
|
|
|
where: {name: {_eq: "artist1"}}
|
|
|
|
) {
|
|
|
|
name
|
2022-04-06 10:18:59 +03:00
|
|
|
albums_aggregate (limit: 2 order_by: {id: asc}){
|
2022-02-18 16:35:32 +03:00
|
|
|
aggregate {
|
|
|
|
count
|
|
|
|
}
|
|
|
|
nodes {
|
|
|
|
title
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
expectedResponse =
|
|
|
|
[yaml|
|
|
|
|
data:
|
|
|
|
artist:
|
|
|
|
- name: artist1
|
|
|
|
albums_aggregate:
|
|
|
|
aggregate:
|
2022-04-06 10:18:59 +03:00
|
|
|
count: 2
|
2022-02-18 16:35:32 +03:00
|
|
|
nodes:
|
|
|
|
- title: album1_artist1
|
2022-04-06 10:18:59 +03:00
|
|
|
- title: album2_artist1
|
2022-02-18 16:35:32 +03:00
|
|
|
|]
|
|
|
|
shouldReturnYaml
|
|
|
|
opts
|
2022-04-20 20:15:42 +03:00
|
|
|
(GraphqlEngine.postGraphqlWithHeaders testEnvironment userHeaders query)
|
2022-02-18 16:35:32 +03:00
|
|
|
expectedResponse
|