-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Decorating types #73
Comments
I though of a few options, do any of them strike your fancy?
|
Thanks for the thorough and insightful response :) I was hoping I could use the middleware approach, just wasn't sure how. I'll try and apply these to my project and see which approach fits me best. |
I tried the middleware approach but couldn't make it work. From #2479 (comment) I found Field Extension is an alternative. The Field Extension approach would be something like: # decoration_extension.rb
class DecorationExtension < GraphQL::Schema::FieldExtension
def after_resolve(value:, **options)
decorator_class = options[:decorator_class] || value.decorator_class
# If you're using Draper. Use `comments.map(&:decorate)` instead of the `comments.decorate` shorthand otherwise AssociationLoader won't work properly.
if value.respond_to? :map
value.map{|v| decorator_class.new(v) }
else
decorator_class.new(value)
end
end
end
# types/article_type.rb
module Types
class ArticleType < Types::BaseObject
field :comments, [Types::CommentType], 'All comments of this article', extensions: [DecorationExtension]
field :likes, [Types::LikeType], 'All comments of this article' do
extension(DecorationExtension, decorator_class: LikeDecorator)
end
# if you are using graphql-batch
def comments
Dataloader::AssociationLoader.for(Article, :comments).load(object)
end
end
end
# types/comment_type.rb
module Types
class ArticleType < Types::BaseObject
field :a_method_of_comment, ...
field :a_medhot_of_comment_decorator, ...
end
end |
Is there a good (read: D.R.Y.) way to Decorate objects?
Right now, we have some (ugly) code looking like this:
So I'm thinking something like
coerce
, but for object types:I've naively tried to do this using
coerce
, but it doesn't seem to work for object types.The text was updated successfully, but these errors were encountered: