Skip to content

Commit a00534d

Browse files
authored
Merge pull request #161 from haskell-graphql/move-defaultable
Move Defaultable into GraphQL.API
2 parents a7e07f5 + 893f510 commit a00534d

File tree

5 files changed

+41
-47
lines changed

5 files changed

+41
-47
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@ Breaking changes
99
----------------
1010

1111
* ``Enum`` handlers are now monadic (see `#118`_)
12-
* restrict usage to Protolude 0.2.1 and later
12+
* You must use protolude 0.2.1 or later
13+
* ``Defaultable`` must now be imported from ``GraphQL.API``, rather than ``GraphQL.Resolver``,
14+
this moves ``GraphQL.API`` closer to being sufficient for API definition. (see `#149`_)
1315

1416
Improvements
1517
------------
1618

1719
* Now support GHC 8.2 as well as 8.0.2 and later
1820

1921
.. _`#118`: https://github.com/jml/graphql-api/issues/118
20-
22+
.. _`#149`: https://github.com/haskell-graphql/graphql-api/issues/149
2123

2224
v0.2.0 (2017-10-12)
2325
===================

src/GraphQL/API.hs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ module GraphQL.API
1515
( Object
1616
, Field
1717
, Argument
18-
, DefaultArgument
1918
, Union
2019
, List
2120
, Enum
2221
, GraphQLEnum(..)
2322
, Interface
2423
, (:>)(..)
24+
, Defaultable(..)
2525
, HasAnnotatedType(..)
2626
, HasAnnotatedInputType
2727
, HasObjectDefinition(..)
@@ -36,7 +36,7 @@ import Protolude hiding (Enum, TypeError)
3636

3737
import GHC.TypeLits (Symbol, KnownSymbol, TypeError, ErrorMessage(..))
3838
import qualified GraphQL.Internal.Schema as Schema
39-
import GraphQL.Internal.Name (NameError, nameFromSymbol)
39+
import GraphQL.Internal.Name (Name, NameError, nameFromSymbol)
4040
import GraphQL.API.Enum (GraphQLEnum(..))
4141
import GHC.Generics ((:*:)(..))
4242
import GHC.Types (Type)
@@ -82,11 +82,34 @@ data Field (name :: Symbol) (fieldType :: Type)
8282
data Argument (name :: Symbol) (argType :: Type)
8383

8484

85-
-- Can't set the value for default arguments via types, but can
86-
-- distinguish to force users to provide a default argument somewhere
87-
-- in their function (using Maybe? ore some new type like
88-
-- https://hackage.haskell.org/package/optional-args-1.0.1)
89-
data DefaultArgument (name :: Symbol) (argType :: Type)
85+
-- | Specify a default value for a type in a GraphQL schema.
86+
--
87+
-- GraphQL schema can have default values in certain places. For example,
88+
-- arguments to fields can have default values. Because we cannot lift
89+
-- arbitrary values to the type level, we need some way of getting at those
90+
-- values. This typeclass provides the means.
91+
--
92+
-- To specify a default, implement this typeclass.
93+
--
94+
-- The default implementation is to say that there *is* no default for this
95+
-- type.
96+
class Defaultable a where
97+
-- | defaultFor returns the value to be used when no value has been given.
98+
defaultFor :: Name -> Maybe a
99+
defaultFor _ = empty
100+
101+
instance Defaultable Int32
102+
103+
instance Defaultable Double
104+
105+
instance Defaultable Bool
106+
107+
instance Defaultable Text
108+
109+
instance Defaultable (Maybe a) where
110+
-- | The default for @Maybe a@ is @Nothing@.
111+
defaultFor _ = pure Nothing
112+
90113

91114
cons :: a -> [a] -> [a]
92115
cons = (:)
@@ -316,7 +339,7 @@ instance forall dataName consName records s l p.
316339
. Schema.NonNullTypeNamed
317340
. Schema.DefinedInputType
318341
. Schema.InputTypeDefinitionObject
319-
. (Schema.InputObjectTypeDefinition name)
342+
. Schema.InputObjectTypeDefinition name
320343
. Schema.NonEmptyList
321344
) (genericGetInputObjectFieldDefinitions @records)
322345

src/GraphQL/Resolver.hs

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ module GraphQL.Resolver
1717
( ResolverError(..)
1818
, HasResolver(..)
1919
, (:<>)(..)
20-
, Defaultable(..)
2120
, Result(..)
2221
, unionValue
2322
) where
@@ -146,38 +145,10 @@ class HasResolver m a where
146145
type Handler m a
147146
resolve :: Handler m a -> Maybe (SelectionSetByType Value) -> m (Result Value)
148147

149-
-- | Specify a default value for a type in a GraphQL schema.
150-
--
151-
-- GraphQL schema can have default values in certain places. For example,
152-
-- arguments to fields can have default values. Because we cannot lift
153-
-- arbitrary values to the type level, we need some way of getting at those
154-
-- values. This typeclass provides the means.
155-
--
156-
-- To specify a default, implement this typeclass.
157-
--
158-
-- The default implementation is to say that there *is* no default for this
159-
-- type.
160-
class Defaultable a where
161-
-- | defaultFor returns the value to be used when no value has been given.
162-
defaultFor :: Name -> Maybe a
163-
defaultFor _ = empty
164-
165148
-- | Called when the schema expects an input argument @name@ of type @a@ but
166149
-- @name@ has not been provided.
167-
valueMissing :: Defaultable a => Name -> Either ResolverError a
168-
valueMissing name = maybe (Left (ValueMissing name)) Right (defaultFor name)
169-
170-
instance Defaultable Int32
171-
172-
instance Defaultable Double
173-
174-
instance Defaultable Bool
175-
176-
instance Defaultable Text
177-
178-
instance Defaultable (Maybe a) where
179-
-- | The default for @Maybe a@ is @Nothing@.
180-
defaultFor _ = pure Nothing
150+
valueMissing :: API.Defaultable a => Name -> Either ResolverError a
151+
valueMissing name = maybe (Left (ValueMissing name)) Right (API.defaultFor name)
181152

182153
instance forall m. (Applicative m) => HasResolver m Int32 where
183154
type Handler m Int32 = m Int32
@@ -287,7 +258,7 @@ instance forall ksH t f m.
287258
( KnownSymbol ksH
288259
, BuildFieldResolver m f
289260
, FromValue t
290-
, Defaultable t
261+
, API.Defaultable t
291262
, HasAnnotatedInputType t
292263
, Monad m
293264
) => BuildFieldResolver m (PlainArgument (API.Argument ksH t) f) where
@@ -303,7 +274,7 @@ instance forall ksK t f m name.
303274
( KnownSymbol ksK
304275
, BuildFieldResolver m f
305276
, KnownSymbol name
306-
, Defaultable t
277+
, API.Defaultable t
307278
, API.GraphQLEnum t
308279
, Monad m
309280
) => BuildFieldResolver m (EnumArgument (API.Argument ksK (API.Enum name t)) f) where

tests/ExampleSchema.hs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,8 @@ import GraphQL.API
8585
, Interface
8686
, Union
8787
, (:>)
88+
, Defaultable(..)
8889
)
89-
-- XXX: This really shouldn't be part of Resolver, since whether or not a
90-
-- thing has a default is part of the API / Schema definition.
91-
import GraphQL.Resolver (Defaultable(..))
9290
import GraphQL.Value (pattern ValueEnum, unName)
9391
import GraphQL.Value.ToValue (ToValue(..))
9492

tests/Examples/InputObject.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Protolude hiding (Enum)
77

88
import GraphQL
99
import GraphQL.API
10-
import GraphQL.Resolver (Handler, Defaultable(..))
10+
import GraphQL.Resolver (Handler)
1111
import GraphQL.Value.FromValue (FromValue)
1212

1313
data DogStuff = DogStuff { toy :: Text, likesTreats :: Bool } deriving (Show, Generic)

0 commit comments

Comments
 (0)