2022-03-16 03:39:21 +03:00
{- # LANGUAGE QuasiQuotes # -}
2021-11-19 18:13:33 +03:00
-- | Test directives.
2022-01-21 10:48:27 +03:00
module Test.DirectivesSpec ( spec ) where
2021-11-19 18:13:33 +03:00
2021-12-29 18:49:56 +03:00
import Data.Aeson ( Value )
2022-01-21 10:48:27 +03:00
import Harness.Backend.Mysql as Mysql
2021-11-19 18:13:33 +03:00
import Harness.GraphqlEngine qualified as GraphqlEngine
2022-01-21 10:48:27 +03:00
import Harness.Quoter.Graphql ( graphql )
import Harness.Quoter.Yaml
2021-11-23 21:15:17 +03:00
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
2021-11-19 18:13:33 +03:00
import Test.Hspec
import Prelude
--------------------------------------------------------------------------------
-- Preamble
2021-11-23 21:15:17 +03:00
spec :: SpecWith State
2021-11-19 18:13:33 +03:00
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 . MySQL ,
2022-02-21 20:05:09 +03:00
mkLocalState = Context . noLocalState ,
2022-03-10 14:18:13 +03:00
setup = Mysql . setup schema ,
teardown = Mysql . teardown schema ,
2022-02-18 16:35:32 +03:00
customOptions = Nothing
2022-02-14 20:24:24 +03:00
}
]
tests
2021-11-19 18:13:33 +03:00
--------------------------------------------------------------------------------
2022-03-10 14:18:13 +03:00
-- Schema
2021-11-19 18:13:33 +03:00
2022-03-10 14:18:13 +03:00
schema :: [ Schema . Table ]
schema = [ author ]
2021-11-19 18:13:33 +03:00
2022-03-10 14:18:13 +03:00
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 " ]
]
2021-11-19 18:13:33 +03:00
--------------------------------------------------------------------------------
-- Tests
2021-12-29 18:49:56 +03:00
data QueryParams = QueryParams
{ includeId :: Bool ,
skipId :: Bool
}
query :: QueryParams -> Value
query QueryParams { includeId , skipId } =
[ graphql |
query author_with_both {
2021-11-19 18:13:33 +03:00
hasura_author {
2021-12-29 18:49:56 +03:00
id @ include ( if : # { includeId } ) @ skip ( if : # { skipId } )
2021-11-19 18:13:33 +03:00
name
}
}
| ]
2021-12-29 18:49:56 +03:00
2022-02-21 20:05:09 +03:00
tests :: Context . Options -> SpecWith State
2022-02-09 18:26:14 +03:00
tests opts = do
2021-12-29 18:49:56 +03:00
it " Skip id field conditionally " \ state ->
shouldReturnYaml
2022-02-09 18:26:14 +03:00
opts
2021-12-29 18:49:56 +03:00
( GraphqlEngine . postGraphql
state
( query QueryParams { includeId = False , skipId = False } )
2021-11-19 18:13:33 +03:00
)
[ yaml |
data :
hasura_author :
- name : Author 1
- name : Author 2
| ]
2021-11-23 21:15:17 +03:00
it " Skip id field conditionally, includeId=true " \ state ->
2021-11-19 18:13:33 +03:00
shouldReturnYaml
2022-02-09 18:26:14 +03:00
opts
2021-12-29 18:49:56 +03:00
( GraphqlEngine . postGraphql
2021-11-23 21:15:17 +03:00
state
2021-12-29 18:49:56 +03:00
( query QueryParams { includeId = True , skipId = False } )
2021-11-19 18:13:33 +03:00
)
[ yaml |
data :
hasura_author :
- id : 1
name : Author 1
- id : 2
name : Author 2
| ]
2021-11-23 21:15:17 +03:00
it " Skip id field conditionally, skipId=true " \ state ->
2021-11-19 18:13:33 +03:00
shouldReturnYaml
2022-02-09 18:26:14 +03:00
opts
2021-12-29 18:49:56 +03:00
( GraphqlEngine . postGraphql
2021-11-23 21:15:17 +03:00
state
2021-12-29 18:49:56 +03:00
( query QueryParams { includeId = False , skipId = True } )
2021-11-19 18:13:33 +03:00
)
[ yaml |
data :
hasura_author :
- name : Author 1
- name : Author 2
| ]
2021-11-23 21:15:17 +03:00
it " Skip id field conditionally, skipId=true, includeId=true " \ state ->
2021-11-19 18:13:33 +03:00
shouldReturnYaml
2022-02-09 18:26:14 +03:00
opts
2021-12-29 18:49:56 +03:00
( GraphqlEngine . postGraphql
2021-11-23 21:15:17 +03:00
state
2021-12-29 18:49:56 +03:00
( query QueryParams { includeId = True , skipId = True } )
2021-11-19 18:13:33 +03:00
)
[ yaml |
data :
hasura_author :
- name : Author 1
- name : Author 2
| ]
2022-01-07 20:51:42 +03:00
-- These two come from <https://github.com/hasura/graphql-engine-mono/blob/ec3568c704c4c3f13ecff757c547f0d5a272307b/server/tests-py/queries/graphql_query/mysql/select_query_author_with_skip_directive.yaml#L1>
it " Author with skip id " \ state ->
shouldReturnYaml
2022-02-09 18:26:14 +03:00
opts
2022-01-07 20:51:42 +03:00
( GraphqlEngine . postGraphqlYaml
state
[ yaml |
query : |
query author_with_skip ( $ skipId : Boolean ! , $ skipName : Boolean ! ) {
hasura_author {
id @ skip ( if : $ skipId )
name @ skip ( if : $ skipName )
}
}
variables :
skipId : true
skipName : false
| ]
)
[ yaml |
data :
hasura_author :
- name : Author 1
- name : Author 2
| ]
it " Author with skip name " \ state ->
shouldReturnYaml
2022-02-09 18:26:14 +03:00
opts
2022-01-07 20:51:42 +03:00
( GraphqlEngine . postGraphqlYaml
state
[ yaml |
query : |
query author_with_skip ( $ skipId : Boolean ! , $ skipName : Boolean ! ) {
hasura_author {
id @ skip ( if : $ skipId )
name @ skip ( if : $ skipName )
}
}
variables :
skipId : false
skipName : true
| ]
)
[ yaml |
data :
hasura_author :
- id : 1
- id : 2
| ]
-- These three come from <https://github.com/hasura/graphql-engine-mono/blob/5f6f862e5f6b67d82cfa59568edfc4f08b920375/server/tests-py/queries/graphql_query/mysql/select_query_author_with_wrong_directive_err.yaml#L1>
it " Rejects unknown directives " \ state ->
shouldReturnYaml
2022-02-09 18:26:14 +03:00
opts
2022-01-07 20:51:42 +03:00
( GraphqlEngine . postGraphqlYaml
state
[ yaml |
query : |
query {
hasura_author {
id @ exclude ( if : true )
name
}
}
| ]
)
[ yaml |
errors :
- extensions :
path : $. selectionSet . hasura_author . selectionSet
code : validation - failed
message : directive " exclude " is not defined in the schema
| ]
it " Rejects duplicate directives " \ state ->
shouldReturnYaml
2022-02-09 18:26:14 +03:00
opts
2022-01-07 20:51:42 +03:00
( GraphqlEngine . postGraphqlYaml
state
[ yaml |
query : |
query {
hasura_author {
id @ include ( if : true ) @ include ( if : true )
name
}
}
| ]
)
[ yaml |
errors :
- extensions :
path : $. selectionSet . hasura_author . selectionSet
code : validation - failed
message : 'the following directives are used more than once : include'
| ]
it " Rejects directives on wrong element " \ state ->
shouldReturnYaml
2022-02-09 18:26:14 +03:00
opts
2022-01-07 20:51:42 +03:00
( GraphqlEngine . postGraphqlYaml
state
[ yaml |
query : |
query @ include ( if : true ) {
hasura_author {
id
name
}
}
| ]
)
[ yaml |
errors :
- extensions :
path : $
code : validation - failed
message : directive " include " is not allowed on a query
| ]