revati

revati

Code_interface for api

In classical phoenix app if i would have Accounts context and Users schema, i would call Accounts.create_user, but in Ash context, Having Accounts api and User resource, when defining code_interface on resource, i have to call User.create. I would much rather have Accounts.create_user.

It would be great to be able to do something like:

defmodule MyApp.MyApi do
  use Ash.Api

  resources do
    resource MyApp.MyApi.MyResource
    resource MyApp.MyApi.MyAnotherResource
  end

  code_interface do
    define :create_resource, resource: MyApp.MyApi.MyResource, action: :create
    define :create_another_resource, resource: MyApp.MyApi.MyAnotherResource, action: :create
  end
end

so i wiuld be able to call MyApp.MyApi.create_resource and so on.

Marked As Solved

zachdaniel

zachdaniel

Creator of Ash

You can actually accomplish this today like so:

# in the resource, use different names

define :create_resource, ...

don’t add define_for YourApi

and then in your api module, call

# you will need this at the top
require Ash.CodeInterface

Ash.CodeInterface.define_interface(__MODULE__, Resource1)
Ash.CodeInterface.define_interface(__MODULE__, Resource2)
Ash.CodeInterface.define_interface(__MODULE__, Resource3)

This will put the functions in the api module. Some users reported compile time/dependency issues doing this in the past, but it’s actually possible that we’ve resolved this with some internal things we’ve done to reduce compilation dependencies. If you have similar issues, you can put the interface in a different module, .e MyApp.Accounts.Interface, and that should resolve those issues.

EDIT: With that said, the idea of having a DSL specifically for it in the api sounds interesting, and is something I’d be willing to explore, providing we can solve for the compilation dependency issues (if they still exist).

Also Liked

revati

revati

Worked like a charm. Just wanted to let you know, there were no conflicts, no errors. 10mins i refactored all my resources to this.

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
hariharasudhan94
Lets say i have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => "XXX...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement