Skip to content

Instantly share code, notes, and snippets.

@palkan
Last active October 31, 2018 20:55
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save palkan/26958308cfbba6ae5d46b0036a2b52bb to your computer and use it in GitHub Desktop.
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