Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronize lazy relationships #35

Merged
merged 1 commit into from
Jan 8, 2020

Conversation

stokarenko
Copy link
Collaborator

@stokarenko stokarenko commented Dec 26, 2019

BatchLoader behaves like a methods delegator but still can't delegate pure ruby operators:

class Level0Serializer4 < BaseTestSerializer
  lazy_has_one :level1

  attribute :conditional_level1 do
    # lazy_level1 is the instance of `BatchLoader` so the result will be always `exists`
    lazy_level1 ? 'exists' : 'missing'
  end

  attribute :safe_navigated_level1 do
    # lazy_level1 is the instance of `BatchLoader` so it will be always considered as
    # `not nil` by safe navigation operator. Then `id` method will be sent and
    # this time it will land to delegation object with a chance to raise
    # `NoMethodError: undefined method `id' for nil:NilClass`
    lazy_level1&.id
  end
end

Fortunately BatchLoader class provides public __sync method which instantly lands to delegation object keeping alive the laziness evaluation. This method is used by batch-loader gem itself for graphql integration. Lets do the same within our lazy_<relationship> instance methods.

@Bajena Bajena self-requested a review December 27, 2019 16:32
@Bajena Bajena self-assigned this Dec 27, 2019
Copy link
Owner

@Bajena Bajena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great spot! I tested it and seems to work as a charm.

One thing I spotted is that we'll be also able to remove the following "hack" (calling itself to invoke the sync) from relationship_wrapper_methods.rb. I tested it and tests seem to pass when itself call gets removed, so if you'd like to add this change here feel free to do it.

If possible please rebase this PR to master before we 🚢 :)

@Bajena
Copy link
Owner

Bajena commented Jan 8, 2020

@stokarenko before we ship this please have a look at my previous comment :)

@stokarenko stokarenko force-pushed the syncronize-lazy-relationships branch from bac52f0 to c2b7164 Compare January 8, 2020 15:20
@stokarenko
Copy link
Collaborator Author

@Bajena Done, please take a look )

Copy link
Owner

@Bajena Bajena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff! I'll release a new version today 🎉

@Bajena Bajena merged commit bece2df into Bajena:master Jan 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants