ash-project / ash_json_api

The JSON:API extension for the Ash Framework
https://hexdocs.pm/ash_json_api
MIT License
56 stars 40 forks source link

(`ash_json_api.install`) chicken and egg problem with adding domains to generated router #212

Closed sevenseacat closed 1 month ago

sevenseacat commented 1 month ago

Describe the bug

Running igniter.install ash_json_api in a project will create a new AppWeb.AshJsonApiRouter module, but not list any domains in it.

This code in the igniter attempts to select existing domains to add to the router, but only selects those that are already configured to extend AshJsonApi.Domain. None of them will, because we're just installing the library - adding the extension to any domains beforehand would cause a compilation error.

To Reproduce

Calling mix igniter.install ash_json_api in any app that has Ash domains already existing in the app - none will be added to the router.

Expected behavior

I'm not sure what I really expected because I'm testing out the installer for the first time.

This is kind of why I wanted separate installation and router gen igniters though - a router gen igniter could take args for domains to add, and add both the domains to the router and the extension to the domains.

zachdaniel commented 1 month ago

So the "idea" here is that when doing mix ash.patch.extend MyApp.Resource json_api is when it does that work. Additionally, the installer can be rerun even after initial install to add those domains to the schema. So if you did

mix ash.patch.extend Some.Domain.Resource json_api # adds extension to resource & domain, and adds to router
mix ash.patch.extend Some.Domain json_api # adds extension to the domain, and adds to the router
mix ash.gen.resource Some.Domain.Resource ... --extend json_api # does both of the above

We could add something like mix ash_phoenix.gen.router, but even then that looks like something you'd only run once. Maybe we should just document this in the ash_json_api setup guide that if you want to extend your existing domains to use ash_json_api to do mix ash.patch.extend Each.Domain json_api to add them to the router?

sevenseacat commented 1 month ago

Ah hah that makes more sense. I was quite confused reading over the code trying to figure out how to get my domain auto-added to the router!

I'm a little wary of having too many options on how to do things. Maybe it's because all of these different igniters are being set up at once, but it feels like a lot to take in. I think this can be closed, but I'll look at how to fill in gaps both in the docs and in the book.