View Source CoseDellaVitaEx.Helpers (CoseDellaVitaEx v0.0.4)

Simple helpers that reduce duplicate work for stuff like settign the success field on mutations or adding errors in the correct structure.

Link to this section Summary

Functions

Translate changeset errors into CoseDellaVitaEx.Errors.* structs that are translated into specific, typed GraphQL data-errors. Unrecognized errors default to CoseDellaVitaEx.Errors.GenericError. The :errors field is added to the response if it does not exist. Supports nested changeset errors, which are added with flattened keys, for example "user.posts". Keys are converted to camelCase when they are added to the errors field. Additionally, it is possible to override fields of the error structs, matching on the struct's :error_type field and the error's path.

Add an :error-type error to a data response (so the error will not end up in the top-level errors).

Sets the :success flag of a response to true if there are no errors, and adds an empty error list if there is no errors field.

Link to this section Functions

Link to this function

add_changeset_errors(response, changeset, error_mapper, base_path \\ [], error_field_overrides \\ %{})

View Source
@spec add_changeset_errors(
  map(),
  Ecto.Changeset.t(),
  (String.t(), map() -> struct()),
  [String.t()],
  map()
) :: map()

Translate changeset errors into CoseDellaVitaEx.Errors.* structs that are translated into specific, typed GraphQL data-errors. Unrecognized errors default to CoseDellaVitaEx.Errors.GenericError. The :errors field is added to the response if it does not exist. Supports nested changeset errors, which are added with flattened keys, for example "user.posts". Keys are converted to camelCase when they are added to the errors field. Additionally, it is possible to override fields of the error structs, matching on the struct's :error_type field and the error's path.

examples-doctests

Examples / doctests

# adds to existing errors
iex> add_changeset_errors(%{errors: ["boom"]}, TestSchema.changeset(%{}), &TestErrorMapper.map/2)
%{errors: ["boom", %RequiredError{message: "This field is required.", path: ["user"], error_type: :required_error}]}

# adds :errors field
# supports nested changeset errors
# supports multiple errors for the same field
# transforms field names to camelCase
iex> cs = %{user: %{email: "ab", bi_cycles: [%{wheel_count: 1}]}, something: 3} |> TestSchema.changeset()
iex> error_overrides = %{{:generic_error, ~w(input something)} => %{message: "overriden message!"}}
iex> add_changeset_errors(%{}, cs, &TestErrorMapper.map/2, ~w(input), error_overrides)
%{
  errors: [
    %LengthError{comparison_type: :max, error_type: :length_error, message: "should be at most 1 character(s)", path: ["input", "user", "email"], reference: 1},
    %NumberError{comparison_type: :greater_than, error_type: :number_error, message: "must be greater than 3", path: ["input", "user", "biCycles", "wheelCount"], reference: 3.0},
    %GenericError{message: "overriden message!", path: ["input", "something"], error_type: :generic_error}
  ]
}

# custom validation
iex> cs = %{user: %{email: "a", bicycles: %{wheel_count: 4}}} |> TestSchema.changeset()
iex> cs = cs |> add_error(:user, "something", custom_validation: :something)
iex> add_changeset_errors(%{}, cs, &TestErrorMapper.map/2)
%{errors: [%TestError{error_type: :test_error, message: "something", path: ["user"]}]}
Link to this function

add_data_error(response \\ %{}, error)

View Source
@spec add_data_error(map(), map() | keyword()) :: map()

Add an :error-type error to a data response (so the error will not end up in the top-level errors).

examples-doctests

Examples / doctests

iex> add_data_error(message: "does not exist", path: [])
%{errors: [%{message: "does not exist", path: []}]}
iex> add_data_error(%{status: :error}, message: "does not exist", path: [])
%{status: :error, errors: [%{message: "does not exist", path: []}]}
Link to this function

format_success_errors(response_tuple)

View Source
@spec format_success_errors({atom(), map()}) :: {atom(), map()}

Sets the :success flag of a response to true if there are no errors, and adds an empty error list if there is no errors field.

examples-doctests

Examples / doctests

iex> {:ok, %{}} |> format_success_errors()
{:ok, %{success: true, errors: []}}
iex> {:ok, %{errors: []}} |> format_success_errors()
{:ok, %{success: true, errors: []}}
iex> {:ok, %{errors: ["boom"]}} |> format_success_errors()
{:ok, %{success: false, errors: ["boom"]}}
iex> {:error, %{}} |> format_success_errors()
{:error, %{}}