absinthe-graphql / absinthe_phoenix

MIT License
309 stars 82 forks source link

How to handle missing data? #27

Open zimt28 opened 6 years ago

zimt28 commented 6 years ago

Using Phoenix alone I handled missing data ("not found") by just using Ecto's ! functions (one! etc.). These raise an Ecto.NoResultsError, which Phoenix can handle via a Plug.Exception implementation to show a 404 page.

I'm now changing my app to use Absinthe.Phoenix.Controller and would like to know what's the best way to handle these cases.

My current approach looks like this, but it's quite repetitive. I'm using the new action_fallback macro from Phoenix 1.3.

  @graphql """
  query ($id: ID!) {
    file(id: $id) @put {
      owner
    }
  }
  """
  def show(conn, %{data: %{file: nil}}), do: {:error, :not_found}
  def show(conn, %{data: data}) do
    render(conn, "show.html", file: data.file)
  end

Is there a better/ suggested way to do this properly?

zimt28 commented 6 years ago

Just a thought: Maybe it's possible to build a directive, so that something which has been configured gets returned/ raised?

Just like

  @graphql """
  query ($id: ID!) {
    file(id: $id) @required @put {
      owner
    }
  }
  """