Skip to content

Instantly share code, notes, and snippets.

@trevorrjohn
Created September 13, 2018 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trevorrjohn/9520f7d59bd2c7318b10e71c15849ebf to your computer and use it in GitHub Desktop.
Save trevorrjohn/9520f7d59bd2c7318b10e71c15849ebf to your computer and use it in GitHub Desktop.
Convert ActionController::Params to boolean values for GraphQL-ruby
# config/initializers/graphql_boolean_parameter.rb
# frozen_string_literal: true
GraphQL::BOOLEAN_TYPE.class_eval do
REGEX = /^(true|false|t|f|0|1)$/
define_method(:validate_input) do |value, ctx|
super(coerce_input(value, ctx), ctx)
end
define_method(:coerce_input) do |value, ctx|
if value.respond_to?(:match?) && value.match?(REGEX)
ActiveRecord::Type::Boolean.new.deserialize(value)
else
super(value, ctx)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment