Action macro with Ash queries
hey Zack, in the #ideas channel, you gave the idea of creating a macro in case I want to move an action logic to another module https://discord.com/channels/711271361523351632/712035333432147989/1075932613061116036
I did that and it seems to work great, but when I add some
Ash.Query
macros to it, then it stops working. The obvious reason should be that the macro is not finding require Ash.Query
but I can't find a way to add it to make it work. This is what I have right now:
32 Replies
This will fail to compile with this error:
Any idea on how to solve this?
You may need to add
import Ash.Expr
inside of your macroUnfortunately that will not work because
expr
will conflict with another one already imported:
Try importing Ash.Expr in the containing module
And then saying
unquote(expr(…))
?
Actually that might not workYeah, I was trying to play around with that but can't make that work 😢
I’ll have to take a look tomorrow
No problem, enjoy the weekend, we can look into this on Monday 🙂
mmm, can't you import it at the start of your macro, then import it again with
:only
keyword to exclude it again?
pretty sure it will be available between those two things then
so something like this

@Blibs were you able to make this work?
Not really. In the end, what I did was keep the definitions of the actions in the resource file but move all changes from it as separated modules to make the action smaller and simpler.
Something like this:
and the actions modules are like this:
It is not perfect, but at least is better than having everything inside the action definition and having a bigger and more polluted (IMO) resource.
I like this for changes that are reusable. If its something resource specific or a manual action I'm not happy with splitting up the definition of the arguments and the logic itself. I would really prefer to have it all in one place in that case. Nothing major in the grand scheme of things though
Lemme take a look at the
expr
issues
Alright, here is what I'm looking at:
How do we feel about using fragments in cases where we want to split a resource up?You could essentially do this with all sections right?
Yeah, this would work for essentially anything
It will merge sections
but "options" would overwrite eachother (which I know isn't always clear what is what on the tin) but like
that in a fragment would end up with one description and the others being overwritten
Feels very powerful. I probably have to restrain myself from overusing it 😉. Would it be possible to emit a warning if something gets overwritten, without a big compile time hit?
Hmm...yeah, actually I think so.
could probably even tell you which fragment did it
Should make it pretty safe, all other compile time checks still apply too, right?
Yep 🙂
It should be equivalent in all other ways as if you had just put all the fragments at the top of the resource
Do we lose any language server features by splitting things up like that?
The autocomplete extension works in fragments, had to make a small adjustment for it to work
but confirmed it works
Amazing 🤩
can the
of
option also be a custom extended Ash.Resource?
Something that configures some extensions by default like a BaseResource
I'm pretty sure that will be impossible, unfortunately
But if I have a custom extension could i declare it as
of
or can i not use this feature with custom extensions?I don't think it would be necessary
you can say
of: Ash.Resource
and then use YourApp.Resource, fragments: [Fragment]
Sorry, I think I didn't communicate that clearly, can i make a fragment for
AshGraphql.Resource
for example?Ah, okay yeah I see. This is how it works currently
and then a resource has the extensions that the sum of all of its fragments have
I know what I'm doing tomorrow 😆
I'll need to run it by some people and make sure this this is the right way to go about it, but this isn't the first time people have wanted to split up their resources 😆 The main thing that I don't like about this pattern is that it doesn't support any kind of dynamic thing. Which may actually just be better.
Don't want to make it too powerfull
Yeah, agreed. I think static is better, and if you want to do this to organize a resource then thats fine, but more complex things and you can write an extension
this is just like...a better option for composing a resource than macros, basically
feels a lot cleaner