Jan Ulbrich
Jan Ulbrich
AEAsh Elixir
Created by Jan Ulbrich on 8/27/2023 in #support
Ash.Resource.ManualRead And Filters
Hi, I’m wrapping an external REST API with an Ash resource and therefor use Ash.Resource.ManualRead. Reading the data is not a problem, but as the REST API is way less capable for filtering, I would like to run the returned data through the Ash filters I applied to the query. I mapped the returned data already to the Ash resource and now have an array of these resources. But how can I now apply the filters from the query? That’s what read!() normally would do, but calling that would lead to a recursion…
3 replies
AEAsh Elixir
Created by Jan Ulbrich on 8/12/2023 in #support
JSON-API And Calculations
Hi, I have a couple of calculations on my models and they seamlessly work in GraphQL. Now I need to make them accessible from the JSON-API and find no automatic way to address them. Do I need to write an extra-route or how should I approach this?
18 replies
AEAsh Elixir
Created by Jan Ulbrich on 6/28/2023 in #support
JSON-API Related() Seems Broken
Hi, 👋 there seems to be a regression in the JSON-API: It worked nicely (after you did a fix a couple of weeks ago), but after updating packages to the newest ones, I get an exception. Here’s the relation:
related(:events, :events_by_extid) do
route("/:extid/events")
end
related(:events, :events_by_extid) do
route("/:extid/events")
end
This is the exception I get:
[error] #PID<0.3383.0> running TickerWeb.Endpoint (connection #PID<0.3382.0>, stream id 1) terminated
Server: localhost:4000 (http)
Request: GET /api/eventhub/alarms/R%201.5%20230310%201956/events
** (exit) an exception was raised:
** (UndefinedFunctionError) function :events.name/0 is undefined (module :events is not available)
:events.name()
(ash_json_api 0.32.0) lib/ash_json_api/controllers/helpers.ex:348: anonymous fn/1 in AshJsonApi.Controllers.Helpers.fetch_related/2
(ash_json_api 0.32.0) lib/ash_json_api/controllers/get_related.ex:20: AshJsonApi.Controllers.GetRelated.call/2
(ticker 0.1.0) deps/plug/lib/plug/router.ex:246: anonymous fn/4 in TickerWeb.Api.Router.dispatch/2
[...]
(cowboy 2.10.0) deps/cowboy/src/cowboy_handler.erl:37: :cowboy_handler.execute/2
(cowboy 2.10.0) deps/cowboy/src/cowboy_stream_h.erl:306: :cowboy_stream_h.execute/3
(cowboy 2.10.0) deps/cowboy/src/cowboy_stream_h.erl:295: :cowboy_stream_h.request_process/3
(stdlib 4.3.1.1) proc_lib.erl:240: :proc_lib.init_p_do_apply/3
[error] #PID<0.3383.0> running TickerWeb.Endpoint (connection #PID<0.3382.0>, stream id 1) terminated
Server: localhost:4000 (http)
Request: GET /api/eventhub/alarms/R%201.5%20230310%201956/events
** (exit) an exception was raised:
** (UndefinedFunctionError) function :events.name/0 is undefined (module :events is not available)
:events.name()
(ash_json_api 0.32.0) lib/ash_json_api/controllers/helpers.ex:348: anonymous fn/1 in AshJsonApi.Controllers.Helpers.fetch_related/2
(ash_json_api 0.32.0) lib/ash_json_api/controllers/get_related.ex:20: AshJsonApi.Controllers.GetRelated.call/2
(ticker 0.1.0) deps/plug/lib/plug/router.ex:246: anonymous fn/4 in TickerWeb.Api.Router.dispatch/2
[...]
(cowboy 2.10.0) deps/cowboy/src/cowboy_handler.erl:37: :cowboy_handler.execute/2
(cowboy 2.10.0) deps/cowboy/src/cowboy_stream_h.erl:306: :cowboy_stream_h.execute/3
(cowboy 2.10.0) deps/cowboy/src/cowboy_stream_h.erl:295: :cowboy_stream_h.request_process/3
(stdlib 4.3.1.1) proc_lib.erl:240: :proc_lib.init_p_do_apply/3
4 replies
AEAsh Elixir
Created by Jan Ulbrich on 6/3/2023 in #support
GraphQL Relationship Resolved w/o Context
Hi there, 👋 I have a query like
{
alarm(filter: {id: { eq: 4711}}) {
message
events(sort: {field: ID, order: DESC}) {
id
message
}
}
}
{
alarm(filter: {id: { eq: 4711}}) {
message
events(sort: {field: ID, order: DESC}) {
id
message
}
}
}
which I need to filter with a Ash.Query.after_action. The filter is called as expected, but the actor of the request (available in the outer filter with the correct value) is nil for this nested. Do I manually need to forward the context in some way or might that be a bug? This is the stack trace raising an exception from Ash.Query.after_action (no error, but manually raised to get the stack):
(ash_graphql 0.25.5) lib/graphql/resolver.ex:1806: AshGraphql.Graphql.Resolver.to_resolution/3
(absinthe 1.7.1) lib/absinthe/middleware/dataloader.ex:37: Absinthe.Middleware.Dataloader.get_result/2
(absinthe 1.7.1) lib/absinthe/phase/document/execution/resolution.ex:232: Absinthe.Phase.Document.Execution.Resolution.reduce_resolution/1
(absinthe 1.7.1) lib/absinthe/phase/document/execution/resolution.ex:187: Absinthe.Phase.Document.Execution.Resolution.do_resolve_field/3
...
(phoenix 1.6.16) lib/phoenix/router/route.ex:41: Phoenix.Router.Route.call/2
(phoenix 1.6.16) lib/phoenix/router.ex:354: Phoenix.Router.__call__/2
(ash_graphql 0.25.5) lib/graphql/resolver.ex:1806: AshGraphql.Graphql.Resolver.to_resolution/3
(absinthe 1.7.1) lib/absinthe/middleware/dataloader.ex:37: Absinthe.Middleware.Dataloader.get_result/2
(absinthe 1.7.1) lib/absinthe/phase/document/execution/resolution.ex:232: Absinthe.Phase.Document.Execution.Resolution.reduce_resolution/1
(absinthe 1.7.1) lib/absinthe/phase/document/execution/resolution.ex:187: Absinthe.Phase.Document.Execution.Resolution.do_resolve_field/3
...
(phoenix 1.6.16) lib/phoenix/router/route.ex:41: Phoenix.Router.Route.call/2
(phoenix 1.6.16) lib/phoenix/router.ex:354: Phoenix.Router.__call__/2
29 replies
AEAsh Elixir
Created by Jan Ulbrich on 5/17/2023 in #support
Compile Error with JsonApi using related()
Hi there! 👋 I’m getting error message "(ArgumentError) Found an action of type read while looking for an action of type get_related, Perhaps you passed a changeset with the incorrect action type into your Api?" and I don’t understand how to solve it. Here’s the code:
relationships do
has_many :events, Event do
source_attribute(:extid)
destination_attribute(:extid)
end
end

