Imported component does not lazy-load and importing-as-option issue

Hey :wave:,

I have an Hanami 2.1 app main and a slice part_one.
In part_one I import a repository from main.

# config/slices/part_one.rb
module Vouchers
  class Slice < Hanami::Slice
    config.no_auto_register_paths << "graphql" << "entities"
    import keys: ["repos.repo_one"], from: Hanami.app.container, as: "app"
  end
end

With this setting I have 2 issues:

#1 Lazy-Loading
In tests my components should load lazy, but it does not work for the key "app.repos.repo_one".

Here’s the console output:

lazy-load does not work for the imported component:

my_app[development]> PartOne::Slice["app.repos.repo_one"]
Dry::Core::Container::KeyError: key not found: "app.repos.repo_one"
from /usr/local/bundle/gems/dry-core-1.0.1/lib/dry/core/container/resolver.rb:32:in `block in call'

after booting the slice, it is available:

my_app[development]> PartOne::Slice.boot
=> PartOne::Slice
my_app[development]> PartOne::Slice["app.repos.repo_one"]
=> #<MyApp::Repos::RepoOne struct_namespace=ROM::Struct auto_struct=true>

#2 Importing without :as-option

# config/slices/part_one.rb
module Vouchers
  class Slice < Hanami::Slice
    config.no_auto_register_paths << "graphql" << "entities"
    import keys: ["repos.repo_one"], from: Hanami.app.container#, as: "app"
  end
end

If I do this, the importing does not work at all. The key “repos.repo_one” is not available at Slice part_one, even after the boot. Is this expected?

Thanks!
best regards

I have a similar question to yours. I would like to place it here to make answering both of us easier. I hope that is okay. :pray:

I would like to import selected components from a slice into app, as opposed to exporting from app, as in @wuarmin’s case. Perhaps a general explanation of importing/exporting between app and slices would be useful to both of us.

With regard to your issue, if it fits your use case, you can make app components generally available to all slices using shared_app_component_keys. I’m not sure if they are lazy-loaded that way. You probably knew that, but I thought it worth mentioning.

@wuarmin, I am curious. If you import without the as: option, does repo_one show up under a different key? or is it completely absent?

@dcr8898 Thanks for your reply.

With regard to your issue, if it fits your use case, you can make app components generally available to all slices using shared_app_component_keys. I’m not sure if they are lazy-loaded that way. You probably knew that, but I thought it worth mentioning.

When I use the shared_app_component_keys-feature, lazy-loading works as expected.

@wuarmin, I am curious. If you import without the as: option, does repo_one show up under a different key? or is it completely absent?
It’s completely absent.

I spent today in the source code and I’m afraid I can’t find a solution for either of our issues.

It seems to me that the app slice is the parent of any other defined slices, and that it has a different start-up process than other slices. I suspect this difference is why your lazy-loading doesn’t work. This may be a bug that someone more familiar with the code can identify.

My issue looks a little more structural–it may not be intended behavior at this point. I will start another thread to focus that. Sorry I couldn’t help more.

Thanks for this informative report, folks. Appreciate you poking at it too, Damian.

I can have a look at this for you next week, and hopefully get you some clear answers.

1 Like