Permit.Absinthe.Middleware (permit_absinthe v0.3.0)

Copy Markdown View Source

Middleware for loading and authorizing resources in Absinthe. Uses the raw resolver function defined in Permit.Absinthe.Resolvers.LoadAndAuthorize to put the resolution outcome in :loaded_resource or :loaded_resources (depending on whether it's an index-like or a single-item action).

Useful in mutations where it's not enough to just load a resource, or whenever you otherwise need to process it after loading.

Usage, mechanism and configuration

See Permit.Absinthe.Resolvers.LoadAndAuthorize for details on resolution of authorized records in Permit.Absinthe. The middleware delegates

Example

mutation do
  @desc "Update an article"
  field :update_article, :article do
    permit action: :update

    arg(:id, non_null(:id))
    arg(:name, non_null(:string))
    arg(:content, non_null(:string))

    middleware Permit.Absinthe.Middleware

    resolve(fn _, %{name: name, content: content}, %{context: context} ->
      case Blog.Content.update_article(context.loaded_resource, %{name: name, content: content}) do
        {:ok, article} ->
          {:ok, article}

        {:error, changeset} ->
          {:error, "Could not update article"}
      end
    end)
  end
end