ash icon indicating copy to clipboard operation
ash copied to clipboard

feat: Exclude resource calculations from resource struct with `field?: false` option

Open chazwatkins opened this issue 5 months ago • 5 comments

Improvements

  • Add field? option to Ash.Resource.Calculation DSL
  • Exclude field?: false calculations from Resource struct and direct loading, but still usable in expressions
  • Add validation that returns Ash.Error.InvalidLoad error when loading a field?: false calculation directly
  • Update calculations docs with explanation of field? option

We could probably do the same for aggregates once this is done.

#2242

Contributor checklist

Leave anything that you believe does not apply unchecked.

  • [x] I accept the AI Policy, or AI was not used in the creation of this PR.
  • [ ] Bug fixes include regression tests
  • [ ] Chores
  • [ ] Documentation changes
  • [x] Features include unit/acceptance tests
  • [ ] Refactoring
  • [ ] Update dependencies

chazwatkins avatar Aug 04 '25 04:08 chazwatkins

ok, the validation is in and you now get an InvalidLoad error when you attempt to load a field?: false resource calculation.

chazwatkins avatar Aug 04 '25 17:08 chazwatkins

Just had a thought: we may actually have a bit of an issue here 😢

Specifically, AshGraphql and AshJsonApi will need to be updated to support this, by not allowing loads for these non-loadable calculations. We'll want PRs for those done before we release this.

zachdaniel avatar Aug 04 '25 19:08 zachdaniel

Just had a thought: we may actually have a bit of an issue here 😢

Specifically, AshGraphql and AshJsonApi will need to be updated to support this, by not allowing loads for these non-loadable calculations. We'll want PRs for those done before we release this.

No experience with those packages, but I'll take a look at them.

chazwatkins avatar Aug 04 '25 20:08 chazwatkins

Both api libraries only expose public?: true calculations. So, I made a small change to have public_calculations and public_calculation only return back field?: true calculations.

Do you see any cases where a field?: false calculation should be exposed outside the resource?

chazwatkins avatar Aug 04 '25 20:08 chazwatkins

Hmm...yes. It could be supported in filters exposed over GraphQL for example without being exposed as a field.

zachdaniel avatar Aug 05 '25 03:08 zachdaniel