graphql-engine/server/tests-hspec/Test/BasicFieldsSpec.hs
jkachmar 647231b685 Yeet some default-extensions
Manually enables:
* EmptyCase
* ExistentialQuantification
* QuantifiedConstraints
* QuasiQuotes
* TemplateHaskell
* TypeFamilyDependencies

...in the following components:
* 'graphql-engine' library
* 'graphql-engine' 'src-test'
* 'graphql-engine' 'tests/integration'
* 'graphql-engine' tests-hspec'

Additionally, performs some light refactoring and documentation.

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/3991
GitOrigin-RevId: 514477d3466b01f60eca8935d0fef60dd0756838
2022-03-16 00:40:17 +00:00

203 lines
4.7 KiB
Haskell

{-# LANGUAGE QuasiQuotes #-}
-- | Test querying an entity for a couple fields.
module Test.BasicFieldsSpec (spec) where
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 qualified as GraphqlEngine
import Harness.Quoter.Graphql (graphql)
import Harness.Quoter.Yaml (shouldReturnYaml, yaml)
import Harness.State (State)
import Harness.Test.Context qualified as Context
import Harness.Test.Schema qualified as Schema
import Test.Hspec (SpecWith, describe, it)
import Prelude
--------------------------------------------------------------------------------
-- Preamble
spec :: SpecWith State
spec =
Context.run
[ Context.Context
{ name = Context.Backend Context.MySQL,
mkLocalState = Context.noLocalState,
setup = Mysql.setup schema,
teardown = Mysql.teardown schema,
customOptions = Nothing
},
Context.Context
{ name = Context.Backend Context.Postgres,
mkLocalState = Context.noLocalState,
setup = Postgres.setup schema,
teardown = Postgres.teardown schema,
customOptions = Nothing
},
Context.Context
{ name = Context.Backend Context.Citus,
mkLocalState = Context.noLocalState,
setup = Citus.setup schema,
teardown = Citus.teardown schema,
customOptions = Nothing
},
Context.Context
{ name = Context.Backend Context.SQLServer,
mkLocalState = Context.noLocalState,
setup = Sqlserver.setup schema,
teardown = Sqlserver.teardown schema,
customOptions = Nothing
},
Context.Context
{ name = Context.Backend Context.BigQuery,
mkLocalState = Context.noLocalState,
setup = Bigquery.setup schema,
teardown = Bigquery.teardown schema,
customOptions =
Just $
Context.Options
{ stringifyNumbers = True
}
}
]
tests
--------------------------------------------------------------------------------
-- Schema
schema :: [Schema.Table]
schema = [author]
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"]
]
_article :: Schema.Table
_article =
Schema.Table
"article"
[ Schema.column "id" Schema.TInt,
Schema.column "name" Schema.TStr,
Schema.column "author_id" Schema.TInt
]
["id"]
[Schema.Reference "author_id" "hasura.author" "id"]
[ [Schema.VInt 1, Schema.VStr "article1", Schema.VInt 1],
[Schema.VInt 2, Schema.VStr "article2", Schema.VInt 2],
[Schema.VInt 3, Schema.VStr "article3", Schema.VInt 3]
]
--------------------------------------------------------------------------------
-- Tests
tests :: Context.Options -> SpecWith State
tests opts = describe "BasicFieldsSpec" $ do
it "Author fields" $ \state ->
shouldReturnYaml
opts
( GraphqlEngine.postGraphql
state
[graphql|
query {
hasura_author(order_by:[{id:asc}]) {
name
id
}
}
|]
)
[yaml|
data:
hasura_author:
- name: Author 1
id: 1
- name: Author 2
id: 2
|]
it "Use operationName" $ \state ->
shouldReturnYaml
opts
( GraphqlEngine.postGraphqlYaml
state
[yaml|
operationName: chooseThisOne
query: |
query ignoreThisOne {
MyQuery {
name
}
}
query chooseThisOne {
hasura_author(order_by:[{id:asc}]) {
id
name
}
}
|]
)
[yaml|
data:
hasura_author:
- name: Author 1
id: 1
- name: Author 2
id: 2
|]
it "Missing field" $ \state -> do
shouldReturnYaml
opts
( GraphqlEngine.postGraphql
state
[graphql|
query {
hasura_author {
id
name
notPresentCol
}
}
|]
)
[yaml|
errors:
- extensions:
code: validation-failed
path: $.selectionSet.hasura_author.selectionSet.notPresentCol
message: |-
field "notPresentCol" not found in type: 'hasura_author'
|]
it "Missing table" $ \state ->
shouldReturnYaml
opts
( GraphqlEngine.postGraphql
state
[graphql|
query {
random {
id
name
}
}
|]
)
[yaml|
errors:
- extensions:
code: validation-failed
path: $.selectionSet.random
message: |-
field "random" not found in type: 'query_root'
|]