An absinthe library to add type constraints to your GraphQL schema using directives.
Install from Hex.pm
def deps do
[
{:absinthe_constraints, "~> 0.2.0"}
]
end
defmodule MyAppWeb.Schema do
use Absinthe.Schema
@prototype_schema AbsintheConstraints.Directive
# Use `import_sdl`, etc...
end
Example using Absinthe.Plug
:
# Somewhere in your router
def my_pipeline(config, opts) do
config
|> Absinthe.Plug.default_pipeline(opts)
|> AbsintheConstraints.Phase.add_to_pipeline(opts)
end
forward("/", Absinthe.Plug, schema: MyAppWeb.Schema, pipeline: {__MODULE__, :my_pipeline})
# Somewhere in your schema
# Adding constraints to input object's fields
input_object :coordinates do
field(:latitude, non_null(:float), directives: [constraints: [min: -90, max: 90]])
field(:longitude, non_null(:float), directives: [constraints: [min: -180, max: 180]])
end
# Adding constraints to query field's arguments
object :my_queries do
field :my_field, non_null(:string) do
arg(:id, non_null(:string), directives: [constraints: [format: "uuid"]])
resolve(&my_resolver/2)
end
end
directives: [constraints: [min_length: 5]]
Restrict to a minimum length
directives: [constraints: [max_length: 5]]
Restrict to a maximum length
directives: [constraints: [format: "uuid"]]
Ensure value is in a particular format
Supported formats:
"uuid"
- uses :elixir_uuid to validate the value"email"
- uses a regex to validate the value
directives: [constraints: [pattern: "^[A-Z][0-9a-z]*$"]]
Ensure value follows a specific Regex pattern
directives: [constraints: [min: 5]]
Ensure value is greater than or equal to
directives: [constraints: [max: 5]]
Ensure value is less than or equal to
directives: [constraints: [min_items: 5]]
Restrict to a minimum number of items
directives: [constraints: [max_items: 5]]
Restrict to a maximum number of items