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
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
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’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
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 14 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Eiji
Firstly
ASTis not extremely low level if we are talking aboutElixir.Well … I have one really good example from my experience …
My first job … I have worked with Ruby/Rails. I saw really small monkey patching usage from someone who was more experienced than me. I liked how it’s simple (so there was no need to change existing code) and I have stupidly changed file with just few lines of monkey patching into weird file with hundreds of lines with monkey patching.
If something is supported it does not mean that it should be always used. Especially when we are talking about evals. Look … I believe that
Joomla!andWordpressdevelopers are not stupid. Same goes to developers of (at least most popular) extensions for those projects. The problem is that lots of sites was trivially hacked by addingPHPcode into image files - just because they used eval.Sure, one module looks safe, but can you guarantee that this will be still safe after few rotations in
devteam? Can you guarantee that it will be still safe after somebody in team decides to move such generator (withevalcall) to open source library? Software is changing really rapidly.If we are going always with easiest solution then we will end with something like
Windowsin few weeks/months.Windowswas created by definitely good developers, but looking how it changes in years we see that something is definitely wrong. For example compare modern (with rewritten code)Plasma 5features and requirements with what offersWindows’s builtin window manager. I can’t do even as good job as that made in both projects, but still I (being not experienced in OS programming) see huge differences.Also my solution is not about only “low level”. We have here typical pattern matching which prevents to add data of any type. We can always add simple guard to check if module have
Schemapart in list too without bigger problem.Summary:
Therefore I’m not sure what’s wrong with my hint.
cjbottaro
Oh… well now we’re going to get into a difference of opinions.
Just because we can do something doesn’t mean we should… or in this case, it’s simpler and/or more readable to eval the args before passing them, instead of passing AST tuples. Also, the arguments are not coming from “user input” so there is no concern with eval’ing them.
I feel like there are two approaches to Elixir metaprogramming:
So I see what you’re saying: everything can be done at an extremely low level. But if I can do stuff at a higher level, that’s supported by the language’s standard lib. Shouldn’t I do that before just mucking with ASTs?
Eiji
The problem is that people don’t want to write
absintheschema withabsinthemacro, but dynamically generateabsintheschema based onectodatabase representation + some extra checks and rules.axelson
I just want to quickly note that one of the goals of Absinthe 1.5 is to need less macros to dynamically define a schema, and I know other people are working on making it simpler as well.
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
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
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
Using
Code.eval_quoted/1seems to work:But I’m unsure if this is the right way.
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
Ahh, now I see. Ok, yeah in your sample, your macro doesn’t use
quote do ... end, got it.