How to get DataLayer transformer to run after optional Extension

Hi I’m working on an Ash DataLayer for Neo4j and I’ve noticed that the AshStateMachine has a transformer which adds the extended resource’s state_attribute to attributes so it introspects using Ash.Resource.Info, however this seems to only happen after my DataLayer transformer has already been run for the resource. How can I ensure in my DataLayer that my transformer runs after the optional Extension transformer?
Solution:
You can use def after and def before to order transformers
Jump to solution
3 Replies
Solution
ZachDaniel
ZachDaniel3mo ago
You can use def after and def before to order transformers
ZachDaniel
ZachDaniel3mo ago
its a bit hacky, unfortunately, but it works If you have a transformer who's only job is to persist some information about the resource, then put it in the persisters list instead of the transformers list. Just promise not to modify the resource in a persister and you're good
Matt Beanland
Matt BeanlandOP3mo ago
Thanks Zach. I'd tried after? before however didn't understand the nuances and had another go after your answer. I see that after? and before? default to false and presume this when either is true this introduces an ordering constraint. I have a design choice about whether to simply
def after?(_), do: true
def after?(_), do: true
and go after everything (put possibly conflict with other code where others have done the same, and/or limit future flexibility), or be explicit about what I need to go after. I did the latter, but to avoid spark 'Cycle detected in transformer order' on compilation, I did
@impl true
def after?(AshStateMachine.Transformers.AddState), do: true
def after?(Ash.Resource.Transformers.DefaultAccept), do: true
def after?(_), do: false
@impl true
def after?(AshStateMachine.Transformers.AddState), do: true
def after?(Ash.Resource.Transformers.DefaultAccept), do: true
def after?(_), do: false
This was because AshStateMachine's AddState transformer has been set to run before Ash.Resource.Transformer.DefaultAccept, and after everything else except that. So I'm coupled to some Transformer module names in both Ash and AshStateMachine. What is best practice with these before and after clauses.?

Did you find this page helpful?