@@ -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
3737import GHC.TypeLits (Symbol , KnownSymbol , TypeError , ErrorMessage (.. ))
3838import qualified GraphQL.Internal.Schema as Schema
39- import GraphQL.Internal.Name (NameError , nameFromSymbol )
39+ import GraphQL.Internal.Name (Name , NameError , nameFromSymbol )
4040import GraphQL.API.Enum (GraphQLEnum (.. ))
4141import GHC.Generics ((:*:) (.. ))
4242import GHC.Types (Type )
@@ -82,11 +82,34 @@ data Field (name :: Symbol) (fieldType :: Type)
8282data 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
91114cons :: a -> [a ] -> [a ]
92115cons = (:)
@@ -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
0 commit comments