(as previously noted) I am enjoying working with Ash on a couple of projects immensely.
I’ve noticed that once I have started to add more actions, attributes, extensions, etc, the length of my module ex file has started to grow, so was curious as to others’ thoughts about best practices in organizing the file for sanity, predictability, and efficiency.
FWIW, I’ve read the hexdocs page on Project Structure, but didn’t spot any material about the modules themselves.
Thanks
Martin
My suggestion is to do the following:
Avoid inline functions
keep things clean by not using inline functions. For example:
change fn changeset, _ ->
do_something()
end
could also be:
change __MODULE__.Changes.DoSomething
This also pushes you to a natural structure for the things that comprise a resource.
Use Spark.Dsl.Fragment
to split up big resources.
You can use Spark.Dsl.Fragment
to split up big resources. A DSL module is considered to have all of the extensions of all of its fragments, so you can split things up however you like, including by extension.
defmodule YourApp.YourDomain.YourResource.Graphql do
use Spark.Dsl.Fragment, of: Ash.Resource, extensions: [AshGraphql.Resource]
graphql do
...
end
end
defmodule YourApp.YourDomain.YourResource do
use Ash.Resource, fragments: [__MODULE__.Graphql]
...
end
4 Likes
Thanks Zach. The Fragment was something that I hadn’t spotted. This is very helpful.
Martin
2 Likes
Zach (another follow-on question)…
Can I add a parameter to a Change module - so it can cut down on custom changes.
For example, I’d like to (and tried to) write
change __MODULE__.Changes.DoSomething, foo: :bar
so that the options (:foo) would be available in the new module’s change function - but saw clearly that the options accepted by the change dsl is limited.
So is there a way to parameterize the change?
Thanks
Martin
change {__MODULE__.Changes.DoSomething, foo: :bar}
The second element of the tuple is passed as opts to the change
Barnabas:
That’s perfect! I had tried:
change {__MODULE__.Changes.DoSomething, :foo}
But didn’t try it as a KW list.
Frankly I’m embarrassed!!
Many thanks for your generous and quick response.
Martin