json_api do
type("alarm")

includes(events: [])

routes do
base("/alarms")

related(:events, :events_by_extid) do
route("/:extid/events")
end
end
end

actions do
read :events_by_extid do
prepare(build(sort: [created_at: :asc]))

prepare(fn query, context ->
Ash.Query.after_action(query, fn _query, alarms ->
{:ok, Event.patch_results_applying_actor_filters(alarms, context)}
end)
end)
end
end
relationships do
has_many :events, Event do
source_attribute(:extid)
destination_attribute(:extid)
end
end

json_api do
type("alarm")

includes(events: [])

routes do
base("/alarms")

related(:events, :events_by_extid) do
route("/:extid/events")
end
end
end

actions do
read :events_by_extid do
prepare(build(sort: [created_at: :asc]))

prepare(fn query, context ->
Ash.Query.after_action(query, fn _query, alarms ->
{:ok, Event.patch_results_applying_actor_filters(alarms, context)}
end)
end)
end
end
I’m using the main branch of ash_json_api together with ash 2.9.4. I went through all Spark DSL stuff, but couldn’t find a clue about get_related searching the documentation.
5 replies
AEAsh Elixir
Created by Jan Ulbrich on 2/17/2023 in #support
Found read action while expecting an action of type relationship...
Hi, I need to customise a relation get the following error when trying to implement something like relationship(:events, :events_by_extid): ** (ArgumentError) Found an action of type read while looking for an action of type relationship The message itself is pretty obvious, but I can’t find a definition of how a relationship action would look like… 😇
23 replies
AEAsh Elixir
Created by Jan Ulbrich on 1/27/2023 in #support
Update Route W/ Arguments
I have a read action (and JSON API route) that takes an alternative identifer :extid instead of looking for the :id column. Now I would like the same route as an alternative for updates, too. The update understands the argument like the read does, but I don’t see how I can modifiy the query to read the correct record. A change can have an optional where which takes a list of functions, but I don’t get what these functions should look like…
50 replies
AEAsh Elixir
Created by Jan Ulbrich on 1/24/2023 in #support
Row Level Permissions
Hi, I’m trying to return certain attributes only for some user roles aka implementing row level permissions. Currently I’m doing that with an prepare/after_action block and while that works, it doesn’t feel natural. Filters and checks don’t help and removing attributes with the after_action make them still appear in the output of AshJsonApi and I would prefer them to not appear at all. Are there other approaches I could take?
18 replies
AEAsh Elixir
Created by Jan Ulbrich on 1/23/2023 in #support
Modifying results after reading
Hi, 👋 I need to manipulate the data returned from a database query. I found out how to do my own read based on Ash.Resource.ManualRead, but how can I call the default logic to apply my changes afterwards? Or is there some kind of after_read hook where I could do so? I would appreciate som pointers… 😇
12 replies