Add nullable types

This commit is contained in:
Eugen Wissner 2019-07-03 17:54:50 +02:00
parent 91679650b5
commit 6238b2fbfa
4 changed files with 63 additions and 47 deletions

View File

@ -6,16 +6,16 @@ module Data.GraphQL.Schema
( Schema ( Schema
, Resolver , Resolver
, Subs , Subs
, nullableArray
, nullableEnum
, nullableObject
, nullableScalar
, object , object
, object'
, objectA , objectA
, objectA'
, scalar , scalar
, scalarA , scalarA
, array , array
, array'
, arrayA , arrayA
, arrayA'
, enum , enum
, enumA , enumA
, resolve , resolve
@ -68,18 +68,16 @@ objectA :: MonadPlus m
=> Name -> (Arguments -> ActionT m [Resolver m]) -> Resolver m => Name -> (Arguments -> ActionT m [Resolver m]) -> Resolver m
objectA name f = Resolver name $ resolveFieldValue f resolveRight objectA name f = Resolver name $ resolveFieldValue f resolveRight
where where
resolveRight fld@(Field _ _ _ flds) resolver = withField name (resolve resolver flds) fld resolveRight fld@(Field _ _ _ flds) resolver = withField (resolve resolver flds) fld
-- | Create a named 'Resolver' from a list of 'Resolver's. -- | Like 'object' but can be null.
object' :: MonadPlus m => Name -> ActionT m [Resolver m] -> Resolver m nullableObject :: MonadPlus m
object' name = objectA' name . const => Name -> (Arguments -> ActionT m (Maybe [Resolver m])) -> Resolver m
nullableObject name f = Resolver name $ resolveFieldValue f resolveRight
-- | Like 'object'' but also taking 'Argument's.
objectA' :: MonadPlus m
=> Name -> (Arguments -> ActionT m [Resolver m]) -> Resolver m
objectA' name f = Resolver name $ resolveFieldValue f resolveRight
where where
resolveRight fld@(Field _ _ _ flds) resolver = withField name (resolve resolver flds) fld resolveRight fld@(Field _ _ _ flds) (Just resolver) = withField (resolve resolver flds) fld
resolveRight fld Nothing
= return $ HashMap.singleton (aliasOrName fld) Aeson.Null
-- | A scalar represents a primitive value, like a string or an integer. -- | A scalar represents a primitive value, like a string or an integer.
scalar :: (MonadPlus m, Aeson.ToJSON a) => Name -> ActionT m a -> Resolver m scalar :: (MonadPlus m, Aeson.ToJSON a) => Name -> ActionT m a -> Resolver m
@ -90,9 +88,20 @@ scalarA :: (MonadPlus m, Aeson.ToJSON a)
=> Name -> (Arguments -> ActionT m a) -> Resolver m => Name -> (Arguments -> ActionT m a) -> Resolver m
scalarA name f = Resolver name $ resolveFieldValue f resolveRight scalarA name f = Resolver name $ resolveFieldValue f resolveRight
where where
resolveRight fld@(Field _ _ _ []) result = withField name (return result) fld resolveRight fld@(Field _ _ _ []) result = withField (return result) fld
resolveRight _ _ = mzero resolveRight _ _ = mzero
-- | Lika 'scalar' but can be null.
nullableScalar :: (MonadPlus m, Aeson.ToJSON a)
=> Name -> (Arguments -> ActionT m (Maybe a)) -> Resolver m
nullableScalar name f = Resolver name $ resolveFieldValue f resolveRight
where
resolveRight fld@(Field _ _ _ []) (Just result) = withField (return result) fld
resolveRight fld Nothing
= return $ HashMap.singleton (aliasOrName fld) Aeson.Null
resolveRight _ _ = mzero
-- | Creates a list of 'Resolver's.
array :: MonadPlus m => Name -> ActionT m [[Resolver m]] -> Resolver m array :: MonadPlus m => Name -> ActionT m [[Resolver m]] -> Resolver m
array name = arrayA name . const array name = arrayA name . const
@ -102,19 +111,17 @@ arrayA :: MonadPlus m
arrayA name f = Resolver name $ resolveFieldValue f resolveRight arrayA name f = Resolver name $ resolveFieldValue f resolveRight
where where
resolveRight fld@(Field _ _ _ sels) resolver resolveRight fld@(Field _ _ _ sels) resolver
= withField name (traverse (`resolve` sels) resolver) fld = withField (traverse (`resolve` sels) resolver) fld
-- | Like 'object'' but taking lists of 'Resolver's instead of a single list. -- | Like 'array' but can be null.
array' :: MonadPlus m => Name -> ActionT m [[Resolver m]] -> Resolver m nullableArray :: MonadPlus m
array' name = arrayA' name . const => Name -> (Arguments -> ActionT m (Maybe [[Resolver m]])) -> Resolver m
nullableArray name f = Resolver name $ resolveFieldValue f resolveRight
-- | Like 'array'' but also taking 'Argument's.
arrayA' :: MonadPlus m
=> Name -> (Arguments -> ActionT m [[Resolver m]]) -> Resolver m
arrayA' name f = Resolver name $ resolveFieldValue f resolveRight
where where
resolveRight fld@(Field _ _ _ sels) resolver resolveRight fld@(Field _ _ _ sels) (Just resolver)
= withField name (traverse (`resolve` sels) resolver) fld = withField (traverse (`resolve` sels) resolver) fld
resolveRight fld Nothing
= return $ HashMap.singleton (aliasOrName fld) Aeson.Null
-- | Represents one of a finite set of possible values. -- | Represents one of a finite set of possible values.
-- Used in place of a 'scalar' when the possible responses are easily enumerable. -- Used in place of a 'scalar' when the possible responses are easily enumerable.
@ -125,7 +132,16 @@ enum name = enumA name . const
enumA :: MonadPlus m => Name -> (Arguments -> ActionT m [Text]) -> Resolver m enumA :: MonadPlus m => Name -> (Arguments -> ActionT m [Text]) -> Resolver m
enumA name f = Resolver name $ resolveFieldValue f resolveRight enumA name f = Resolver name $ resolveFieldValue f resolveRight
where where
resolveRight fld resolver = withField name (return resolver) fld resolveRight fld resolver = withField (return resolver) fld
-- | Like 'array'' but also taking 'Argument's.
nullableEnum :: MonadPlus m
=> Name -> (Arguments -> ActionT m (Maybe [Text])) -> Resolver m
nullableEnum name f = Resolver name $ resolveFieldValue f resolveRight
where
resolveRight fld (Just resolver) = withField (return resolver) fld
resolveRight fld Nothing
= return $ HashMap.singleton (aliasOrName fld) Aeson.Null
resolveFieldValue :: MonadPlus m resolveFieldValue :: MonadPlus m
=> ([Argument] -> ActionT m a) => ([Argument] -> ActionT m a)
@ -138,17 +154,13 @@ resolveFieldValue f resolveRight fld@(Field alias name args _) = do
where where
resolveLeft err = do resolveLeft err = do
_ <- addErrMsg err _ <- addErrMsg err
return $ HashMap.singleton (fromMaybe name alias) Aeson.Null return $ HashMap.singleton (aliasOrName fld) Aeson.Null
-- | Helper function to facilitate 'Argument' handling. -- | Helper function to facilitate 'Argument' handling.
withField :: (MonadPlus m, Aeson.ToJSON a) withField :: (MonadPlus m, Aeson.ToJSON a)
=> Name -> CollectErrsT m a -> Field -> CollectErrsT m (HashMap Text Aeson.Value) => CollectErrsT m a -> Field -> CollectErrsT m (HashMap Text Aeson.Value)
withField name v (Field alias _ _ _) withField v fld
= HashMap.singleton aliasOrName . Aeson.toJSON <$> runAppendErrs v = HashMap.singleton (aliasOrName fld) . Aeson.toJSON <$> runAppendErrs v
{- TODO: Report error when Non-Nullable type for field argument.
else return (HashMap.singleton aliasOrName Aeson.Null) -}
where
aliasOrName = fromMaybe name alias
-- | Takes a list of 'Resolver's and a list of 'Field's and applies each -- | Takes a list of 'Resolver's and a list of 'Field's and applies each
-- 'Resolver' to each 'Field'. Resolves into a value containing the -- 'Resolver' to each 'Field'. Resolves into a value containing the
@ -160,8 +172,9 @@ resolve resolvers = fmap (Aeson.toJSON . fold) . traverse tryResolvers
tryResolvers fld = mplus (maybe mzero (tryResolver fld) $ find (compareResolvers fld) resolvers) $ errmsg fld tryResolvers fld = mplus (maybe mzero (tryResolver fld) $ find (compareResolvers fld) resolvers) $ errmsg fld
compareResolvers (Field _ name _ _) (Resolver name' _) = name == name' compareResolvers (Field _ name _ _) (Resolver name' _) = name == name'
tryResolver fld (Resolver _ resolver) = resolver fld tryResolver fld (Resolver _ resolver) = resolver fld
errmsg (Field alias name _ _) = do errmsg fld@(Field alias name _ _) = do
addErrMsg $ T.unwords ["field", name, "not resolved."] addErrMsg $ T.unwords ["field", name, "not resolved."]
return $ HashMap.singleton aliasOrName Aeson.Null return $ HashMap.singleton (aliasOrName fld) Aeson.Null
where
aliasOrName = fromMaybe name alias aliasOrName :: Field -> Text
aliasOrName (Field alias name _ _) = fromMaybe name alias

View File

@ -1,4 +1,4 @@
resolver: lts-13.26 resolver: lts-13.27
packages: packages:
- '.' - '.'
extra-deps: [] extra-deps: []

View File

@ -6,7 +6,7 @@
packages: [] packages: []
snapshots: snapshots:
- completed: - completed:
size: 499889 size: 500539
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/13/26.yaml url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/13/27.yaml
sha256: ecb02ee16829df8d7219e7d7fe6c310819820bf335b0b9534bce84d3ea896684 sha256: 690db832392afe55733b4c7023fd29b1b1c660ee42f1fb505b86b07394ca994e
original: lts-13.26 original: lts-13.27

View File

@ -24,15 +24,18 @@ schema = hero :| [human, droid]
hero :: MonadPlus m => Resolver m hero :: MonadPlus m => Resolver m
hero = Schema.objectA "hero" $ \case hero = Schema.objectA "hero" $ \case
[] -> character artoo [] -> character artoo
[Argument "episode" (ValueInt n)] -> character . getHero $ fromIntegral n
[Argument "episode" (ValueEnum "NEWHOPE")] -> character $ getHero 4 [Argument "episode" (ValueEnum "NEWHOPE")] -> character $ getHero 4
[Argument "episode" (ValueEnum "EMPIRE" )] -> character $ getHero 5 [Argument "episode" (ValueEnum "EMPIRE" )] -> character $ getHero 5
[Argument "episode" (ValueEnum "JEDI" )] -> character $ getHero 6 [Argument "episode" (ValueEnum "JEDI" )] -> character $ getHero 6
_ -> ActionT $ throwE "Invalid arguments." _ -> ActionT $ throwE "Invalid arguments."
human :: MonadPlus m => Resolver m human :: MonadPlus m => Resolver m
human = Schema.objectA "human" $ \case human = Schema.nullableObject "human" $ \case
[Argument "id" (ValueString i)] -> character =<< lift (getHuman i) [Argument "id" (ValueString i)] -> do
humanCharacter <- lift $ return $ getHuman i >>= Just
case humanCharacter of
Nothing -> return Nothing
Just e -> Just <$> character e
_ -> ActionT $ throwE "Invalid arguments." _ -> ActionT $ throwE "Invalid arguments."
droid :: MonadPlus m => Resolver m droid :: MonadPlus m => Resolver m