<p align="center">
<img src="https://user-images.githubusercontent.com/464900/56933576-702b7880-6ab6-11e9-8b09-d86de46d2edc.png" width="600" />
<br /><br />
Bridges the gap between <a target="_blank" href="https://hexdocs.pm/ecto/Ecto.html">Ecto</a> and <a target="_blank" href="http://absinthe-graphql.org/">Absinthe GraphQL</a><br /> by listing validation messages in a mutation payload.
<br /><br />
<a href="https://travis-ci.com/mirego/absinthe_error_payload"><img src="https://travis-ci.com/mirego/absinthe_error_payload.svg?branch=master" /></a>
<a href="https://coveralls.io/github/mirego/absinthe_error_payload?branch=master"><img src="https://coveralls.io/repos/github/mirego/absinthe_error_payload/badge.svg?branch=master" /></a>
<a href="https://hex.pm/packages/absinthe_error_payload"><img src="https://img.shields.io/hexpm/v/absinthe_error_payload.svg" /></a>
</p>
The primary philosophy is that messages generated by invalid and/or unexpected user input are DATA, and should be returned as such.
On the other hand, errors made in using an API - like querying a field that doesn’t exist -, are actually ERRORS and should be returned as errors.
GraphQL fits perfectly here. Error are handled explicitly in the query and actual errors are treated as internal error on client side library.
## Installation
Install by adding `absinthe_error_payload` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:absinthe_error_payload, "~> 1.0"}
]
end
```
## Usage
The best place to find usage examples is by looking at the test cases at the [AbsintheErrorPayload GitHub](https://github.com/mirego/absinthe_error_payload) or the docs for [AbsintheErrorPayload.Payload](https://hexdocs.pm/absinthe_error_payload/AbsintheErrorPayload.Payload.html#content)
Here’s a quick summary of what AbsintheErrorPayload includes
[AbsintheErrorPayload.ValidationMessage](https://hexdocs.pm/absinthe_error_payload/AbsintheErrorPayload.ValidationMessage.html#content) structs/objects are created with all the information you’d normally be able to access through [Ecto.Changeset.traverse_errors/2](https://hexdocs.pm/ecto/Ecto.Changeset.html#traverse_errors/2).
AbsintheErrorPayload includes a [schema](https://hexdocs.pm/absinthe_error_payload/AbsintheErrorPayload.ValidationMessageTypes.html#content) definition to add ValidationMessages to your schema.
[AbsintheErrorPayload.Payload](https://hexdocs.pm/absinthe_error_payload/AbsintheErrorPayload.Payload.html#content) is middleware that takes your resolver output (either an updated object or a changeset with errors) and converts it into a AbsintheErrorPayload Mutation Response (aka Payload).
Payloads have three fields
- `successful` - Indicates if the mutation completed successfully or not. Boolean.
- `messages` - a list of validation errors. Always empty on success
- `result` - the data object that was created/updated/deleted on success. Always nil when unsuccessful.
Finally [AbsintheErrorPayload.TestHelper](https://hexdocs.pm/absinthe_error_payload/AbsintheErrorPayload.TestHelper.html#content) has some helper methods that make it easier to write ExUnit tests against your schema.
## Hey, this looks like Kronky!
This library has been forked from [Kronky](https://github.com/Ethelo/kronky), renamed to `AbsintheErrorPayload` and published on `hexpm`.
Kronky was not being updated and while it still worked for older versions, a more maintained release was requested by many users.
Thank you [Ethelo](http://ethelo.com/) for the awesome library!
## License
AbsintheErrorPayload is © 2019 [Mirego](https://www.mirego.com) and may be freely distributed under the [New BSD license](http://opensource.org/licenses/BSD-3-Clause). See the [`LICENSE.md`](https://github.com/mirego/absinthe_error_payload/blob/master/LICENSE.md) file.
As mentionned above, AbsintheErrorPayload is based on [Kronky](https://github.com/Ethelo/kronky) which is licensed under the [MIT license](http://opensource.org/licenses/MIT). See the [`KRONKY_LICENSE.md`](https://github.com/mirego/absinthe_error_payload/blob/master/KRONKY_LICENSE.md) file.
The flask logo is based on [this lovely icon by Danil Polshin](https://thenounproject.com/term/flask/1428207), from The Noun Project. Used under a [Creative Commons BY 3.0](http://creativecommons.org/licenses/by/3.0/) license.
## About Mirego
[Mirego](https://www.mirego.com) is a team of passionate people who believe that work is a place where you can innovate and have fun. We’re a team of [talented people](https://life.mirego.com) who imagine and build beautiful Web and mobile applications. We come together to share ideas and [change the world](http://www.mirego.org).
We also [love open-source software](https://open.mirego.com) and we try to give back to the community as much as we can.