From a7d457b55782c1ee6a0eb77b5ae3bd7650801ae5 Mon Sep 17 00:00:00 2001 From: mikkoh Date: Wed, 18 Jan 2017 15:35:11 -0500 Subject: [PATCH 1/2] Added field_from_name to TypeDefinition --- lib/graphql_schema.rb | 5 +++++ test/graphql_schema_test.rb | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/lib/graphql_schema.rb b/lib/graphql_schema.rb index e8a8983..45c5d6b 100644 --- a/lib/graphql_schema.rb +++ b/lib/graphql_schema.rb @@ -209,6 +209,11 @@ def fields(include_deprecated: false) include_deprecated ? @fields : @fields.reject(&:deprecated?) end + def field_from_name(field_name, include_deprecated: false) + @fields_by_name ||= fields(include_deprecated: include_deprecated).map{ |field| [field.name, field]}.to_h + @fields_by_name[field_name] + end + def input_fields @input_fields ||= @hash.fetch('inputFields').map{ |field_hash| InputValue.new(field_hash) } end diff --git a/test/graphql_schema_test.rb b/test/graphql_schema_test.rb index 3f43b50..9c67e8f 100644 --- a/test/graphql_schema_test.rb +++ b/test/graphql_schema_test.rb @@ -128,6 +128,11 @@ def test_enum? assert_equal false, field('QueryRoot', 'keys').type.enum? end + def test_field_from_name? + assert_equal get_string_field, type('QueryRoot').field_from_name('get_string') + assert_equal nil, type('QueryRoot').field_from_name('does_not_exist') + end + def test_description assert_equal 'Time since epoch in seconds', type('Time').description assert_nil type('StringEntry').description From 5458a81a02364175fc8114da7188e258f2a9f8af Mon Sep 17 00:00:00 2001 From: mikkoh Date: Mon, 20 Mar 2017 14:20:52 -0400 Subject: [PATCH 2/2] Exposed fields_by_name --- lib/graphql_schema.rb | 5 ++--- test/graphql_schema_test.rb | 11 ++++++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/graphql_schema.rb b/lib/graphql_schema.rb index 45c5d6b..b6f9cd3 100644 --- a/lib/graphql_schema.rb +++ b/lib/graphql_schema.rb @@ -209,9 +209,8 @@ def fields(include_deprecated: false) include_deprecated ? @fields : @fields.reject(&:deprecated?) end - def field_from_name(field_name, include_deprecated: false) - @fields_by_name ||= fields(include_deprecated: include_deprecated).map{ |field| [field.name, field]}.to_h - @fields_by_name[field_name] + def fields_by_name + @fields_by_name ||= fields(include_deprecated: true).map{ |field| [field.name, field]}.to_h end def input_fields diff --git a/test/graphql_schema_test.rb b/test/graphql_schema_test.rb index 9c67e8f..6f5ea6e 100644 --- a/test/graphql_schema_test.rb +++ b/test/graphql_schema_test.rb @@ -128,9 +128,10 @@ def test_enum? assert_equal false, field('QueryRoot', 'keys').type.enum? end - def test_field_from_name? - assert_equal get_string_field, type('QueryRoot').field_from_name('get_string') - assert_equal nil, type('QueryRoot').field_from_name('does_not_exist') + def test_fields_by_name + assert_equal get_string_field, type('QueryRoot').fields_by_name['get_string'] + assert_equal get_field, type('QueryRoot').fields_by_name['get'] + assert_equal nil, type('QueryRoot').fields_by_name['does_not_exist'] end def test_description @@ -194,6 +195,10 @@ def query_root type('QueryRoot') end + def get_field + field('QueryRoot', 'get') + end + def get_string_field field('QueryRoot', 'get_string') end