Introduce graphql function

This simplifies Attoparsec parsing when executing a GraphQL
query.
This commit is contained in:
Danny Navarro 2016-02-05 12:32:35 +01:00
parent eca3c2d8d4
commit c81ddb0335
3 changed files with 26 additions and 16 deletions

16
Data/GraphQL.hs Normal file
View File

@ -0,0 +1,16 @@
module Data.GraphQL where
import Control.Applicative (Alternative, empty)
import Data.Text (Text)
import qualified Data.Aeson as Aeson
import qualified Data.Attoparsec.Text as Attoparsec
import Data.GraphQL.Execute
import Data.GraphQL.Parser
import Data.GraphQL.Schema
graphql :: (Alternative m, Monad m) => Schema m -> Text -> m Aeson.Value
graphql schema = either (const empty) (execute schema)
. Attoparsec.parseOnly document

View File

@ -22,7 +22,8 @@ data-files: tests/data/*.graphql
library
default-language: Haskell2010
ghc-options: -Wall
exposed-modules: Data.GraphQL.AST
exposed-modules: Data.GraphQL
Data.GraphQL.AST
Data.GraphQL.Encoder
Data.GraphQL.Execute
Data.GraphQL.Schema

View File

@ -11,30 +11,23 @@ import Control.Applicative (Alternative, (<|>), empty, liftA2)
import Data.Maybe (catMaybes)
import qualified Data.Aeson as Aeson
import Data.Attoparsec.Text (parseOnly)
import Data.Text (Text)
import Test.Tasty (TestTree)
import Test.Tasty.HUnit
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.HUnit (testCase, (@?=))
import Data.GraphQL.AST
import Data.GraphQL.Execute
import qualified Data.GraphQL.Parser as Parser
import Data.GraphQL
import Data.GraphQL.Schema
-- * Test
-- See https://github.com/graphql/graphql-js/blob/master/src/__tests__/starWarsQueryTests.js
test :: TestTree
test = testCase "R2-D2" $ execute schema heroQuery @?= expected
where
heroQuery :: Document
heroQuery = either (error "Parsing error") id $ parseOnly Parser.document
"query HeroNameQuery{hero{name}}"
expected :: Maybe Response
expected = Just $ Aeson.Object [("hero", Aeson.Object [("name", "R2-D2")])]
test = testGroup "Basic Queries"
[testCase "R2-D2"
$ graphql schema "query HeroNameQuery{hero{name}}"
@?= Just (Aeson.Object [("hero", Aeson.Object [("name", "R2-D2")])])
]
-- * Schema
-- See https://github.com/graphql/graphql-js/blob/master/src/__tests__/starWarsSchema.js