rmosolgo/graphql-ruby

Enum types require at least one value after adding `use GraphQL::Schema::Visibility` to schema

fabioxgn opened this issue · 3 comments

I have an enum that is dynamically generating the values, like this (removed all other:

module Types
  class BaseEnum < GraphQL::Schema::Enum
    enum_value_class(Types::BaseEnumValue)
  end

  class ReportSlugEnum < Types::BaseEnum
    description "Available reports"

    def self.enum_values(_context)
      {
        some_report: Reports::SomeReport,
        ... # more reports here
      }.map { |k, v|
        GraphQL::Schema::EnumValue.new(k.to_s, owner: self, value: v.name)
      }

After upgrading to 2.4.0, when I add use GraphQL::Schema::Visibility to my schema I get this error:

Unexpected exception raised: GraphQL::Schema::Enum::MissingValuesError
Enum types require at least one value, but ReportSlugEnum didn't provide any for this query. Make sure at least one value is defined and visible for this query.

/Users/fabio/.asdf/installs/ruby/3.2.3/lib/ruby/gems/3.2.0/gems/graphql-2.4.0/lib/graphql/schema/visibility/profile.rb:128:in `block in initialize'
/Users/fabio/.asdf/installs/ruby/3.2.3/lib/ruby/gems/3.2.0/gems/graphql-2.4.0/lib/graphql/schema/visibility/profile.rb:292:in `enum_values'
/Users/fabio/.asdf/installs/ruby/3.2.3/lib/ruby/gems/3.2.0/gems/graphql-2.4.0/lib/graphql/schema/visibility/profile.rb:497:in `visit_type'
/Users/fabio/.asdf/installs/ruby/3.2.3/lib/ruby/gems/3.2.0/gems/graphql-2.4.0/lib/graphql/schema/visibility/profile.rb:414:in `load_all_types'
/Users/fabio/.asdf/installs/ruby/3.2.3/lib/ruby/gems/3.2.0/gems/graphql-2.4.0/lib/graphql/schema/visibility/profile.rb:170:in `type'

Versions

graphql version: 2.4.
rails (or other framework): 7.0.8.5
other applicable versions (graphql-batch, etc)

Hey, sorry for the trouble and thanks for this report. The problem is that Visibility isn't calling def enum_values (but it should!), instead it's calling all_enum_value_definitions (which is only populated via enum_value(...) configs):

values = non_duplicate_items(enum_t.all_enum_value_definitions, @cached_visible)

I'll update it to work with an example like yours and follow up back here!

👋 I just released 2.4.1 with a patch for this issue. Please give it a try and let me know if you run into any other problems on that version!