0

https://ash-hq.org/docs/guides/ash/latest/tutorials/get-started proposes this file structure:

lib/
├─ helpdesk/
│  ├─ support/
│  │  ├─ registry.ex    # <-----
│  │  ├─ resources/
│  │  │  ├─ ticket.ex
│  ├─ support.ex

support is the API here or is it helpdesk? I am struggling with the wording. Anyhow I understand the tutorial so that the file registry.ex has always to be under the `support´ directory.

This works

My project has this file structure:

lib
├── feriendaten
│   ├── geo
│   │   ├── geo.ex
│   │   └── resources
│   │       ├── level.ex
│   │       ├── location.ex
│   │       └── school.ex
│   ├── legacy
│   │   ├── legacy.ex
│   │   └── resources
│   │       └── legacy_location.ex
│   ├── registry.ex   # <---- one file

The file lib/feriendaten/registry.ex has this content:

defmodule Feriendaten.Registry do
  use Ash.Registry,
    extensions: [
      # This extension adds helpful compile time validations
      Ash.Registry.ResourceValidations
    ]

  entries do
    entry Feriendaten.Geo.Level
    entry Feriendaten.Geo.Location
    entry Feriendaten.Legacy.LegacyLocation
    entry Feriendaten.Geo.School
  end
end

That works without a problem.

This doesn't works

According to the above tutorial the file structure should be like this (one registry.ex in each directory):

lib
├── feriendaten
│   ├── geo
│   │   ├── geo.ex
│   │   ├── registry.ex   # <- first registry.ex
│   │   └── resources
│   │       ├── level.ex
│   │       ├── location.ex
│   │       └── school.ex
│   ├── legacy
│   │   ├── legacy.ex
│   │   ├── registry.ex   # <- second registry.ex
│   │   └── resources
│   │       └── legacy_location.ex

I tried it with this code:

lib/feriendaten/geo/registry.ex

defmodule Feriendaten.Geo.Registry do
  use Ash.Registry,
    extensions: [
      # This extension adds helpful compile time validations
      Ash.Registry.ResourceValidations
    ]

  entries do
    entry Feriendaten.Geo.Level
    entry Feriendaten.Geo.Location
    entry Feriendaten.Geo.School
  end
end

lib/feriendaten/legacy/registry.ex

defmodule Feriendaten.Legacy.Registry do
  use Ash.Registry,
    extensions: [
      # This extension adds helpful compile time validations
      Ash.Registry.ResourceValidations
    ]

  entries do
    entry Feriendaten.Legacy.LegacyLocation
  end
end

But this results into this error:

Compiling 4 files (.ex)
** (EXIT from #PID<0.95.0>) an exception was raised:
    ** (RuntimeError) Resources Feriendaten.Geo.Location, Feriendaten.Geo.Level must be included in the registry.

Is the file path of the registry.ex in the tutorial misleading or am I doing a mistake? How can I fix it?

wintermeyer
  • 8,178
  • 8
  • 39
  • 85

1 Answers1

1

So the file path will never matter for Ash Framework libraries. We make no assertion on the file name. The only thing at play here is what resources are in your registry.

Registries are just tools to list the modules available to a given api. What is not shown here, I believe, is an api module with something like this:

resources do
  registry Foo
  registry Bar
end

The problem here is that you can only have one registry per api. The second call to registry is simply overwriting the first one.

In Ash, to split up our application, we create multiple APIs each with their own registry. Then, to relate across api modules, you use the api option. I.e

belongs_to :owner, MyApp.Accounts.User do
  api MyApp.Accounts
end

If we detect a relationship to a resource and that api either: is not in the registry also, or does not have a configured api, we raise the error that you see above.

Borromakot
  • 293
  • 1
  • 2
  • 13