graphiti
graphiti copied to clipboard
Allow `attribute :id` and `filter :id` to be inherited from the root resource
This seems to fix it for me:
diff --git i/lib/graphiti/resource/dsl.rb w/lib/graphiti/resource/dsl.rb
index 7353edf..867fcf4 100644
--- i/lib/graphiti/resource/dsl.rb
+++ w/lib/graphiti/resource/dsl.rb
@@ -142,6 +142,7 @@ module Graphiti
end
def apply_attributes_to_serializer
+ return unless serializer
serializer.type(type)
Util::SerializerAttributes.new(self, attributes).apply
end
A bit of a longer explanation on this one:
So, we always want the serializer and type
to inherit correctly - TopPostResource
should have the same type
and a subclass of the same serializer as PostResource
. However, this is not true when the resource is self.abstract_class = true
like ApplicationResource
- we wouldn't want to inherit a type of application_resources
.
So, we nil-out these properties when setting an abstract class. This is why you get an error at that line to begin with - there's no serializer because we removed it with abstract_class
. If you remove that code, you'll see a ton of spec failures.
There may be a better way to work around this, but another strategy might be to raise an error telling the user to do self.inherited
when trying to add an attribute, filter etc on a base class:
def self.inherited(klass)
super
klass.attribute(:id, :string) { @object.guid }
end
As this is specific to inheriting ID from the parent, we could also do this with a class attribute (on mobile so forgive formatting):
ApplicationResource self.default_id_type = :uuid end
@richmolj is @wadetandy suggestion something that could be added to the library?
I think it would be nice to be able to configure the ID attribute from an abstract resource I'm think about ::default_id_sttribute=(definition)
or ::default_id_attribute(type, options = {}, &blk)
.
What about something like this? https://github.com/graphiti-api/graphiti/compare/master...masterT:add-resource-id-attribute-definition?expand=1