Created
February 8, 2017 17:35
-
-
Save bruce/e5307411f757c6ac585005d7f8f37e68 to your computer and use it in GitHub Desktop.
Extending an Absinthe schema using custom, app-specific macros
This file contains hidden or 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 MyApp.Color do | |
@moduledoc """ | |
Just an example source of the values | |
""" | |
def list do | |
~w(red green blue)a | |
end | |
end |
This file contains hidden or 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 MyApp.Schema.Macros do | |
@moduledoc """ | |
App-specific macros to extend the schema | |
""" | |
defmacro color_definitions do | |
for color <- MyApp.Color.list() do | |
quote do: value unquote(color) | |
end | |
end | |
end |
This file contains hidden or 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 MyApp.Schema do | |
use Absinthe.Schema | |
# Get the macros | |
import MyApp.Schema.Macros | |
enum :colors do | |
# Use the macro | |
color_definitions() | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment