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 library
default-language: Haskell2010 default-language: Haskell2010
ghc-options: -Wall ghc-options: -Wall
exposed-modules: Data.GraphQL.AST exposed-modules: Data.GraphQL
Data.GraphQL.AST
Data.GraphQL.Encoder Data.GraphQL.Encoder
Data.GraphQL.Execute Data.GraphQL.Execute
Data.GraphQL.Schema Data.GraphQL.Schema

View File

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