rmosolgo / graphql-ruby

Ruby implementation of GraphQL
http://graphql-ruby.org
MIT License
5.38k stars 1.39k forks source link

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

Closed fabioxgn closed 2 weeks ago

fabioxgn commented 3 weeks ago

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)

rmosolgo commented 3 weeks ago

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):

https://github.com/rmosolgo/graphql-ruby/blob/dcaaed1cea47394fad61fceadf291ff3cb5f2932/lib/graphql/schema/visibility/profile.rb#L126

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

fabioxgn commented 3 weeks ago

Thanks @rmosolgo!

rmosolgo commented 2 weeks ago

👋 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!