GreenFairy.Config (GreenFairy v0.3.0)

View Source

Global configuration for GreenFairy.

This module provides a way to configure global defaults that apply across all types in your schema, including:

  • Global Authorization - Default authorization that applies to all types
  • Default Node Resolution - How to fetch nodes by ID

Usage

Configure in your schema module:

defmodule MyApp.GraphQL.Schema do
  use GreenFairy.Schema, discover: [MyApp.GraphQL]
  use GreenFairy.Relay, repo: MyApp.Repo

  use GreenFairy.Config,
    authorize: fn object, ctx ->
      # Global authorization - runs before type-specific authorization
      if ctx[:current_user] do
        :all
      else
        :none
      end
    end,
    node_resolver: fn type_module, id, ctx ->
      # Default way to fetch nodes
      struct = type_module.__green_fairy_struct__()
      MyApp.Repo.get(struct, id)
    end
end

Authorization Composition

When both global and type-level authorization are defined:

  1. Global authorization runs first
  2. If global returns :none, the object is hidden
  3. If global returns :all or a field list, type-level authorization runs
  4. The final visible fields are the intersection of both

Options

  • :authorize - Global authorization function fn object, ctx -> :all | :none | [fields] end

  • :authorize_with_info - Authorization with path info fn object, ctx, info -> ... end
  • :node_resolver - Default node resolver fn type_module, id, ctx -> result end

Summary

Functions

Configures global defaults for GreenFairy.

Composes two authorization results.

Checks if a schema has global authorization configured.

Runs global authorization for an object.

Functions

__using__(opts)

(macro)

Configures global defaults for GreenFairy.

compose_auth(result, result)

Composes two authorization results.

Returns the intersection of allowed fields when both allow access.

Examples

compose_auth(:all, :all)           #=> :all
compose_auth(:all, [:id, :name])   #=> [:id, :name]
compose_auth([:id, :name], :all)   #=> [:id, :name]
compose_auth(:none, :all)          #=> :none
compose_auth(:all, :none)          #=> :none
compose_auth([:id, :name], [:id])  #=> [:id]

has_global_auth?(schema)

Checks if a schema has global authorization configured.

run_global_auth(schema, object, ctx, info \\ nil)

Runs global authorization for an object.

Returns the authorization result from the schema's global authorize function, or :all if no global authorization is configured.