Skip to content

Commit

Permalink
Merge 5df9529 into 4e07fe6
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielpra1 committed Jul 23, 2019
2 parents 4e07fe6 + 5df9529 commit fdcbf63
Show file tree
Hide file tree
Showing 26 changed files with 1,260 additions and 665 deletions.
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: elixir
elixir:
- 1.9.0
otp_release:
- 22.0
services:
- postgresql
before_script:
- cp config/db.travis.exs config/db.secret.exs
script:
- MIX_ENV=test mix coveralls.travis
91 changes: 88 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Crudry

[![Coverage Status](https://coveralls.io/repos/github/gabrielpra1/crudry/badge.svg?branch=master)](https://coveralls.io/github/gabrielpra1/crudry?branch=master)

Crudry is an elixir library for DRYing CRUD of Phoenix Contexts and Absinthe Resolvers.

It also provides a simple middleware for translating changeset errors into readable messages.
Expand All @@ -20,6 +22,89 @@ end

## Usage

* [Context Generation](https://hexdocs.pm/crudry/Crudry.Context.html#module-usage)
* [Resolver Generation](https://hexdocs.pm/crudry/Crudry.Resolver.html#module-usage)
* [Middleware](https://hexdocs.pm/crudry/Crudry.Middlewares.HandleChangesetErrors.html)
### Context Generation

To generate CRUD functions for a given schema in your context, simply do:

```elixir
defmodule MyApp.MyContext do
alias MyApp.Repo

require Crudry.Context
Crudry.Context.generate_functions MyApp.MySchema
end
```

To see the functions that are generated and custom options, refer to the [Crudry.Context docs](https://hexdocs.pm/crudry/Crudry.Context.html#module-usage).

### Resolver Generation

With the context all set up, the resolver is ready to be generated:

```elixir
defmodule MyApp.MyResolver do
alias MyApp.Repo

require Crudry.Resolver
Crudry.Resolver.generate_functions MyApp.MyContext, MyApp.MySchema
end
```

To see the functions that are generated and custom options, refer to the [Crudry.Resolver docs](https://hexdocs.pm/crudry/Crudry.Resolver.html#module-usage).


### Translate Errors middleware

Absinthe Middleware to translate errors and changeset errors into human readable messages using [Gettext](https://github.com/elixir-lang/gettext).

The `create`, `update` and `delete` functions in the resolver all return `Ecto.Changeset` as errors, so it's useful to translate them into human readable messages.

Crudry provides an `Absinthe.Middleware` to help with that, handling nested changeset errors. Just add it as a middleware to your mutations:

```elxixir
field :create_user, :user do
arg :params, non_null(:user_params)
resolve &UsersResolver.create_user/2
middleware Crudry.Middlewares.TranslateErrors
end
```

Or define it for all queries and mutations, using [middleware/3](https://hexdocs.pm/absinthe/Absinthe.Middleware.html#module-object-wide-authentication):

```elixir
alias Crudry.Middlewares.TranslateErrors

def middleware(middleware, _field, _object) do
middleware ++ [TranslateErrors]
end
```

`Cudry.Translator` is used by default to translate error messages to the default locale `en`. You can also use your own Gettext module by adding it to your Absinthe's schema `context/1` function:

```elixir
def context(context) do
Map.put(context, :translator, MyAppWeb.Gettext)
end
```

Or just override the default locale in your [Context Plug](https://hexdocs.pm/absinthe/context-and-authentication.html#context-and-plugs):

```elixir
def call(conn, _) do
Absinthe.Plug.put_options(conn, context: %{locale: "pt_BR"})
end
```

Refer to the [TranslateErrors docs](https://hexdocs.pm/crudry/Crudry.Middlewares.TranslateErrors.html) for more information.

## Related Projects

* [Rajska](https://github.com/rschef/rajska) is an elixir authorization library for Absinthe.
* [Uploadex](https://github.com/gabrielpra1/uploadex) is an elixir library for handling uploads using Ecto and Arc

## License

MIT License.

See [LICENSE](./LICENSE) for more information.
5 changes: 5 additions & 0 deletions config/db.travis.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use Mix.Config

config :crudry, Crudry.Repo,
username: "postgres",
password: ""
Loading

0 comments on commit fdcbf63

Please sign in to comment.