cjbottaro

cjbottaro

How to programmatically call macros?

I want to programmatically generate input objects from introspecting my schema, but I can’t figure out the metaprogramming to do so.

Essentially, I want this desired module def:

defmodule InputObjects do
  use Absinthe.Schema.Notation

  input_object :user_filter do
    field :id, :integer_filter
    field :email, :string_filter
  end
end

But from coming from variables:

defmodule InputObjects do
  use Absinthe.Schema.Notation

  name = :user_filter
  fields = [
    {:id, :integer_filter},
    {:email, :string_filter}
  ]
  
  input_object name do
    Enum.each fields, fn {name, type} ->
      field name, type
    end
  end
end

How to do this? Thanks for the help!

Marked As Solved

Eiji

Eiji

@cjbottaro The problem with absinthe is that they are using macros and work only with raw data (not in variables).

defmodule Example do
  defmacro sample(some_data) do
    {:here, [], [:just, :goes, :ast, :without, :quote]}
  end
end

# instead of:

defmodule Example do
  defmacro sample(some_data) do
    quote bind_quoted: [some_data: some_data], unquote: false do
      here(:just, :goes, :normal, :data, :not, :in, :ast, :format, some_data)
    end
  end
end

This is because absinthe is making some checks on raw data.

Let’s say we have such simple code:

defmodule Example do
  defmacro sample(data) do
    IO.inspect(data)
  end
end

Example.sample(5)
5 # IO.inspect call here
5

# vs

data = 5         
5
Example.sample(data)
{:data, [line: 8], nil} # IO.inspect call here
5

As you can see it’s not possible to work on it without proper quoting. Same goes if you want to use absinthe macros i.e. you need to pass raw data.

However this does not mean that it’s not possible to pass variables - this only means that we need pass raw data to absinthe macros. It should be hint for more experienced developers. Just write your own macro!

Firstly you need to know what AST you need to return:

# inside iex call
quote do
  # code of which ast you want to preview
end

# for example:
quote do
  input_object :user_filter do
    field :id, :integer_filter
    field :email, :string_filter
  end
end

{:input_object, [],
 [
   :user_filter,
   [
     do: {:__block__, [],
      [
        {:field, [], [:id, :integer_filter]},
        {:field, [], [:email, :string_filter]}
      ]}
   ]
 ]}

Let’s split it:

  1. {:field, [], [:id, :integer_filter]}
    As you can see it’s field/2 macro AST

  2. {:__block__, [], […]}
    Block here is list of AST expressions inside function which are not single literals. For example: def sample(…) do 5 end gives us just raw 5 in place of whole :__block__ part, but if we add one more line with same literal they would be arguments in :__block__ AST.

  3. Finally {:input_object, [], [:user_filter, [do: …]]}
    Similarly to 1st point it’s ast for input_object/2 call. Here do … end goes to 2nd argument which is keyword list [do: …]. As in 2nd point we could have: [do: 5] or [do: {:__block__, [], […]}]. For us it’s 2nd case as we will never contain literals there.

From this here goes example code:

defmodule Example do
  defmacro sample do
    name = :user_filter

    fields = [
      [:id, :integer_filter],
      [:email, :string_filter]
    ]

    data = Enum.map(fields, &Example.ast_call(:field, &1))
    block = Example.ast_call(:__block__, data)
    do_block_keyword = [do: block]
    Example.ast_call(:input_object, [:user_filter, do_block_keyword])
    # since you generated AST you do not need `quote do … end` here
  end

  # in same way you can simply create helper functions
  # for specific absinthe calls,
  # so you can minimize your initial data (i.e. no need to pass empty list as 2nd argument in each ast_call)
  # and your code is more readable
  def ast_call(name, args), do: {name, [], args}

  # for example:
  # def field_ast(name, type), do: {:field, [], [name, type]}
end

and here is usage:

defmodule InputObjects do
  use Absinthe.Schema.Notation
  require Example
  Example.sample()
end

Sorry if I made any typo - I wrote everything from memory. :077:

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement