AshCredo.Check.Refactor.UseCodeInterface (ash_credo v0.5.2)

Copy Markdown View Source

Basics

This check is disabled by default.

Learn how to enable it via .credo.exs.

This check has a base priority of normal and works with any version of Elixir.

Explanation

When both the resource and action name are literal values, prefer calling a code interface function on the resource module.

# Flagged — resource and action are both literals
Ash.read(MyApp.Post, action: :published)
Ash.bulk_create(inputs, MyApp.Post, :import)

# Preferred — use the code interface
MyApp.Post.published()
MyApp.Post.import(inputs)

Builder functions are also flagged, but note that the code interface equivalent preserves the return type via generated query_to_*, changeset_to_*, or input_to_* helpers:

# Flagged
Ash.Query.for_read(MyApp.Post, :published)
Ash.Changeset.for_create(MyApp.Post, :create, params)

# Preferred — returns a query / changeset, not results
MyApp.Post.query_to_published()
MyApp.Post.changeset_to_create(params)

This applies to direct Ash API calls, bulk operations, and changeset/query/action-input builder functions.

Check-Specific Parameters

There are no specific parameters for this check.

General Parameters

Like with all checks, general params can be applied.

Parameters can be configured via the .credo.exs config file.