cjbottaro
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!
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Hello,
I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter).
The diffic...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Other Trending Topics
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Aludel - LLM Evaluation Workbench
Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Eiji
@cjbottaro The problem with
absintheis that they are using macros and work only with raw data (not in variables).This is because
absintheis making some checks on raw data.Let’s say we have such simple code:
As you can see it’s not possible to work on it without proper quoting. Same goes if you want to use
absinthemacros 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
ASTyou need to return:Let’s split it:
{:field, [], [:id, :integer_filter]}As you can see it’s
field/2macro AST{:__block__, [], […]}Block here is list of
ASTexpressions inside function which are not single literals. For example:def sample(…) do 5 endgives us just raw5in place of whole:__block__part, but if we add one more line with same literal they would be arguments in:__block__AST.Finally
{:input_object, [], [:user_filter, [do: …]]}Similarly to 1st point it’s ast for
input_object/2call. Heredo … endgoes 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:
and here is usage:
Sorry if I made any typo - I wrote everything from memory.
cjbottaro
Holy moly… thanks for that super detailed explanation!
Let me see if I understand. The strategy here is not to “programmatically call” the Absinthe macros, but rather examine the AST they output and mimic it with my own macro?
Why can the
defmacro be used withunquotebut the Absinthe macros not?And lastly, does the metadata element of the AST tuple not matter?
Thanks!
blatyo
I think you just need to use
Enum.map/2instead or use a for comprehension.defhas side effects, which is why you can do it in anEnum.each/2I believe.Eiji
Because
defis function which can accept any data (raw or in variables). As saidabsinthecalls likeinput_object/2andfield/2can’t use variables as they are macros withoutquote do … endcode (again because of some raw data checks at compile time). Same goes to my version ofExample.sample/0macro. If you would add some arguments then you need to pass only raw data. It’s how meta-programming works.I recommend to read some articles/books about meta-programming like:
https://www.bignerdranch.com/blog/getting-started-with-elixir-metaprogramming/
because for beginners it’s hard even to visualize how it works.
cjbottaro
Ahh, now I see. Ok, yeah in your sample, your macro doesn’t use
quote do ... end, got it.cjbottaro
One last question… how do you make your
Example.sample/0macro take arguments? For example, if you want to pass in the name and fields like so:Thanks!
cjbottaro
Using
Code.eval_quoted/1seems to work:But I’m unsure if this is the right way.
Eiji
What I learned in
PHPworld is to absolutely never useevalunless it’s really, really required, but not sure how it looks like inElixir. Of course everything depends on some things like from where you are accepting input. If you have data in code then you can simply create it in macro or call some function to return input data.Look that when you are accepting any argument in macro then you will receive it quoted. You can use it inside
quote do … endor just fetch them from somewhere, but first fetch call needs to come from macro.cjbottaro
Hi, I just wanted to say thanks for your help. Here’s my final working code; it definitely could not have been done without your help, example code, and explanations.
Again, thanks a ton…
Eiji
you are passing raw value, but then …
Look that:
returns:
so you can do:
Secondly code is much cleaner when you are writing it top → bottom instead of bottom → top. It’s faster to read it.
Finally make sure you are using
Elixirbuiltin code formatter:https://hexdocs.pm/mix/master/Mix.Tasks.Format.html