This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Your actual dynamic worker | |
defmodule Awesome.Worker do | |
use GenServer, restart: :transient | |
alias Awesome.Hero | |
# Client | |
def start_link(%Hero{} = hero), do: | |
GenServer.start_link(@mod, hero, name: proc_name(hero)) | |
defp proc_name(%Hero{name: name}), do: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def from_packages_list(data: str) -> Generator: | |
"""Parses CRAN package metadata from | |
https://cran.r-project.org/src/contrib/PACKAGES | |
and returns the list of dictionaries. | |
Args: | |
data (str): raw text from the package list | |
Returns: | |
(Generator): each entry from packages as dictionary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def process_errors(%Changeset{} = changeset) do | |
%{ | |
message: "Changeset errors occurred", | |
code: :schema_errors, | |
errors: to_api_errors(changeset) | |
} | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# add this to your schema module | |
# if it's a field for the mutation object, add this middleware to the end | |
def middleware(middleware, _field, %{identifier: :mutation}) do | |
middleware ++ [Idp.Middlewares.HandleAPIErrors] | |
end | |
# if it's any other object keep things as is | |
def middleware(middleware, _field, _object), do: middleware |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Idp.Middlewares.HandleAPIErrors do | |
@behaviour Absinthe.Middleware | |
alias Idp.EctoHelpers | |
def call(resolution, _config) do | |
errors = | |
resolution | |
|> Map.get(:errors) | |
|> Enum.flat_map(&process_errors/1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def action_wrapped(fun) do | |
case fun.() do | |
{:ok, result} -> | |
{:ok, result} | |
{:error, changeset = %Changeset{}} -> | |
{ | |
:error, | |
%{ | |
message: "Changeset errors occurred", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule IdpWeb.Schema.CityResolvers do | |
use IdpWeb.Schema.Errors | |
alias Idp.Geo.Cities | |
alias Idp.EctoHelpers | |
def create(_parent, city, _ctx) do | |
EctoHelpers.action_wrapped(fn -> | |
Cities.create_city(city) | |
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule IdpWeb.Schema.Errors do | |
@moduledoc """ | |
Defines common GraphQL error responses | |
""" | |
defmacro __using__(_) do | |
quote do | |
@permission_denied { | |
:error, | |
%{ | |
message: "Permission denied", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Idp.EctoHelpers do | |
alias Ecto.Changeset | |
@doc """ | |
Transform `%Ecto.Changeset{}` errors to a map | |
containing field name as a key on which validation | |
error happened and it's formatted message. | |
For example: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
:error, | |
%{ | |
message: "Changeset errors occurred", | |
code: :changeset_errors, | |
errors: [ | |
%{field: "error message"} | |
] | |
} | |
} |
NewerOlder