Yson.Schema (yson v0.2.2) View Source
Defines a Yson Schema that can be included in other schemas.
It is the base of high level schemas like Yson.GraphQL.Schema and Yson.Json.Schema, and contains useful macros to build up Schema description and resolvers tree.
defmodule Person do
use Yson.Schema
map :person do
map :address do
value(:street)
value(:city)
end
end
endDeep nesting is allowed but it is always possible to move a block outside and reference it with reference/2. The previous example could be changed as follows:
defmodule Person do
use Yson.Schema
map :person do
reference(:address)
end
map :address do
value(:street)
value(:city)
end
end
Link to this section Summary
Functions
Imports schema main types from another Yson.Schema module.
Defines a interface.
Defines a map.
References a map or an interface by name.
Defines the root of the schema.
Defines a simple field.
Link to this section Functions
Imports schema main types from another Yson.Schema module.
Examples
defmodule Address do
use Yson.Schema
map :address do
value(:city)
end
end
defmodule Person do
use Yson.Schema
import_schema(Address)
map :person do
reference(:address)
end
endImported schema is not public. If a module imports Person schema and needs to refer :address explicitly, it will need to import Address too.
Defines a interface.
It contains virtual fields that will be automatically mapped on parent node as children. An interface field could be a value, a map, an interface or a reference.
Example
interface :address do
value(:city)
end
Defines a map.
A map field could be a value, a map, an interface or a reference.
Example
map :person do
value(:name)
endYou can also specify custom resolver to parse data. It can be either a reference or an anonymous function.
Example
reverse_name = fn %{name: name} -> %{name: String.reverse(name)} end
map :person, resolver: &reverse_name/1 do
value(:name)
end
References a map or an interface by name.
The referenced map/interface should be defined outside the root/2 macro scope.
Example
map :any do
reference(:referenced)
end
map :referenced do
value(:name)
endIt is also possible to rename the referenced field:
Example
map :any do
reference(:referenced, as: :my_field)
end
map :referenced do
value(:name)
end
Defines the root of the schema.
It contains the schema tree. A root field can be a value, a map, an interface or a reference.
Examples
root do
value(:name)
endYou can also specify custom resolver to parse data. It can be either a reference or an anonymous function.
Example
reverse_name = fn %{name: name} -> %{name: String.reverse(name)} end
root resolver: &reverse_name/1 do
value(:name)
end
Defines a simple field.
Example
value(:referenced)You can also specify custom resolver to parse data. It can be either a reference or an anonymous function.
Example
value(:name, &String.reverse/1)