Phoenix generated resource patterns match in controller

Hello!

I have a question on why the generated code in the controller patterns match like this:

def create(conn, %{"resource" => resource_params}) do
...
end

Is it a good design decision to follow in my REST api? Or is it better to just have all the params without being nested in the "resource"?

If the consumer of this api doesn’t send the "resource" then a pattern match error is raised but the consumer doesn’t have enough information of what happened (missing "resource" key), how do I overcome this?

Thank you!

You can use fallback controller. You define only the happy path, then You define the not so happy path in the fallback controller.

This might be outdated, but it did not change that much since Phoenix 1.3… at least for FB controller.

I like to keep into it’s resource space, but it is up to You.

I also prefer to use GraphQL instead of json api, it takes a bit more work to do, but it is flexible.

1 Like