GreenFairy.Config (GreenFairy v0.3.0)
View SourceGlobal 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
endAuthorization Composition
When both global and type-level authorization are defined:
- Global authorization runs first
- If global returns
:none, the object is hidden - If global returns
:allor a field list, type-level authorization runs - The final visible fields are the intersection of both
Options
:authorize- Global authorization functionfn object, ctx -> :all | :none | [fields] end:authorize_with_info- Authorization with path infofn object, ctx, info -> ... end:node_resolver- Default node resolverfn 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
Configures global defaults for GreenFairy.
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]
Checks if a schema has global authorization configured.
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.