-
-
Save schrockwell/cf35e67224627f951c6047ee79b53301 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule RHRLive.Resolver do | |
@callback resolve(map :: map, field :: atom, opts :: keyword) :: term | |
defmacro __using__(_) do | |
quote do | |
@behaviour RHRLive.Resolver | |
@before_compile RHRLive.Resolver | |
def resolve(map, field), do: resolve(map, field, []) | |
end | |
end | |
defmacro __before_compile__(_) do | |
quote do | |
def resolve(map, field, _opts), do: Map.fetch!(map, field) | |
end | |
end | |
end | |
defmodule MyApp.Data.MySchema do | |
use MyApp.Resolver | |
def resolve(schema, :some_computed_thing, _opts) do | |
"$#{schema.something}" | |
end | |
end | |
MyApp.Data.MySchema(schema, :id) # => 123 | |
MyApp.Data.MySchema(schema, :something) # => 456 | |
MyApp.Data.MySchema(schema, :some_computed_thing) # => "$456" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment