When argument is not found return null
The relevant test was restored too.
This commit is contained in:
parent
fb071210cf
commit
2b5648efda
@ -24,7 +24,7 @@ module Data.GraphQL.Schema
|
|||||||
, Value(..)
|
, Value(..)
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Control.Applicative (Alternative(empty))
|
import Control.Applicative (Alternative(empty), (<|>))
|
||||||
import Data.Foldable (fold)
|
import Data.Foldable (fold)
|
||||||
import Data.List.NonEmpty (NonEmpty)
|
import Data.List.NonEmpty (NonEmpty)
|
||||||
import Data.Maybe (fromMaybe)
|
import Data.Maybe (fromMaybe)
|
||||||
@ -66,6 +66,7 @@ objectA
|
|||||||
=> Name -> (Arguments -> Resolvers f) -> Resolver f
|
=> Name -> (Arguments -> Resolvers f) -> Resolver f
|
||||||
objectA name f fld@(Field _ _ args flds) = withField name (resolve (f args) flds) fld
|
objectA name f fld@(Field _ _ args flds) = withField name (resolve (f args) flds) fld
|
||||||
|
|
||||||
|
|
||||||
-- | Create a named 'Resolver' from a list of 'Resolver's.
|
-- | Create a named 'Resolver' from a list of 'Resolver's.
|
||||||
object' :: (Alternative f, Monad f) => Text -> f [Resolver f] -> Resolver f
|
object' :: (Alternative f, Monad f) => Text -> f [Resolver f] -> Resolver f
|
||||||
object' name resolvs = objectA' name $ \case
|
object' name resolvs = objectA' name $ \case
|
||||||
@ -136,9 +137,11 @@ enumA _ _ _ = empty
|
|||||||
withField
|
withField
|
||||||
:: (Alternative f, Aeson.ToJSON a)
|
:: (Alternative f, Aeson.ToJSON a)
|
||||||
=> Name -> f a -> Field -> f (HashMap Text Aeson.Value)
|
=> Name -> f a -> Field -> f (HashMap Text Aeson.Value)
|
||||||
withField name f (Field alias name' _ _) =
|
withField name v (Field alias name' _ _) =
|
||||||
if name == name'
|
if name == name'
|
||||||
then fmap (HashMap.singleton aliasOrName . Aeson.toJSON) f
|
then fmap (HashMap.singleton aliasOrName . Aeson.toJSON) v
|
||||||
|
-- TODO: Report error when Non-Nullable type for field argument.
|
||||||
|
<|> pure (HashMap.singleton aliasOrName Aeson.Null)
|
||||||
else empty
|
else empty
|
||||||
where
|
where
|
||||||
aliasOrName = fromMaybe name alias
|
aliasOrName = fromMaybe name alias
|
||||||
|
@ -140,24 +140,19 @@ test = testGroup "Star Wars Query Tests"
|
|||||||
$ object [ "data" .= object [
|
$ object [ "data" .= object [
|
||||||
"human" .= object [hanName]
|
"human" .= object [hanName]
|
||||||
]]
|
]]
|
||||||
-- TODO: Enable after Error handling restoration
|
, testCase "Invalid ID" . testQueryParams
|
||||||
-- , testCase "Invalid ID" . testQueryParams
|
(\v -> if v == "id"
|
||||||
-- (\v -> if v == "id"
|
then Just "Not a valid ID"
|
||||||
-- then Just "Not a valid ID"
|
else Nothing)
|
||||||
-- else Nothing)
|
[r| query humanQuery($id: String!) {
|
||||||
-- [r| query humanQuery($id: String!) {
|
human(id: $id) {
|
||||||
-- human(id: $id) {
|
name
|
||||||
-- name
|
}
|
||||||
-- }
|
}
|
||||||
-- }
|
-- The GraphQL spec specifies that an error should be reported when the
|
||||||
-- |] $ object ["data" .= object ["human" .= object ["name" .= Aeson.Null]],
|
-- type of the argument is Non-Nullable. However the equivalent test in
|
||||||
-- "errors" .= Aeson.toJSON [object ["message" .= ("field name not resolved." :: Text)]]]
|
-- `graphql-js` doesn't check for any errors.
|
||||||
-- TODO: This test is directly ported from `graphql-js`, however do we want
|
|] $ object ["data" .= object ["human" .= Aeson.Null]]
|
||||||
-- to mimic the same behavior? Is this part of the spec? Once proper
|
|
||||||
-- exceptions are implemented this test might no longer be meaningful.
|
|
||||||
-- If the same behavior needs to be replicated, should it be implemented
|
|
||||||
-- when defining the `Schema` or when executing?
|
|
||||||
-- $ object [ "data" .= object ["human" .= Aeson.Null] ]
|
|
||||||
, testCase "Luke aliased" . testQuery
|
, testCase "Luke aliased" . testQuery
|
||||||
[r| query FetchLukeAliased {
|
[r| query FetchLukeAliased {
|
||||||
luke: human(id: "1000") {
|
luke: human(id: "1000") {
|
||||||
|
Loading…
Reference in New Issue
Block a user