Skip to content

Commit 5ac071e

Browse files
authored
Merge pull request #169 from haskell-graphql/make-examples-programs
Make examples programs
2 parents 8835750 + 377408b commit 5ac071e

File tree

9 files changed

+207
-73
lines changed

9 files changed

+207
-73
lines changed

examples

-1
This file was deleted.

tests/Examples/InputObject.hs renamed to examples/InputObject.hs

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
{-# LANGUAGE DataKinds #-}
2-
{-# LANGUAGE TypeOperators #-}
32
{-# LANGUAGE DeriveGeneric #-}
3+
{-# LANGUAGE TypeOperators #-}
44

5-
module Examples.InputObject where
5+
-- | Demonstrate input object usage.
6+
module Main (main) where
67

78
import Protolude hiding (Enum)
89

10+
import qualified Data.Aeson as Aeson
11+
912
import GraphQL
1013
import GraphQL.API
1114
import GraphQL.Resolver (Handler)
12-
import GraphQL.Value (FromValue)
15+
import GraphQL.Value (FromValue, toValue)
1316

14-
data DogStuff = DogStuff { toy :: Text, likesTreats :: Bool } deriving (Show, Generic)
17+
data DogStuff = DogStuff { _toy :: Text, _likesTreats :: Bool } deriving (Show, Generic)
1518
instance FromValue DogStuff
1619
instance HasAnnotatedInputType DogStuff
1720
instance Defaultable DogStuff where
@@ -30,10 +33,6 @@ description (DogStuff toy likesTreats)
3033
| likesTreats = pure $ "likes treats and their favorite toy is a " <> toy
3134
| otherwise = pure $ "their favorite toy is a " <> toy
3235

33-
-- $setup
34-
-- >>> import Data.Aeson (encode)
35-
-- >>> import GraphQL.Value (ToValue(..))
36-
3736
-- | Show input object usage
3837
--
3938
-- >>> response <- example "{ description(dogStuff: {toy: \"bone\", likesTreats: true}) }"
@@ -45,3 +44,11 @@ description (DogStuff toy likesTreats)
4544
-- {"data":{"description":"their favorite toy is a shoe"}}
4645
example :: Text -> IO Response
4746
example = interpretAnonymousQuery @Query root
47+
48+
49+
main :: IO ()
50+
main = do
51+
response <- example "{ description(dogStuff: {toy: \"bone\", likesTreats: true}) }"
52+
putStrLn $ Aeson.encode $ toValue response
53+
response' <- example "{ description }"
54+
putStrLn $ Aeson.encode $ toValue response'
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
{-# LANGUAGE DataKinds #-}
2-
module Examples.UnionExample where
2+
module Main (main) where
33

44
import Protolude
5+
6+
import qualified Data.Aeson as Aeson
57
import GraphQL.API (Field, List, Object, Union)
6-
import GraphQL (Response, interpretAnonymousQuery)
8+
import GraphQL (interpretAnonymousQuery)
79
import GraphQL.Resolver (Handler, (:<>)(..), unionValue)
10+
import GraphQL.Value (ToValue(..))
811

912
-- Slightly reduced example from the spec
1013
type MiniCat = Object "MiniCat" '[] '[Field "name" Text, Field "meowVolume" Int32]
@@ -31,22 +34,9 @@ catOrDogList = pure $
3134
, unionValue @MiniDog miniDog
3235
]
3336

34-
-- $setup
35-
-- >>> import Data.Aeson (encode)
36-
-- >>> import GraphQL.Value (ToValue(..))
37-
38-
-- | Show usage of a single unionValue
39-
--
40-
-- >>> response <- exampleQuery
41-
-- >>> putStrLn $ encode $ toValue response
42-
-- {"data":{"myPet":{"meowVolume":32,"name":"MonadicFelix"}}}
43-
exampleQuery :: IO Response
44-
exampleQuery = interpretAnonymousQuery @CatOrDog catOrDog "{ myPet { ... on MiniCat { name meowVolume } ... on MiniDog { barkVolume } } }"
45-
46-
-- | 'unionValue' can be used in a list context
47-
--
48-
-- >>> response <- exampleListQuery
49-
-- >>> putStrLn $ encode $ toValue response
50-
-- {"data":{"pets":[{"meowVolume":32,"name":"Felix"},{"meowVolume":32,"name":"Mini"},{"barkVolume":100}]}}
51-
exampleListQuery :: IO Response
52-
exampleListQuery = interpretAnonymousQuery @CatOrDogList catOrDogList "{ pets { ... on MiniCat { name meowVolume } ... on MiniDog { barkVolume } } }"
37+
main :: IO ()
38+
main = do
39+
response <- interpretAnonymousQuery @CatOrDog catOrDog "{ myPet { ... on MiniCat { name meowVolume } ... on MiniDog { barkVolume } } }"
40+
putStrLn $ Aeson.encode $ toValue response
41+
response' <- interpretAnonymousQuery @CatOrDogList catOrDogList "{ pets { ... on MiniCat { name meowVolume } ... on MiniDog { barkVolume } } }"
42+
putStrLn $ Aeson.encode $ toValue response'

graphql-api.cabal

+33-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
--
33
-- see: https://github.com/sol/hpack
44
--
5-
-- hash: 244f60f8e2def55fa21d6d61b793b76e26ffbe6fea3b5a4b9fee01127d32358d
5+
-- hash: 6a38b887cec0d4a157469f5d73041fd16cb286d8f445f4e213c6f08965dbc563
66

77
name: graphql-api
88
version: 0.3.0
@@ -74,6 +74,38 @@ library
7474
Paths_graphql_api
7575
default-language: Haskell2010
7676

77+
executable input-object-example
78+
main-is: InputObject.hs
79+
hs-source-dirs:
80+
examples
81+
default-extensions: NoImplicitPrelude OverloadedStrings RecordWildCards TypeApplications
82+
ghc-options: -Wall -fno-warn-redundant-constraints
83+
build-depends:
84+
aeson
85+
, attoparsec
86+
, base >=4.9 && <5
87+
, exceptions
88+
, graphql-api
89+
, protolude >=0.2.1
90+
, transformers
91+
default-language: Haskell2010
92+
93+
executable union-example
94+
main-is: UnionExample.hs
95+
hs-source-dirs:
96+
examples
97+
default-extensions: NoImplicitPrelude OverloadedStrings RecordWildCards TypeApplications
98+
ghc-options: -Wall -fno-warn-redundant-constraints
99+
build-depends:
100+
aeson
101+
, attoparsec
102+
, base >=4.9 && <5
103+
, exceptions
104+
, graphql-api
105+
, protolude >=0.2.1
106+
, transformers
107+
default-language: Haskell2010
108+
77109
test-suite graphql-api-doctests
78110
type: exitcode-stdio-1.0
79111
main-is: Main.hs
@@ -118,8 +150,6 @@ test-suite graphql-api-tests
118150
ASTTests
119151
EndToEndTests
120152
EnumTests
121-
Examples.InputObject
122-
Examples.UnionExample
123153
ExampleSchema
124154
OrderedMapTests
125155
ResolverTests

package.yaml

+17
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,23 @@ library:
4343
- QuickCheck
4444
- text
4545

46+
executables:
47+
input-object-example:
48+
main: InputObject.hs
49+
source-dirs: examples
50+
other-modules: []
51+
dependencies:
52+
- aeson
53+
- graphql-api
54+
55+
union-example:
56+
main: UnionExample.hs
57+
source-dirs: examples
58+
other-modules: []
59+
dependencies:
60+
- aeson
61+
- graphql-api
62+
4663
tests:
4764
graphql-api-tests:
4865
main: Spec.hs

0 commit comments

Comments
 (0)