-
Notifications
You must be signed in to change notification settings - Fork 33
Allow Handlers to return errors. #224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Prior to this change, the graphql-api internals could generate errors but the user-supplied Handler could not. This adds the ability for the user-supplied Handler to also return an error. 1. Added a HandlerError constructor to the ResolverError to represent errors generated by the handler. 2. Updated the resolve to return a 'HandlerResult a' which is now a typedef for 'Either Text a'. This could not be done as a 'Union' type because the 'Left err' value should have a different effect on the results (null in 'data' and adding to 'errors'). 3. Updated all the HasResolver class instances for the change in haskell-graphql#2. 4. Added 'returns' and 'handlerError' convenience functions for handlers to abstract the internals of the success/failure implementation. 5. Enhanced the tests (particularly ResolverSpec) for testing the generation of success and error results for different handler types. This change is *not* backward compatible---and cannot be because the handlers now need the ability to signal errors. However, all existing handlers which perforce return successful values can be updated rather simply using the new 'returns' helper for each handler field instead of the previous 'pure' function: miniCat name = pure (pure name :<> pure 32) becomes: miniCat name = pure (returns name :<> returns 32) And: handler :: Handler IO Query handler = pure $ \fooId -> do foo <- lookupFoo fooId -- note that fmap maps over the Maybe, so we still need -- have to wrap the result in a pure. sequence $ fmap (pure . viewFoo) foo becomes: handler :: Handler IO Query handler = pure $ \fooId -> do foo <- lookupFoo fooId returns $ viewFoo <$> foo The current reported package version of 0.3.0 should be changed to at least 0.4.0 to indicate a breaking, non-backward-compatible change.
|
Addresses issue #172, largely using the method recommended by @harendra-kumar. |
|
Also note that this is still not completely compliant with the GraphQL specification, which states:
This is not currently supported by the existed per-resolver error handling (e.g. |
|
Thank you, I think this is a great addition! The test failure is a simple test ratchet change in this file: https://github.com/haskell-graphql/graphql-api/blob/master/scripts/hpc-ratchet#L37 |
|
Thanks, @teh! Are there any plans for a hackage release in the near future? |
|
Hey, I pushed http://hackage.haskell.org/package/graphql-api-0.4.0 - please test and let me know if you have any issues! |
With the merged pull request haskell-graphql#224, version 0.4.0 allows handlers to return errors. This change requires the use of a different function in the handler. This file change updates the README.md file to reflect this updated version.
Update README.md with changes from #224
Prior to this change, the graphql-api internals could generate errors
but the user-supplied Handler could not. This adds the ability for
the user-supplied Handler to also return an error.
Added a HandlerError constructor to the ResolverError to represent
errors generated by the handler.
Updated the resolve to return a 'HandlerResult a' which is now a
typedef for 'Either Text a'. This could not be done as a 'Union'
type because the 'Left err' value should have a different effect on
the results (null in 'data' and adding to 'errors').
Updated all the HasResolver class instances for the change in WIP: Basic servant like api #2.
Added 'returns' and 'handlerError' convenience functions for
handlers to abstract the internals of the success/failure
implementation.
Enhanced the tests (particularly ResolverSpec) for testing the
generation of success and error results for different handler
types.
This change is not backward compatible---and cannot be because the
handlers now need the ability to signal errors. However, all existing
handlers which perforce return successful values can be updated rather
simply using the new 'returns' helper for each handler field instead
of the previous 'pure' function:
becomes:
And:
becomes:
The current reported package version of 0.3.0 should be changed to at
least 0.4.0 to indicate a breaking, non-backward-compatible change.