From c81ddb03353c65abaea1778159b0a7c37ffe38a8 Mon Sep 17 00:00:00 2001 From: Danny Navarro Date: Fri, 5 Feb 2016 12:32:35 +0100 Subject: [PATCH] Introduce `graphql` function This simplifies Attoparsec parsing when executing a GraphQL query. --- Data/GraphQL.hs | 16 ++++++++++++++++ graphql.cabal | 3 ++- tests/Test/StarWars.hs | 23 ++++++++--------------- 3 files changed, 26 insertions(+), 16 deletions(-) create mode 100644 Data/GraphQL.hs diff --git a/Data/GraphQL.hs b/Data/GraphQL.hs new file mode 100644 index 0000000..d18ff2f --- /dev/null +++ b/Data/GraphQL.hs @@ -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 diff --git a/graphql.cabal b/graphql.cabal index 7cffa53..40da990 100644 --- a/graphql.cabal +++ b/graphql.cabal @@ -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 diff --git a/tests/Test/StarWars.hs b/tests/Test/StarWars.hs index bdd1991..8fe01ae 100644 --- a/tests/Test/StarWars.hs +++ b/tests/Test/StarWars.hs @@ -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