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

WIP: Allow an adapter to return a new object to replace the existing one. #367

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mattgibson
Copy link

Opening a PR for discussion. I can refactor and add specs as advised if this is acceptable.

The intention of this change is to enable the use of an adapter that returns Dry::Structs, which are immutable. Associating parents or children needs to involve creation of a new object, rather than modifying the one passed in by reference as a parameter, which is currently what Graphiti expects. This small change has no functional effects on the rest of the code, but does allow adapters to return new objects.

The adapter at the moment is WIP too, but looks a bit like this:

module Common
  module Graphiti
    module Adapters
      # An adapter to allow us to return entities from create and update operations
      # which are done using interactors. Reads for show and index operations
      # remain as active record.
      class EntityAdapter < ::Graphiti::Adapters::ActiveRecord
        # In order to keep the entities read-only we don't want to have setter methods,
        # which graphiti usually looks for with non-active-record objects. Instead,
        # we create new immutable Dry::Structs with the extra associations
        def associate(parent, child, association_name, association_type) # rubocop:disable ModularMonolith/Arguments
          new_parent = create_new_entity_with_child(parent, child, association_name, association_type)
          copy_across_graphiti_internal_instance_variables(parent, new_parent)
          new_parent
        end

        private

        def create_new_entity_with_child(parent, child, association_name, association_type)
          if association_type.in?([:belongs_to, :has_one])
            parent.class.new(parent.attributes.merge(association_name => child))
          else
            parent.class.new(parent.attributes.merge(association_name => child.to_a))
          end
        end

        def copy_across_graphiti_internal_instance_variables(parent, new_parent)
          %w(
            @__graphiti_serializer
            @__graphiti_resource
            @_jsonapi_temp_id
          ).each do |instance_variable_name|
            new_parent.instance_variable_set(instance_variable_name,
                                             parent.instance_variable_get(instance_variable_name))
          end
        end
      end
    end
  end
end

This is to enable the use of an adapter that returns Dry::Structs, which are immutable. Associating parents or children needs to involve creation of a new object, rather than modifying the one passed in by reference as a parameter.
@richmolj
Copy link
Contributor

Thanks for this @mattgibson ❤️ ! I haven't taken a look at the code yet but wanted to give a fast response.

Dry::Struct is such a pain but I agree we should support it. I wonder, though, if we can put a lot of this code into the model layer (with a mixin) instead of the adapter (like the code copying instance vars)?

@jkeen jkeen marked this pull request as draft February 27, 2024 20:00
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.

None yet

2 participants