You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We need to apply the same extension options to several fields. To reduce duplication, we'd like to use the .with_options method provided by ActiveSupport on any Object but it does not work as expected.
Steps to reproduce
Imagine we have something like that:
# May come from an external gemclassExtension < ::GraphQL::Schema::FieldExtensiondefapply=@name=options.delete :namedefresolve(context:,object:,arguments:, **_rest)=yieldobject,arguments,name:
endclassField < ::GraphQL::Schema::Fielddefinitialize(...)superextension(Extension)endendclassMutation < Types::Objects::Basefield_classFieldwith_optionsextension_options: {name: :value}dofield:record_create,mutation: Records::Createfield:record_update,mutation: Records::Updateendend
In GraphQL::Schema::FieldExtension, the constructor simply assign options to the extension. If the extension manipulate its options Hash (with #merge or #delete), it will also be altered for subsequent calls. In case of a #delete, later fields will not be extended.
Expected behavior
with_options should work as expected and actually forward options to every .field call.
Actual behavior
with_options works as expected but field extensions may consume options in a destructive way.
This can be fixed with a single line change as:
module GraphQL
class Schema
class FieldExtension
# […]
def initialize(field:, options:)
@field = field
- @options = options || {}+ @options = options&.dup || {}
@added_default_arguments = nil
apply
end
# […]
end
end
end
The text was updated successfully, but these errors were encountered:
I don't want to add a .dup to the default behavior because most of the time, it will be a needless allocation. I'd rather keep the default path as efficient as I can.
Instead, you might:
Implement def initialize to copy the incoming options, as you've suggested above;
Sadly, the first two solutions can't be applied to field extensions provided by an external gem (without monky-patching them) but I understand your point.
Describe the bug
We need to apply the same extension options to several fields. To reduce duplication, we'd like to use the
.with_options
method provided by ActiveSupport on any Object but it does not work as expected.Steps to reproduce
Imagine we have something like that:
In
GraphQL::Schema::FieldExtension
, the constructor simply assign options to the extension. If the extension manipulate its options Hash (with#merge
or#delete
), it will also be altered for subsequent calls. In case of a#delete
, later fields will not be extended.Expected behavior
with_options
should work as expected and actually forward options to every.field
call.Actual behavior
with_options
works as expected but field extensions may consume options in a destructive way.This can be fixed with a single line change as:
The text was updated successfully, but these errors were encountered: