A wrapping resource which exposes an api which would return a return value

Hi

I hope I am rephrase my usecase. I have trimmed down for clarity sake. Let’s say that, I have a

defmodule ResourceA do
  ...
  attributes do
    attribute :date
  end
end

ResourceB

defmodule ResourceB do
  ...
  attributes do
    attribute :methods, one_of: [:phone, :online]
  end
end

ResourceC - whose job is get values from ResourceA and ResourceB and return it.

defmodule ResourceC do
  ...
  code_interface do
    define :overlap, args:[...]
  end
  actions do
    action :overlaps, .. do
      argument :start_date, :string, allow_nil?: false
      argument :method, :atom, allow_nil?: false
      run fn input, context -> 
          start_date = inputs.arguments.start_date
          method = inputs.arguments.method
          # gets overlaps from ResourceA and ResourceB
          {:ok, result}
      end
    end
  end
end

Now in the Doamin

defmodule Domain do
use  Ash.Domain, extensions:[AshJsonApi.Domain]

resources do
 ...
end

json_api do
  routes do
   base_route "/overlaps", ResourceC do 
     route :get, "/overlaps", :overlaps
   end
  end
end
end

I am able to make the call. I can’t pass query params to the api call. I tried to debug but in vain. I am getting 400 error response. Any help is appreciated.

Thanks.

In the code that I can see here, the action does not have start_date or method arguments. Have you added those arguments to the generic action?

Also, when sharing errors etc. please share as much context, like what the response body was for the 400 error and if there are any logs indicating additional detail etc. Just 400 status could be many many things :slight_smile:

Hi

My bad. Yes the start_date and method are arguments for the action.
here is the response.

15:52:27.017 [error] required: Required | is required


  1) test GET /get_slots (ResourceCTest)
     test/xxxx/api/reserve/resources/resourcec_test.exs:27
     Expected to get status 200 but got 400.

     Response body: %{
       "errors" => [
         %{
           "code" => "required",
           "detail" => "is required",
           "id" => "8745f55a-8645-4d96-a35d-2ea152158dfe",
           "meta" => %{},
           "status" => "400",
           "title" => "Required"
         }
       ],
       "jsonapi" => %{"version" => "1.0"}
     }

 code: ApiTestHelper.get_test(new_path, user, org_id, [])
     stacktrace:
       (ash_json_api 1.4.16) lib/ash_json_api/test/test.ex:68: AshJsonApi.Test.get/3
       (xxx 2.0.0) test/support/api_test_helper.ex:12: ApiTestHelper.get_test/4
       test/xxx/api/reserve/resources/resourcec.exs:81: (test)

I have noticed that if I don’t pass the arguments as query parameters. I get the error saying that I am missing the arguments. If I pass the arguments they are not ending up in the Ash.run_action() call being made from the GenericActionRoute. I am not sure what I was missing.

Hmm…it sounds like a bug? So input.arguments is an empty map?

Yes it is empty. I have noticed query_params is being populated in AshJsonApi.Request.parse_query_params, but it is relying on request.route. Not from request.query_params. I did not understand why it was done like that. I didnot dig deeper after that.

Oh, right, did you set this? DSL: AshJsonApi.Resource — ash_json_api v1.4.16

i.e query_params: [:start_date, :method]. I’m trying to remember why we require that to be set :thinking:… I think it is for post and other methods that could include a body and so require being told what is where. In the future we could probably assume that for get requests all of the inputs are query params.

1 Like

Oh I missed that. I got it working. Thanks stupid me. I should have gone through the options first :disappointed:

1 Like