Skip to content

Commit 11b7e62

Browse files
committed
Add tests for anonymous queries w|w/o variables
Added tests in ASTTests.hs: - parses anonymous query documents - changed previous test of same name to: parses shorthand syntax documents - parses anonymous query with variables Added tests in ValidationTests.hs: - Treats anonymous queries as valid - Treats anonymous queries with variables as valid
1 parent a8cd01f commit 11b7e62

File tree

3 files changed

+91
-3
lines changed

3 files changed

+91
-3
lines changed

Diff for: src/GraphQL/Internal/Name.hs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
{-# LANGUAGE ScopedTypeVariables #-}
77
module GraphQL.Internal.Name
88
( Name(unName, Name)
9+
, mempty
910
, NameError(..)
1011
, makeName
1112
, nameFromSymbol

Diff for: tests/ASTTests.hs

+51-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import Test.Tasty (TestTree)
1313
import Test.Tasty.Hspec (testSpec, describe, it, shouldBe)
1414

1515
import GraphQL.Value (String(..))
16-
import GraphQL.Internal.Name (Name)
16+
import GraphQL.Internal.Name (Name (Name), mempty)
1717
import qualified GraphQL.Internal.Syntax.AST as AST
1818
import qualified GraphQL.Internal.Syntax.Parser as Parser
1919
import qualified GraphQL.Internal.Syntax.Encoder as Encoder
@@ -78,7 +78,7 @@ tests = testSpec "AST" $ do
7878
output `shouldBe` "[1.5,1.5]"
7979
parseOnly Parser.value output `shouldBe` Right input
8080
describe "Parser" $ do
81-
it "parses anonymous query documents" $ do
81+
it "parses shorthand syntax documents" $ do
8282
let query = [r|{
8383
dog {
8484
name
@@ -96,6 +96,25 @@ tests = testSpec "AST" $ do
9696
]
9797
parsed `shouldBe` expected
9898

99+
it "parses anonymous query documents" $ do
100+
let query = [r|query {
101+
dog {
102+
name
103+
}
104+
}|]
105+
let Right parsed = parseOnly Parser.queryDocument query
106+
let expected = AST.QueryDocument
107+
[ AST.DefinitionOperation
108+
(AST.Query
109+
(AST.Node (Name mempty) [] []
110+
[ AST.SelectionField
111+
(AST.Field Nothing dog [] []
112+
[ AST.SelectionField (AST.Field Nothing someName [] [] [])
113+
])
114+
]))
115+
]
116+
parsed `shouldBe` expected
117+
99118
it "parses invalid documents" $ do
100119
let query = [r|{
101120
dog {
@@ -162,3 +181,33 @@ tests = testSpec "AST" $ do
162181
]))
163182
]
164183
parsed `shouldBe` expected
184+
185+
it "parses anonymous query with variables" $ do
186+
let query = [r|
187+
query ($atOtherHomes: Boolean = true) {
188+
dog {
189+
isHousetrained(atOtherHomes: $atOtherHomes)
190+
}
191+
}
192+
|]
193+
let Right parsed = parseOnly Parser.queryDocument query
194+
let expected = AST.QueryDocument
195+
[ AST.DefinitionOperation
196+
(AST.Query
197+
(AST.Node (Name mempty)
198+
[ AST.VariableDefinition
199+
(AST.Variable "atOtherHomes")
200+
(AST.TypeNamed (AST.NamedType "Boolean"))
201+
(Just (AST.ValueBoolean True))
202+
] []
203+
[ AST.SelectionField
204+
(AST.Field Nothing dog [] []
205+
[ AST.SelectionField
206+
(AST.Field Nothing "isHousetrained"
207+
[ AST.Argument "atOtherHomes"
208+
(AST.ValueVariable (AST.Variable "atOtherHomes"))
209+
] [] [])
210+
])
211+
]))
212+
]
213+
parsed `shouldBe` expected

Diff for: tests/ValidationTests.hs

+39-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Test.QuickCheck ((===))
1010
import Test.Tasty (TestTree)
1111
import Test.Tasty.Hspec (testSpec, describe, it, shouldBe)
1212

13-
import GraphQL.Internal.Name (Name)
13+
import GraphQL.Internal.Name (Name(Name), mempty)
1414
import qualified GraphQL.Internal.Syntax.AST as AST
1515
import GraphQL.Internal.Schema (Schema)
1616
import GraphQL.Internal.Validation
@@ -25,6 +25,9 @@ me = "me"
2525
someName :: Name
2626
someName = "name"
2727

28+
dog :: Name
29+
dog = "dog"
30+
2831
-- | Schema used for these tests. Since none of them do type-level stuff, we
2932
-- don't need to define it.
3033
schema :: Schema
@@ -45,6 +48,41 @@ tests = testSpec "Validation" $ do
4548
]
4649
getErrors schema doc `shouldBe` []
4750

51+
it "Treats anonymous queries as valid" $ do
52+
let doc = AST.QueryDocument
53+
[ AST.DefinitionOperation
54+
(AST.Query
55+
(AST.Node (Name mempty) [] []
56+
[ AST.SelectionField
57+
(AST.Field Nothing dog [] []
58+
[ AST.SelectionField (AST.Field Nothing someName [] [] [])
59+
])
60+
]))
61+
]
62+
getErrors schema doc `shouldBe` []
63+
64+
it "Treats anonymous queries with variables as valid" $ do
65+
let doc = AST.QueryDocument
66+
[ AST.DefinitionOperation
67+
(AST.Query
68+
(AST.Node (Name mempty)
69+
[ AST.VariableDefinition
70+
(AST.Variable "atOtherHomes")
71+
(AST.TypeNamed (AST.NamedType "Boolean"))
72+
(Just (AST.ValueBoolean True))
73+
] []
74+
[ AST.SelectionField
75+
(AST.Field Nothing dog [] []
76+
[ AST.SelectionField
77+
(AST.Field Nothing "isHousetrained"
78+
[ AST.Argument "atOtherHomes"
79+
(AST.ValueVariable (AST.Variable "atOtherHomes"))
80+
] [] [])
81+
])
82+
]))
83+
]
84+
getErrors schema doc `shouldBe` []
85+
4886
it "Detects duplicate operation names" $ do
4987
let doc = AST.QueryDocument
5088
[ AST.DefinitionOperation

0 commit comments

Comments
 (0)