Created
September 13, 2018 13:32
-
-
Save trevorrjohn/9520f7d59bd2c7318b10e71c15849ebf to your computer and use it in GitHub Desktop.
Convert ActionController::Params to boolean values for GraphQL-ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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