Skip to content

Instantly share code, notes, and snippets.

@schrockwell
Last active September 20, 2019 01:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schrockwell/cf35e67224627f951c6047ee79b53301 to your computer and use it in GitHub Desktop.
Save schrockwell/cf35e67224627f951c6047ee79b53301 to your computer and use it in GitHub Desktop.
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