Instantly share code, notes, and snippets.

@palkan /example.rb Secret
Last active Oct 31, 2018

Embed
What would you like to do?
Custom GraphQL option
module CustomField
def initialize(*args, **kwargs, &block)
@my_custom_option = kwargs.delete(:custom)
super(*args, **kwargs, &block)
end
def resolve_field(obj, args, ctx)
return super if @my_custom_option.nil?
# do custom logic
end
end
# Extend you base field with custom logic
class ExampleType < GraphQL::Schema::Object
field_class.prepend(CustomField)
# now you can use `custom` option and enhance
# the default logic
field :name, String, custom: true
end
# you can also patch GraphQL::Schema::Field class globally
GraphQL::Schema::Field.prepend(CustomField)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment