diff options
Diffstat (limited to 'docs/tutorial')
| -rw-r--r-- | docs/tutorial/tutorial.lhs | 57 |
1 files changed, 40 insertions, 17 deletions
diff --git a/docs/tutorial/tutorial.lhs b/docs/tutorial/tutorial.lhs index edaf7f2..13afb81 100644 --- a/docs/tutorial/tutorial.lhs +++ b/docs/tutorial/tutorial.lhs @@ -17,14 +17,15 @@ Since this file is a literate haskell file, we start by importing some dependenc > import Control.Monad.IO.Class (liftIO) > import Data.Aeson (encode) > import Data.ByteString.Lazy.Char8 (putStrLn) -> import Data.HashMap.Strict (HashMap) > import qualified Data.HashMap.Strict as HashMap -> import Data.List.NonEmpty (NonEmpty(..)) > import Data.Text (Text) +> import qualified Data.Text as Text > import Data.Time (getCurrentTime) > > import Language.GraphQL -> import qualified Language.GraphQL.Schema as Schema +> import Language.GraphQL.Trans +> import Language.GraphQL.Type +> import qualified Language.GraphQL.Type.Out as Out > > import Prelude hiding (putStrLn) @@ -35,11 +36,19 @@ example from [graphql.js](https://github.com/graphql/graphql-js). First we build a GraphQL schema. -> schema1 :: HashMap Text (NonEmpty (Schema.Resolver IO)) -> schema1 = HashMap.singleton "Query" $ hello :| [] +> schema1 :: Schema IO +> schema1 = Schema queryType Nothing > -> hello :: Schema.Resolver IO -> hello = Schema.scalar "hello" (return ("it's me" :: Text)) +> queryType :: ObjectType IO +> queryType = ObjectType "Query" Nothing [] +> $ HashMap.singleton "hello" +> $ Out.Resolver helloField hello +> +> helloField :: Field IO +> helloField = Field Nothing (Out.NamedScalarType string) mempty +> +> hello :: ActionT IO Value +> hello = pure $ String "it's me" This defines a simple schema with one type and one field, that resolves to a fixed value. @@ -52,7 +61,7 @@ Next we define our query. To run the query, we call the `graphql` with the schema and the query. > main1 :: IO () -> main1 = putStrLn =<< encode <$> graphql schema1 query1 +> main1 = graphql schema1 query1 >>= putStrLn . encode This runs the query by fetching the one field defined, returning @@ -65,13 +74,21 @@ returning For this example, we're going to be using time. -> schema2 :: HashMap Text (NonEmpty (Schema.Resolver IO)) -> schema2 = HashMap.singleton "Query" $ time :| [] +> schema2 :: Schema IO +> schema2 = Schema queryType2 Nothing +> +> queryType2 :: ObjectType IO +> queryType2 = ObjectType "Query" Nothing [] +> $ HashMap.singleton "time" +> $ Out.Resolver timeField time > -> time :: Schema.Resolver IO -> time = Schema.scalar "time" $ do +> timeField :: Field IO +> timeField = Field Nothing (Out.NamedScalarType string) mempty +> +> time :: ActionT IO Value +> time = do > t <- liftIO getCurrentTime -> return $ show t +> pure $ String $ Text.pack $ show t This defines a simple schema with one type and one field, which resolves to the current time. @@ -82,7 +99,7 @@ Next we define our query. > query2 = "{ time }" > > main2 :: IO () -> main2 = putStrLn =<< encode <$> graphql schema2 query2 +> main2 = graphql schema2 query2 >>= putStrLn . encode This runs the query, returning the current time @@ -124,14 +141,20 @@ This will fail Now that we have two resolvers, we can define a schema which uses them both. -> schema3 :: HashMap Text (NonEmpty (Schema.Resolver IO)) -> schema3 = HashMap.singleton "Query" $ hello :| [time] +> schema3 :: Schema IO +> schema3 = Schema queryType3 Nothing +> +> queryType3 :: ObjectType IO +> queryType3 = ObjectType "Query" Nothing [] $ HashMap.fromList +> [ ("hello", Out.Resolver helloField hello) +> , ("time", Out.Resolver timeField time) +> ] > > query3 :: Text > query3 = "query timeAndHello { time hello }" > > main3 :: IO () -> main3 = putStrLn =<< encode <$> graphql schema3 query3 +> main3 = graphql schema3 query3 >>= putStrLn . encode This queries for both time and hello, returning |
