Relying on metadata on "public" API an anti-pattern?

Is it generally considered an anti-pattern to rely on data that's stuffed inside metadata on a resource, especially in a "public" API? When I say public API, I mean generally following the pattern of Resource actions being used only internally in their domain and Domain actions being used externally of the domain. For example, say I have an MyApp.Accounts.Session resource that puts a token in the metadata during the create action since the token is hashed before inserting into the DB. The public API on the domain exposes it through an Accounts.create_session action. Do we really want callers to be doing Resource.get_metadata on the result in that public context to get the token or is that an anti-pattern? Is there a better way to be surfacing that information? For example:
def my_caller_func do
# ... other code
session = Accounts.create_session!(user)
token = Ash.Resource.get_metadata(session, :token)
end
def my_caller_func do
# ... other code
session = Accounts.create_session!(user)
token = Ash.Resource.get_metadata(session, :token)
end
This could be in a Phoenix controller or someplace else where we may be calling these domain functions.
2 Replies
ZachDaniel
ZachDaniel4mo ago
Yes, metadata is designed for this purpose Metadata can even be declared on the action to allow it to be read over APIs etc.
Steve
SteveOP4mo ago
All right, thanks. Just wanted to make sure I wasn't doing anything wild

Did you find this page helpful?