AbsintheHelpers.Phases.ApplyConstraints (absinthe_helpers v0.1.6)
Validates input nodes against constraints defined by the constraints
directive in your Absinthe schema. Constraints can be applied to fields
and arguments, enforcing rules such as min, max, etc. These constraints
can be applied to both individual items and lists simultaneously.
Example usage
Add this phase to your pipeline in your router:
pipeline =
config
|> Absinthe.Plug.default_pipeline(opts)
|> AbsintheHelpers.Phases.ApplyConstraints.add_to_pipeline(opts)Add the constraints directive's prototype schema to your schema:
defmodule MyApp.Schema do
use Absinthe.Schema
@prototype_schema AbsintheHelpers.Directives.Constraints
directive :constraints do
on([:argument_definition, :field_definition, :input_field_definition])
arg(:min, :integer, description: "Minimum value allowed")
arg(:max, :integer, description: "Maximum value allowed")
arg(:min_items, :integer, description: "Minimum number of items allowed in a list")
arg(:max_items, :integer, description: "Maximum number of items allowed in a list")
end
# ...
endApply constraints to a field or argument:
field :my_list, list_of(:integer) do
directive(:constraints, [min_items: 2, max_items: 5, min: 1, max: 100])
resolve(&MyResolver.resolve/3)
end
field :my_field, :integer do
arg :my_arg, non_null(:string), directives: [constraints: [min: 10]]
resolve(&MyResolver.resolve/3)
end
Summary
Functions
Link to this function
add_to_pipeline(pipeline, opts)
Link to this function
flag_invalid(node)
@spec flag_invalid(node :: Absinthe.Blueprint.node_t()) :: Absinthe.Blueprint.node_t()
Link to this function
flag_invalid(node, flag)
@spec flag_invalid(node :: Absinthe.Blueprint.node_t(), flag :: atom()) :: Absinthe.Blueprint.node_t()
Link to this function
flag_invalid(node, flag, reason)
@spec flag_invalid( node :: Absinthe.Blueprint.node_t(), flag :: atom(), reason :: String.t() ) :: Absinthe.Blueprint.node_t()
Link to this function
inherit_invalid(node, children, add_flag)
Link to this function