Permit.Absinthe.LoadAndAuthorize (permit_absinthe v0.1.0)

This module contains the load_and_authorize/2 function that can be used from within a custom resolver function, or as a resolver function in its entirety.

Summary

Functions

Resolves and authorizes a resource or list of resources.

Functions

load_and_authorize(args, resolution)

Resolves and authorizes a resource or list of resources.

This function can be used as a resolver function directly or called from a custom resolver.

Parameters

  • args - The arguments passed to the field
  • resolution - The Absinthe resolution struct

Examples

# As a resolver function
field :post, :post do
  arg :id, non_null(:id)
  resolve &load_and_authorize/2
end

# Resolver for a list of resources
field :posts, list_of(:post) do
  resolve &load_and_authorize/2
end

# From a custom resolver
def my_custom_resolver(parent, args, resolution) do
  case load_and_authorize(parent, args, resolution, :one) do
    {:ok, resource} ->
      # Do something with the authorized resource
      {:ok, transform_resource(resource)}

    error ->
      error
  end
end