mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-22 15:01:39 +03:00
181 lines
5.5 KiB
Haskell
181 lines
5.5 KiB
Haskell
|
{-# LANGUAGE QuasiQuotes #-}
|
||
|
|
||
|
-- |
|
||
|
-- Tests for GraphQL query directives.
|
||
|
--
|
||
|
-- https://spec.graphql.org/June2018/#sec-Type-System.Directives
|
||
|
-- https://hasura.io/docs/latest/queries/postgres/variables-aliases-fragments-directives/
|
||
|
-- https://hasura.io/docs/latest/queries/ms-sql-server/variables-aliases-fragments-directives/
|
||
|
-- https://hasura.io/docs/latest/queries/bigquery/variables-aliases-fragments-directives/
|
||
|
module Test.Queries.DirectivesSpec (spec) where
|
||
|
|
||
|
import Data.Aeson (Value)
|
||
|
import Harness.Backend.BigQuery qualified as BigQuery
|
||
|
import Harness.Backend.Citus qualified as Citus
|
||
|
import Harness.Backend.Mysql qualified as Mysql
|
||
|
import Harness.Backend.Postgres qualified as Postgres
|
||
|
import Harness.Backend.Sqlserver qualified as Sqlserver
|
||
|
import Harness.GraphqlEngine (postGraphql)
|
||
|
import Harness.Quoter.Graphql (graphql)
|
||
|
import Harness.Quoter.Yaml (shouldReturnYaml, yaml)
|
||
|
import Harness.Test.Context (Options (..))
|
||
|
import Harness.Test.Context qualified as Context
|
||
|
import Harness.Test.Schema (Table (..), table)
|
||
|
import Harness.Test.Schema qualified as Schema
|
||
|
import Harness.TestEnvironment (TestEnvironment)
|
||
|
import Test.Hspec (SpecWith, describe, it)
|
||
|
import Prelude
|
||
|
|
||
|
spec :: SpecWith TestEnvironment
|
||
|
spec =
|
||
|
Context.run
|
||
|
[ Context.Context
|
||
|
{ name = Context.Backend Context.MySQL,
|
||
|
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
|
||
|
setup = Mysql.setup schema,
|
||
|
teardown = Mysql.teardown schema,
|
||
|
customOptions = Nothing
|
||
|
},
|
||
|
Context.Context
|
||
|
{ name = Context.Backend Context.Postgres,
|
||
|
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
|
||
|
setup = Postgres.setup schema,
|
||
|
teardown = Postgres.teardown schema,
|
||
|
customOptions = Nothing
|
||
|
},
|
||
|
Context.Context
|
||
|
{ name = Context.Backend Context.Citus,
|
||
|
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
|
||
|
setup = Citus.setup schema,
|
||
|
teardown = Citus.teardown schema,
|
||
|
customOptions = Nothing
|
||
|
},
|
||
|
Context.Context
|
||
|
{ name = Context.Backend Context.SQLServer,
|
||
|
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
|
||
|
setup = Sqlserver.setup schema,
|
||
|
teardown = Sqlserver.teardown schema,
|
||
|
customOptions = Nothing
|
||
|
},
|
||
|
Context.Context
|
||
|
{ name = Context.Backend Context.BigQuery,
|
||
|
mkLocalTestEnvironment = Context.noLocalTestEnvironment,
|
||
|
setup = BigQuery.setup schema,
|
||
|
teardown = BigQuery.teardown schema,
|
||
|
customOptions =
|
||
|
Just $
|
||
|
Context.Options
|
||
|
{ stringifyNumbers = True
|
||
|
}
|
||
|
}
|
||
|
]
|
||
|
tests
|
||
|
|
||
|
--------------------------------------------------------------------------------
|
||
|
-- Schema
|
||
|
|
||
|
schema :: [Schema.Table]
|
||
|
schema =
|
||
|
[ (table "author")
|
||
|
{ tableColumns =
|
||
|
[ Schema.column "id" Schema.TInt,
|
||
|
Schema.column "name" Schema.TStr
|
||
|
],
|
||
|
tablePrimaryKey = ["id"],
|
||
|
tableData =
|
||
|
[ [Schema.VInt 1, Schema.VStr "Author 1"],
|
||
|
[Schema.VInt 2, Schema.VStr "Author 2"]
|
||
|
]
|
||
|
}
|
||
|
]
|
||
|
|
||
|
--------------------------------------------------------------------------------
|
||
|
-- Tests
|
||
|
|
||
|
tests :: Context.Options -> SpecWith TestEnvironment
|
||
|
tests opts = do
|
||
|
let shouldBe :: IO Value -> Value -> IO ()
|
||
|
shouldBe = shouldReturnYaml opts
|
||
|
|
||
|
describe "Directives" do
|
||
|
it "Rejects unknown directives" \testEnvironment -> do
|
||
|
let expected :: Value
|
||
|
expected =
|
||
|
[yaml|
|
||
|
errors:
|
||
|
- extensions:
|
||
|
path: $.selectionSet.hasura_author.selectionSet
|
||
|
code: validation-failed
|
||
|
message: |-
|
||
|
directive 'exclude' is not defined in the schema
|
||
|
|]
|
||
|
|
||
|
actual :: IO Value
|
||
|
actual =
|
||
|
postGraphql
|
||
|
testEnvironment
|
||
|
[graphql|
|
||
|
query {
|
||
|
hasura_author {
|
||
|
id @exclude(if: true)
|
||
|
name
|
||
|
}
|
||
|
}
|
||
|
|]
|
||
|
|
||
|
actual `shouldBe` expected
|
||
|
|
||
|
it "Rejects duplicate directives" \testEnvironment -> do
|
||
|
let expected :: Value
|
||
|
expected =
|
||
|
[yaml|
|
||
|
errors:
|
||
|
- extensions:
|
||
|
path: $.selectionSet.hasura_author.selectionSet
|
||
|
code: validation-failed
|
||
|
message: |-
|
||
|
the following directives are used more than once: ['include']
|
||
|
|]
|
||
|
|
||
|
actual :: IO Value
|
||
|
actual =
|
||
|
postGraphql
|
||
|
testEnvironment
|
||
|
[graphql|
|
||
|
query {
|
||
|
hasura_author {
|
||
|
id @include(if: true) @include(if: true)
|
||
|
name
|
||
|
}
|
||
|
}
|
||
|
|]
|
||
|
|
||
|
actual `shouldBe` expected
|
||
|
|
||
|
it "Rejects directives on wrong element" \testEnvironment -> do
|
||
|
let expected :: Value
|
||
|
expected =
|
||
|
[yaml|
|
||
|
errors:
|
||
|
- extensions:
|
||
|
path: $
|
||
|
code: validation-failed
|
||
|
message: |-
|
||
|
directive 'include' is not allowed on a query
|
||
|
|]
|
||
|
|
||
|
actual :: IO Value
|
||
|
actual =
|
||
|
postGraphql
|
||
|
testEnvironment
|
||
|
[graphql|
|
||
|
query @include(if: true) {
|
||
|
hasura_author {
|
||
|
id
|
||
|
name
|
||
|
}
|
||
|
}
|
||
|
|]
|
||
|
|
||
|
actual `shouldBe` expected
|