Skip to content

Because Mr. Ramazotti and Ms. Turner love GraphQL and Ecto. Or they should.

Notifications You must be signed in to change notification settings

weareyipyip/CoseDellaVitaEx

Repository files navigation

CoseDellaVitaEx

Generic helpers for GraphQL API's.

Setup

  1. Add to your dependencies:
    def deps do
      [
        {:cose_della_vita_ex, "~> 0.0.3"}
      ]
    end
  2. Include the generic GraphQL types in your schema:
    defmodule MyApp.Schema do
      use Absinthe.Schema
      use CoseDellaVitaEx.Schema
    
      # Your own types here
    end
  3. Create an error mapper:
    defmodule MyApp.ErrorMapper do
      alias CoseDellaVitaEx.ErrorMapper
    
      @behaviour ErrorMapper
    
      def map(%{custom_validation: :my_validation}, message): %MyError{message: message}
      # Add more custom errors here, then fall back to the included errors
      def map(opts, message), do: ErrorMapper.map_default(opts, message)
    end

Error handling

Convert Ecto changeset errors to GraphQL types.

  1. Define possible errors for your mutation, so the client can match on those: The generic_error type will be used as a fallback
    defmodule MyApp.MyTypes
      alias CoseDellaVitaEx.Types.ErrorTypes
    
      @my_mutation_error_types [:my_error]
    
      union :my_mutation_error do
        types(unquote(@my_mutation_error_types))
        resolve_type(&ErrorTypes.resolve_error_type(&1, &2, @my_mutation_error_types))
      end
    
      object :my_mutation_result do
        field(:success, :boolean)
        field(:errors, list_of(:my_mutation_error))
      end
    end

Or if you do not have any specific errors to resolve you can also use the base mutation type:

defmodule MyApp.MyTypes
  object :my_mutation_result do
    import_fields(:base_mutation_payload)
  end
end
  1. Automatically convert Ecto changeset errors in your resolver:
    defmodule MyApp.MyResolver do
      import CoseDellaVitaEx.Helpers
    
      alias MyApp.ErrorMapper
    
      def create(_parent, _fields, _resolution) do
        with {:ok, my_entity} <- MyDB.insert() do
          {:ok, %{my_entity: my_entity}}
        else
          {:error, %Ecto.Changeset{} = changeset} ->
            {:ok, add_changeset_errors(%{}, changeset, &ErrorMapper.map/2)}
        end
        |> format_success_errors()
      end
    end

About

Because Mr. Ramazotti and Ms. Turner love GraphQL and Ecto. Or they should.

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages