From 39e9213d90bb18c7f218085e1dcaed8f2b6fa835 Mon Sep 17 00:00:00 2001 From: Lenny Burdette Date: Thu, 1 Aug 2019 10:23:23 -0700 Subject: [PATCH] fix: Handle rails params in Any (#13) * Handle rails params in Any * chore: test for rails strong parameters coercion --- Gemfile.lock | 42 ++++++++++++++++++++++++++++++ apollo-federation.gemspec | 1 + lib/apollo-federation/any.rb | 6 +++-- spec/apollo-federation/any_spec.rb | 10 +++++++ 4 files changed, 57 insertions(+), 2 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index fdd82d0ac..2fcf0a666 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -7,13 +7,44 @@ PATH GEM remote: https://rubygems.org/ specs: + actionpack (5.2.3) + actionview (= 5.2.3) + activesupport (= 5.2.3) + rack (~> 2.0) + rack-test (>= 0.6.3) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.0, >= 1.0.2) + actionview (5.2.3) + activesupport (= 5.2.3) + builder (~> 3.1) + erubi (~> 1.4) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.0, >= 1.0.3) + activesupport (5.2.3) + concurrent-ruby (~> 1.0, >= 1.0.2) + i18n (>= 0.7, < 2) + minitest (~> 5.1) + tzinfo (~> 1.1) ast (2.4.0) + builder (3.2.3) byebug (11.0.1) coderay (1.1.2) + concurrent-ruby (1.1.5) + crass (1.0.4) diff-lcs (1.3) + erubi (1.8.0) graphql (1.9.6) + i18n (1.6.0) + concurrent-ruby (~> 1.0) jaro_winkler (1.5.3) + loofah (2.2.3) + crass (~> 1.0.2) + nokogiri (>= 1.5.9) method_source (0.9.2) + mini_portile2 (2.4.0) + minitest (5.11.3) + nokogiri (1.10.3) + mini_portile2 (~> 2.4.0) parallel (1.17.0) parser (2.6.3.0) ast (~> 2.4.0) @@ -24,6 +55,13 @@ GEM byebug (~> 11.0) pry (~> 0.10) rack (2.0.7) + rack-test (1.1.0) + rack (>= 1.0, < 3) + rails-dom-testing (2.0.3) + activesupport (>= 4.2.0) + nokogiri (>= 1.6) + rails-html-sanitizer (1.0.4) + loofah (~> 2.2, >= 2.2.2) rainbow (3.0.0) rake (12.3.2) rspec (3.8.0) @@ -49,12 +87,16 @@ GEM rubocop-rspec (1.33.0) rubocop (>= 0.60.0) ruby-progressbar (1.10.1) + thread_safe (0.3.6) + tzinfo (1.2.5) + thread_safe (~> 0.1) unicode-display_width (1.6.0) PLATFORMS ruby DEPENDENCIES + actionpack apollo-federation! pry-byebug rack diff --git a/apollo-federation.gemspec b/apollo-federation.gemspec index ecad4165f..b6d14d188 100644 --- a/apollo-federation.gemspec +++ b/apollo-federation.gemspec @@ -28,6 +28,7 @@ Gem::Specification.new do |spec| spec.add_dependency 'graphql' + spec.add_development_dependency 'actionpack' spec.add_development_dependency 'pry-byebug' spec.add_development_dependency 'rack' spec.add_development_dependency 'rake' diff --git a/lib/apollo-federation/any.rb b/lib/apollo-federation/any.rb index d4193a68e..15eb73f8f 100644 --- a/lib/apollo-federation/any.rb +++ b/lib/apollo-federation/any.rb @@ -9,8 +9,10 @@ class Any < GraphQL::Schema::Scalar def self.coerce_input(value, _ctx) # TODO: Should we convert it to a Mash-like object? result = {} - value.each_key do |key| - result[key.to_sym] = value[key] + + # `value` can be an ActionController::Parameters instance + value.each_pair do |key, val| + result[key.to_sym] = val end result diff --git a/spec/apollo-federation/any_spec.rb b/spec/apollo-federation/any_spec.rb index 16134cfd4..e5281fbff 100644 --- a/spec/apollo-federation/any_spec.rb +++ b/spec/apollo-federation/any_spec.rb @@ -2,6 +2,7 @@ require 'spec_helper' require 'apollo-federation/any' +require 'action_controller' RSpec.describe ApolloFederation::Any do it 'converts the keys to symbols' do @@ -9,4 +10,13 @@ described_class.coerce_input({ 'one' => 1, 'two' => 2, '__typename' => 'Thing' }, nil), ).to eql(one: 1, two: 2, __typename: 'Thing') end + + it 'converts ActionController::Parameters' do + params = ActionController::Parameters.new( + 'one' => 1, 'two' => 2, '__typename' => 'Thing', + ) + expect( + described_class.coerce_input(params, nil), + ).to eql(one: 1, two: 2, __typename: 'Thing') + end end