diff --git a/lib/graphql_schema.rb b/lib/graphql_schema.rb index e8a8983..b6f9cd3 100644 --- a/lib/graphql_schema.rb +++ b/lib/graphql_schema.rb @@ -209,6 +209,10 @@ def fields(include_deprecated: false) include_deprecated ? @fields : @fields.reject(&:deprecated?) end + def fields_by_name + @fields_by_name ||= fields(include_deprecated: true).map{ |field| [field.name, field]}.to_h + 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..6f5ea6e 100644 --- a/test/graphql_schema_test.rb +++ b/test/graphql_schema_test.rb @@ -128,6 +128,12 @@ def test_enum? assert_equal false, field('QueryRoot', 'keys').type.enum? end + 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 assert_equal 'Time since epoch in seconds', type('Time').description assert_nil type('StringEntry').description @@ -189,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